我想使用Java 8以递归方式列出计算机上的所有文件.
Java 8提供了listFiles一种返回所有文件和目录但没有递归的方法.如何使用它来获取完整的递归文件列表(不使用变异集合)?
我已经尝试了下面的代码,但它只有一个深度:
static Function<Path, Stream<Path>> listFiles = p -> {
if (p.toFile().isDirectory()) {
try { return Files.list(p); }
catch (Exception e) { return Stream.empty(); }
} else {
return Stream.of(p);
}
};
public static void main(String[] args) throws IOException {
Path root = Paths.get("C:/temp/");
Files.list(root).flatMap(listFiles).forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
并且使用return Files.list(p).flatMap(listFiles);不编译(不确定为什么)...
注意:我对涉及FileVisitors或外部库的解决方案不感兴趣.
在阅读API时,DirectoryStream我错过了很多功能.首先,它建议使用for循环从流到List.我想念它DirectoryStream不是一个事实Stream.
我怎样才能让一个Stream<Path>从DirectoryStreamJava中8?
我有要并行处理的元素集合.当我使用a时List,并行性有效.但是,当我使用a时Set,它并不是并行运行的.
我写了一个显示问题的代码示例:
public static void main(String[] args) {
ParallelTest test = new ParallelTest();
List<Integer> list = Arrays.asList(1,2);
Set<Integer> set = new HashSet<>(list);
ForkJoinPool forkJoinPool = new ForkJoinPool(4);
System.out.println("set print");
try {
forkJoinPool.submit(() ->
set.parallelStream().forEach(test::print)
).get();
} catch (Exception e) {
return;
}
System.out.println("\n\nlist print");
try {
forkJoinPool.submit(() ->
list.parallelStream().forEach(test::print)
).get();
} catch (Exception e) {
return;
}
}
private void print(int i){
System.out.println("start: " + i);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}
System.out.println("end: …Run Code Online (Sandbox Code Playgroud) 我是Java 8中使用Lambda表达式功能的初学者.Lambda表达式在解决Prime数字检查,阶乘等程序方面非常有用.
然而,它们可以有效地用于解决像Fibonacci这样的问题,其中当前值取决于前两个值的总和.我已经很好地使用Lambda表达式有效地解决了素数检查问题.下面给出了相同的代码.
boolean checkPrime=n>1 && LongStream.range(2, (long) Math.sqrt(n)).parallel().noneMatch(e->(n)%e==0);
Run Code Online (Sandbox Code Playgroud)
在方法的上述代码中,noneMatch我们使用范围中的当前值(e)进行评估.但对于Fibonacci问题,我们需要前两个值.
我们怎样才能实现呢?
假设我有一个单词列表,我想创建一个方法,将新列表的大小作为参数并返回新列表.如何从原始sourceList中获取随机单词?
public List<String> createList(int listSize) {
Random rand = new Random();
List<String> wordList = sourceWords.
stream().
limit(listSize).
collect(Collectors.toList());
return wordList;
}
Run Code Online (Sandbox Code Playgroud)
那么我如何以及在哪里使用我的随机数?
我有文件路径列表:.
List<Path> filePaths; //e.g. [src\test\resources\file\15\54\54_exampleFile.pdf]
Run Code Online (Sandbox Code Playgroud)
54 以上是指文件ID
然后,我获得了一个我的应用程序可以处理Set的StringID,如下所示:
Set<String> acceptedIds = connection.getAcceptedIDs(); //e.g. elements [64, 101, 33]
Run Code Online (Sandbox Code Playgroud)
如何使用Java 8 lambdas来filter排除filePaths其中不包含acceptedIds集合Set中包含的任何可接受的ID的所有元素.
换句话说,我只想保留filePaths具有acceptedIdsset中的id的路径.例如,54不在上面的列表中,因此被删除.
filePaths.stream().filter(...).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 如何从List使用Java 8 Streams 获取下一个元素?
如果我迭代a List,我想比较当前与列表的下一个元素.
是否可以使用Java 8 Stream?
我有两个结构如下的类:
public class Company {
private List<Person> person;
...
public List<Person> getPerson() {
return person;
}
...
}
public class Person {
private String tag;
...
public String getTag() {
return tag;
}
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,Company类有一个Person对象列表,每个Person对象都可以获得Tag值.
如果我得到Person对象的List,有没有办法使用来自Java 8的Stream来找到所有Person对象中最常见的一个Tag值(如果是平局,可能只是最随机的一个)共同)?
String mostCommonTag;
if(!company.getPerson().isEmpty) {
mostCommonTag = company.getPerson().stream() //How to do this in Stream?
}
Run Code Online (Sandbox Code Playgroud) 给出以下代码:
List<String> strList = new ArrayList<>(Arrays.asList("Java","Python","Php"));
Stream<String> jFilter = strList.stream().filter(str -> str.startsWith("J"));
strList.add("JavaScript"); // element added after filter creation
strList.add("JQuery"); // element added after filter creation
System.out.println(Arrays.toString(jFilter.toArray()));
Run Code Online (Sandbox Code Playgroud)
哪个输出:
[Java, JavaScript, JQuery]
Run Code Online (Sandbox Code Playgroud)
即使在创建过滤后的流后添加了过滤结果,为什么会出现JavaScript并JQuery显示在过滤结果中?
我想转换我看起来像这样的地图:
{
key="someKey1", value=Apple(id="1", color="green"),
key="someKey2", value=Apple(id="2", color="red"),
key="someKey3", value=Apple(id="3", color="green"),
key="someKey4", value=Apple(id="4", color="red"),
}
Run Code Online (Sandbox Code Playgroud)
到另一个地图,将所有相同颜色的苹果放入同一个列表中:
{
key="red", value=list={apple1, apple3},
key="green", value=list={apple2, apple4},
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
Map<String, Set<Apple>> sortedApples = appleMap.entrySet()
.stream()
.collect(Collectors.toMap(l -> l.getColour, ???));
Run Code Online (Sandbox Code Playgroud)
我是在正确的轨道上吗?我应该使用过滤器来执行此任务吗?有没有更简单的方法?