List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
long count = strings.stream().filter(x -> x.contains("a")).count();
List<String> a = strings.stream().filter(x -> x.contains("a")).collect( Collectors.toList());
System.out.println(a);
Random random=new Random();
random.ints(10).forEach(System.out::println);
//map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素的整数值:
List<Integer> collect = strings.stream().filter(a -> !a.isEmpty()).map(x -> x.length()).distinct().collect(Collectors.toList());
System.out.println(collect);
strings.stream().filter(a->!a.isEmpty()).sorted((x,y)->x.length()-y.length()).forEach(System.out::println);
List<String> sortedcollect = strings.stream().filter(a -> !a.isEmpty()).sorted((x, y) -> x.length() - y.length()).collect(Collectors.toList());
System.out.println(sortedcollect);
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
System.out.println("=================================");
numbers.stream().sorted().forEach(System.out::println);
System.out.println("=================================");
numbers.stream().sorted((x,y)->y-x).forEach(System.out::println);
Map<Integer, Integer> collect = numbers.stream().distinct().collect(Collectors.toMap(str -> numbers.indexOf(str), str -> str));
System.out.println(collect);
IntSummaryStatistics intSummaryStatistics = numbers.stream().mapToInt(x -> x).summaryStatistics();
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getAverage());
double payPercent=123.349;
System.out.println((double)Math.round(payPercent*100)/100);
// 然并卵 add by dt 17:36 2021/9/13
double round = (double) Math.round((intSummaryStatistics.getAverage() * 100)) / 100;
System.out.println(round);
System.out.println( intSummaryStatistics.getAverage());
System.out.println(intSummaryStatistics.getSum());
System.out.println(intSummaryStatistics.getCount());
System.out.println(intSummaryStatistics.getMin());
Map map = new HashMap<String, String>();
map.put("张三", "20");
map.put("李四", "21");
String name = "张三";
map.forEach((key, val) -> {
if ("张三".equals(key)) {
System.out.println("存在数据-》键:" + key + " 值:" + val);
return;
}
});
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
String a = Optional.ofNullable(strings)
.filter(list -> CollectionUtils.isNotEmpty(list))
.map(strings1 -> strings1.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList()))
.map(strings1 -> strings1.get(1))
.orElse("");
System.out.println(a);
Java 8 新特性 Lambda&Stream
最新推荐文章于 2024-08-18 22:05:18 发布