java8 集合神操作


  

 1 public class StreamUtils {
 2 
 3     
 4 
 5     private static final List<Integer> listInteger = Lists.newArrayList(1, 2, 3, 4, 5, 6, 3, 5, 1, 4, 2, 8, 9);
 6 
 7     private static final List<Integer> arrayList = Lists.newArrayList(1, 25, 6, 9, 22, 44);
 8 
 9     public static void main(String[] args) {
10         ///取%2的數
11         List<Integer> collect = listInteger.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
12         System.out.println(collect);
13         ///去重
14         List<Integer> collect1 = listInteger.stream().distinct().collect(Collectors.toList());
15         System.out.println(collect1);
16 
17         ///跳過前面3個元素
18         List<Integer> collect2 = listInteger.stream().skip(3).collect(Collectors.toList());
19         System.out.println(collect2);
20 
21         ///取前面3個元素
22         List<Integer> collect3 = listInteger.stream().limit(3).collect(Collectors.toList());
23         System.out.println(collect3);
24 
25         ///打印dish getName集合
26         List<String> collect4 = list.stream().map(Dish::getName).collect(Collectors.toList());
27         System.out.println(collect4);
28 
29         String[] helloWord = {"hellow", "word"};
30         ///{h,e,l,l,o,w},{w,o,r,d}
31         Stream<String[]> stream = Arrays.stream(helloWord).map(s -> s.split(""));
32         ///h,e,l,l,o,w,w,o,r,d || flatMap 扁平化操作接受stream
33         Stream<String> stringStream = stream.flatMap(Arrays::stream);
34         ///去重
35         stringStream.distinct().forEach(System.out::println);
36         //allMatch 所有的元素的滿足條件
37         System.out.println(arrayList.stream().allMatch(i -> i > 50));
38 
39         ///anyMatch 當元素數組中有一個元素滿足就返回true
40         System.out.println(arrayList.stream().anyMatch(i -> i > 40));
41 
42         ///noneMatch 沒有一個元素滿足的情況下返回true
43         System.out.println(arrayList.stream().noneMatch(i -> i < 0));
44 
45         ///findAny隨機獲取一個元素
46         Optional<Integer> any = arrayList.stream().filter(i -> i > 2).findAny();
47         System.out.println(any.get());
48 
49         ///Options 中的orElse 如果返回結果是null使用orElse可以設置默認值,返回-1
50         Integer integer = arrayList.stream().filter(i -> i > 66).findAny().orElse(-1);
51         System.out.println(integer);
52 
53         ///isPresent元素是否存在,ifPresent 元素存在需要做什么事情
54         Optional<Integer> first = arrayList.stream().filter(i -> i > 10).findFirst();
55         System.out.println("optional元素是否存在:"+first.isPresent());
56         first.ifPresent(System.out::println);
57 
58         //reduce 聚合函數 將數組中的元素累加  0設置默認值初始值
59         Integer sum = arrayList.stream().reduce(0, (x, y) -> x + y);
60         System.out.println(sum);
61 
62         ///打印數組中累加的值
63         arrayList.stream().reduce((x,y)->x+y).ifPresent(System.out::println);
64 
65         ///獲取數組中的最大值
66         System.out.println(arrayList.stream().reduce(Integer::max).get());
67         ///獲取數組最小值
68         System.out.println(arrayList.stream().reduce(Integer::min).get());
69 
70         ///累加
71         arrayList.stream().reduce(Integer::sum).ifPresent(System.out::println);
72 
73  
///根據name分組
        Map<String, List<UserInfo>> collect = listUser.stream().collect(Collectors.groupingBy(UserInfo::getName));
        System.out.println(JSON.toJSONString(collect));

        ///Collectors.averagingDouble 取出平均值
        Optional.ofNullable(list.stream().collect(Collectors.averagingDouble(Dish::getOalories)))
                .ifPresent(System.out::println);

        ///collectingAndThen 對結果進行處理
        Optional.ofNullable(list.stream().collect(Collectors.collectingAndThen(Collectors.averagingDouble(Dish::getOalories),(a->"平均值:"+a))))
                .ifPresent(System.out::println);

        List<Dish> dishList = list.stream().filter(d -> d.getType().equals(Dish.Type.OTHER)).collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));

//        dishList.add(new Dish("salmon", false, 550, Dish.Type.FISH));

        System.out.println(JSON.toJSONString(dishList));

        ///打印集合個數
        Optional.ofNullable(list.stream().collect(Collectors.counting())).ifPresent(System.out::println);
        ///{OTHER=4, MEAT=3, FISH=2} 分組之后統計分組的個數
        Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.counting()))).ifPresent(System.out::println);
        ///分組之后 求出平均值 並且返回的TreeMap
        Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new,Collectors.averagingDouble(Dish::getOalories)))).ifPresent(System.out::println);
        ///DoubleSummaryStatistics 統計集合的值 DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}
        DoubleSummaryStatistics summaryStatistics = list.stream().collect(Collectors.summarizingDouble(Dish::getOalories));
        System.out.println(summaryStatistics.toString());

        ///concurrentMap 和 Map使用一樣
        ConcurrentMap<Dish.Type, List<Dish>> collect1 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType));
        System.out.println(collect1);
        ///轉換為skipListMap
        ConcurrentSkipListMap<Dish.Type, Double> collect2 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingDouble(Dish::getOalories)));

        String collect3 = list.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(",", "[", "]")));

        System.out.println(collect3);
 
         

 

74     }
75 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM