简而言之,就是input.stream()
+ 具体操作
+ collect(Collectors.toList())
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class streamLearn {
public static List<String> removeEmpty(List<String> input) {
List<String> output = input.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
return output;
}
public static List<String> removalDuplicate(List<String> input) {
List<String> output = input.stream().distinct().collect(Collectors.toList());
return output;
}
public static List<Integer> calculate(List<Integer> input) {
List<Integer> output = input.stream().map(i -> i*i).collect(Collectors.toList());
return output;
}
public static Long count(List<Integer> input) {
Long output = input.stream().count();
return output;
}
public static Long countParallel(List<Integer> input) {
Long output = input.parallelStream().count();
return output;
}
public static void main(String[] args) throws InterruptedException {
System.out.println(removeEmpty(Arrays.asList("a", "", "b", "c")));
System.out.println(removalDuplicate(Arrays.asList("a", "a", "b", "c")));
System.out.println(calculate(Arrays.asList(1,2,3,4,5)));
System.out.println(count(Collections.nCopies(100, 1)));
System.out.println(countParallel(Collections.nCopies(100, 1)));
}
}
//输出如下:
//[a, b, c]
//[a, b, c]
//[1, 4, 9, 16, 25]
//100
//100
猜你喜欢:👇🏻
⭐【Java】自动设置(set)和获取(get)类里的变量
⭐【Java】列表那些入门的list操作方法
⭐【Java】读取本地Json文件