非常に包括的なlambda式はリストセットを再削除し、グループ化し、ソートし、フィルタリングし、和を求め、最値メソッドツールクラス


私たちはよくリストの集合を重くします.そこで、私は自分で時間をかけてlambda式でリストの集合をどのようにカプセル化してテストして、後で迅速に使用することができます.オブジェクトのセット内の属性の重み付けなどが含まれます.特殊なリストの再削除要求があれば、以下の方法を自分で修正することができます.
以下の方法は本人が比較的簡単な書き方を検証したので、参考にしてください.
文書ディレクトリ
  • 一、重量除去
  • 二、並べ替え
  • 三、濾過、加算、最値
  • 一、重さを取り除く
    import java.util.*;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    
    public class ListUtil {
    
        public static List<String> list = Arrays.asList("1","2","2","3","3","4","4","5","6","7","8");
        public static List<City> cities = null;
        static {
            cities = new ArrayList<City>(){
                {
                    add(new City("  ",11));
                    add(new City("  ",22));
                    add(new City("  ",55));
                    add(new City("  ",33));
                    add(new City("  ",33));
                    add(new City("  ",43));
                }
            };
    
        }
        public static void main(String[] args) {
            System.out.println(ListUtil.distinctElements(list));
            System.out.println(ListUtil.getNoDuplicateElements(list));
            System.out.println(ListUtil.getDuplicateElements(list));
            System.out.println(ListUtil.getDuplicateElementsForObject(cities));
            System.out.println(ListUtil.getNoDuplicateElementsForObject(cities));
            System.out.println(ListUtil.getElementsAfterDuplicate(cities));
            System.out.println(ListUtil.getDuplicateObject(cities));
            System.out.println(ListUtil.getNoDuplicateObject(cities));
            System.out.println(ListUtil.distinctObject(cities));
        }
    
    
    
        //       [1, 2, 3, 4, 5, 6, 7, 8]
        public static <T> List<T> distinctElements(List<T> list) {
            return list.stream().distinct().collect(Collectors.toList());
        }
    
        //lambda              [1, 5, 6, 7, 8]
        public static <T> List<T> getNoDuplicateElements(List<T> list) {
            //           Map,    ,         
            Map<T, Long> map = list.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
            System.out.println("getDuplicateElements2: "+map);
            return map.entrySet().stream() // Set   Stream
                    .filter(entry -> entry.getValue() == 1) //             1   entry
                    .map(entry -> entry.getKey()) //    entry   (    )    Stream
                    .collect(Collectors.toList()); //     List
        }
    
        //lambda             [2, 3, 4]
        public static <T> List<T> getDuplicateElements(List<T> list) {
            return list.stream().collect(Collectors.collectingAndThen(Collectors
                                .groupingBy(p -> p, Collectors.counting()), map->{
                                    map.values().removeIf(size -> size ==1); // >1         ;== 1        
                                    List<T> tempList = new ArrayList<>(map.keySet());
                                    return tempList;
                                }));
        }
    
        //  set  
        public static <T> Set<T> getDuplicateElements2(List<T> list) {
            Set<T> set = new HashSet<>();
            Set<T> exist = new HashSet<>();
            for (T s : list) {
                if (set.contains(s)) {
                    exist.add(s);
                } else {
                    set.add(s);
                }
            }
            return exist;
        }
    
        /**-----------  List   --------------*/
    
        //                     [  ,   ]
        public static List<String> getDuplicateElementsForObject(List<City> list) {
            return list.stream().collect(Collectors.groupingBy(p -> p.getCity(),Collectors.counting())).entrySet().stream()
                    .filter(entry -> entry.getValue() > 1) // >1        ;==         
                    .map(entry -> entry.getKey())
                    .collect(Collectors.toList());
        }
    
        //                      [  ,   ]
        public static List<String> getNoDuplicateElementsForObject(List<City> list){
            Map<String,List<City>> map = list.stream().collect(Collectors.groupingBy(City::getCity));
            return map.entrySet().stream().filter(entry -> entry.getValue().size() == 1)
                    .map(entry -> entry.getKey()) //    entry   (    )    Stream
                    .collect(Collectors.toList()); //     List
    
        }
    
        //                [  ,   ,   ,   ]
        public static List<String> getElementsAfterDuplicate(List<City> list) {
            return list.stream().map(o->o.getCity()).distinct().collect(Collectors.toList());
        }
    
        //               
        // [[City(city=  , total=11), City(city=  , total=33)], [City(city=  , total=22), City(city=  , total=55)]]
        public static List<List<City>> getDuplicateObject(List<City> list) {
            return list.stream().collect(Collectors.groupingBy(City::getCity)).entrySet().stream()
                    .filter(entry -> entry.getValue().size() > 1) // >1        ;==         
                    .map(entry -> entry.getValue())
                    .collect(Collectors.toList());
        }
    
        //               
        //[[City(city=  , total=43)], [City(city=  , total=33)]]
        public static List<City> getNoDuplicateObject(List<City> list) {
            List<City> cities = new ArrayList<>();
            list.stream().collect(Collectors.groupingBy(City::getCity)).entrySet().stream()
                        .filter(entry -> entry.getValue().size() ==1) //>1        ;==         ;
                        .map(entry -> entry.getValue())
                        .forEach(p -> cities.addAll(p));
            return cities;
        }
    
    
        //                  
        //[City(city=  , total=11), City(city=  , total=22), City(city=  , total=33), City(city=  , total=43)]
        public static List<City> distinctObject(List<City> list) {
            return list.stream().filter(distinctByKey(City::getCity)).collect(Collectors.toList());
        }
    
        public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
            Map<Object, Boolean> seen = new ConcurrentHashMap<>();
            return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
        }
    
    }
    
    
    
    

    二、並べ替え
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class ListUtil_sort {
    
        public static List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);
        public static List<City> cities = null;
        public static List<City> cities2 = null;
        static {
            cities = new ArrayList<City>(){
                {
                    add(new City("  ",11));
                    add(new City("  ",55));
                    add(new City("  ",33));
                    add(new City("  ",33));
                }
            };
    
            cities2 = new ArrayList<City>(){
                {
                    add(new City("  ",11,11));
                    add(new City("  ",55,22));
                    add(new City("  ",33,55));
                    add(new City("  ",33,44));
                }
            };
    
        }
        public static void main(String[] args) {
            System.out.println(sort(list));
            System.out.println(reversed(list));
            System.out.println(sortForObject(cities));
            System.out.println(reversedForObject(cities));
            System.out.println(sortForObject2(cities2));
        }
    
        //list     
        public static <T> List<T> sort(List<T> list){
            return list.stream().sorted().collect(Collectors.toList());
        }
    
        //list     
        public static List<Integer> reversed(List<Integer> list){
            return list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        }
    
        //              
        public static List<City> sortForObject(List<City> list){
            return list.stream().sorted(Comparator.comparing(City::getTotal)).collect(Collectors.toList());
        }
    
        //              
        public static List<City> reversedForObject(List<City> list){
            return list.stream().sorted(Comparator.comparing(City::getTotal).reversed()).collect(Collectors.toList());
        }
    
        //              
        public static List<City> sortForObject2(List<City> list){
            return list.stream().sorted(Comparator.comparing(City::getTotal).thenComparing(City::getNum)).collect(Collectors.toList());
        }
    }
    
    

    三、濾過、求和、最値
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class ListUtil_sum {
    
        public static List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);
        public static List<String> strList = Arrays.asList("10","1","6","4");
        public static List<City> cities = null;
        public static Map<String,Integer> cityMap = null;
        static {
            cities = new ArrayList<City>(){
                {
                    add(new City("  ",11));
                    add(new City("  ",55));
                    add(new City("  ",45));
                    add(new City("  ",33));
                }
            };
            cityMap = new HashMap<>();
            cityMap.put("  ",55);
            cityMap.put("  ",11);
        }
        public static void main(String[] args) {
            System.out.println(calculation(list));
            calculation2(cities);
            listToMap(cities);
            mapToList(cityMap);
            stringToList("  、  ");
            joinStringValueByList(cities);
            joinStringValueByList2(strList);
            System.out.println(filter(cities));
        }
    
    
        //            
        ///IntSummaryStatistics{count=4, sum=132, min=11, average=33.000000, max=55}
        public static IntSummaryStatistics calculation(List<Integer> list){
            IntSummaryStatistics stat = list.stream().collect(Collectors.summarizingInt(p -> p));
            System.out.println("max:"+stat.getMax());
            System.out.println("min:"+stat.getMin());
            System.out.println("sum:"+stat.getSum());
            System.out.println("count:"+stat.getCount());
            System.out.println("average:"+stat.getAverage());
            return stat;
        }
    
        //            
        public static void calculation2(List<City> list){
            System.out.println("sum="+ list.stream().mapToInt(City::getTotal).sum());
            System.out.println("max="+ list.stream().mapToInt(City::getTotal).max().getAsInt());
            System.out.println("min="+ list.stream().mapToInt(City::getTotal).min().getAsInt());
            System.out.println("ave="+ list.stream().mapToInt(City::getTotal).average().getAsDouble());
        }
    
        //     List map
        public static void listToMap(List<City> list){
            //  (k1,k2)->k1    ,      key,   key1,  key2
            Map<String,City> map = list.stream().collect(Collectors.toMap(City::getCity,city -> city, (k1, k2) -> k1));
            map.forEach((k,v) -> System.out.println("k=" + k + ",v=" + v));
        }
    
    
        //               
        public static void calculation11(List<City> list){
            Map<String, IntSummaryStatistics> intSummaryStatistics = list.stream().
                    collect(Collectors.groupingBy(i -> i.getCity(), Collectors.summarizingInt(City::getTotal)));
            System.out.println("-4-->" + intSummaryStatistics);
            System.out.println("-5-->" + intSummaryStatistics.get("  ").getSum());
        }
    
        //     map list
        public static void mapToList(Map<String,Integer> map){
            List<City> list = map.entrySet().stream().map(key -> new City(key.getKey(),key.getValue())).collect(Collectors.toList());
            System.out.println(list);
            list.forEach(bean -> System.out.println(bean.getCity() + "," + bean.getTotal()));
        }
    
        //         list
        public static void stringToList(String str){
            //     
            // list = Arrays.asList(str.split(","));
            //    
            List<String> list = Arrays.asList(str.split(",")).stream().map(string -> String.valueOf(string)).collect(Collectors.toList());
            list.forEach(string -> System.out.println(string));
        }
    
        //            
        public static void joinStringValueByList(List<City> list){
            System.out.println(list.stream().map(City::getCity).collect(Collectors.joining(",")));
        }
    
        //            
        public static void joinStringValueByList2(List<String> list){
            //   
            System.out.println(String.join(",", list));
            //   
            System.out.println(list.stream().collect(Collectors.joining(",")));
        }
    
        //       
        public static List<City> filter(List<City> list){
            return list.stream().filter(a -> a.getTotal()>44).collect(Collectors.toList());
        }
    
    }