我有一张Map<String, List<String>>地图,我希望从中提取一个List<String>包含地图中所有字符串列表的字符串.我想使用java8流语法.
在旧的Java我会做:
List<String> all = new LinkedList<String>();
for (String key: map.keySet()) {
all.addAll(map.get(key));
}
return all;
Run Code Online (Sandbox Code Playgroud)
如何使用流?
我想知道如果我findAny()没有发现任何巧合,我可以在数据流中使用它,它不会返回null.
String CountryFinal= "Spain";
List<ParContriesTO> listContries= new ArrayList<SelectItem>();
listContries.add(new SelectItem(215, "Germany"));
Integer idCountry = (int) listContries.stream()
.filter(country -> country.getNoCountry().equals(CountryFinal))
.findAny().orElse(null).getCoCountry();
Run Code Online (Sandbox Code Playgroud) 给出从名称到数字列表的映射.
我想使用java 8 stream api计算每个Name的平均值.
Map<String, List<Double>> NameToQuaters = new HashMap<>();
Map<String, Double> NameToMean = ?
Run Code Online (Sandbox Code Playgroud) 为什么一个原始类型需要铸造而另一个不需要铸造?
/* This method uses stream operations to count how many numbers in a given array
* of integers are negative
*/
public static void countNegatives(int[] nums) {
long howMany = stream(nums) // or: int howMany = (int) stream(nums)
.filter(n -> n < 0)
.count();
System.out.print(howMany);
}
Run Code Online (Sandbox Code Playgroud) list.stream().filter( a-> a < 20 && a > 7).forEach(a -> System.out.println(a));
Run Code Online (Sandbox Code Playgroud)
fiter 懒惰地执行。
forEach 急切地被执行。
这意味着什么?
鉴于代码:
public Statement methodCallByName(MethodDeclaration method, String string) {
List<ExpressionStatement> expressions = method.getBody().statements().stream()
.filter(s -> s instanceof ExpressionStatement)
.map(ExpressionStatement.class::cast)
.collect(Collectors.toList());
return null;
}
Run Code Online (Sandbox Code Playgroud)
我在Eclipse Oxygen中有以下错误:
请注意,根据JDT文档statements()返回a .List
怎么了?
可能将Java int []数组的副本复制到HashSet <Integer>但很难回答这么新的问题.
我有一套我想宣布的:
int[] flattened = Arrays.stream(arcs).flatMapToInt(Arrays::stream).toArray();
Set<Integer> set = new HashSet<Integer>(Arrays.asList(flattened));
Run Code Online (Sandbox Code Playgroud)
但由于返回类型Arrays.asList是一个列表本身,它无法解决.什么是将列表int[]转换为最佳方法Set<Integer>
我写了一个线性搜索实现.我也使用Java 7和8编写了代码.
以下是我的代码:
int[] arr = new int[] { 12, 34, 94, 8, 37, 10 };
System.out.println("Enter the element to search: ");
Scanner scan = new Scanner(System.in);
int element = scan.nextInt();
boolean found = false;
// Linear search implementation using Java 7 features
for (int i = 0; i < arr.length; i++) {
if (element == arr[i]) {
int n = element;
System.out.println("Element : "+n+" has been found at the index : "+i);
found = true;
break;
}
}
// …Run Code Online (Sandbox Code Playgroud) 我正在准备使用Groovy语言的Jenkins管道脚本。我想将所有文件和文件夹移动到另一个位置。由于Groovy支持Java,因此我在下面的Java代码中执行了该操作。
管道{代理任何
stages{
stage('Organise Files'){
steps{
script{
File sourceFolder = new File("C:\\My-Source");
File destinationFolder = new File("C:\\My-Destination");
File[] listOfFiles = sourceFolder.listFiles();
echo "Files Total: " + listOfFiles.length;
for (File file : listOfFiles) {
if (file.isFile()) {
echo file.getName()
Files.copy(Paths.get(file.path), Paths.get("C:\\My-Destination"));
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码引发以下异常:
groovy.lang.MissingPropertyException:无此类属性:类文件:WorkflowScript
我也尝试了下面的代码,但也不起作用。
FileUtils.copyFile(file.path, "C:\\My-Destination");
Run Code Online (Sandbox Code Playgroud)
最后,我确实尝试使用java I / O Stream执行操作,并且代码如下:
def srcStream = new File("C:\\My-Source\\**\\*").newDataInputStream()
def dstStream = new File("C:\\My-Destination").newDataOutputStream()
dstStream << srcStream
srcStream.close()
dstStream.close()
Run Code Online (Sandbox Code Playgroud)
但它也不起作用,并引发以下异常:
java.io.FileNotFoundException:C:\ My-Source(访问被拒绝)
谁能建议我解决问题的方法,也请让我知道复制或移动文件后如何从源位置删除文件?还有一件事,在复制期间我可以使用通配符过滤一些文件夹和文件吗?也请让我知道。
需要从文件中返回包含3个或更多单词的所有单词的流.是否有更好的方式,然后使用Stream.iterate:
private Stream<String> getWordsStream(String path){
Stream.Builder<String> wordsStream = Stream.builder();
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner s = new Scanner(inputStream);
s.useDelimiter("([^a-zA-Z])");
Pattern pattern = Pattern.compile("([a-zA-Z]{3,})");
while ((s.hasNext())){
if(s.hasNext(pattern)){
wordsStream.add(s.next().toUpperCase());
}
else {
s.next();
}
}
s.close();
return wordsStream.build();
}
Run Code Online (Sandbox Code Playgroud)