java stream复杂去重复和条件排序代码实现

  • Post author:
  • Post category:java


复杂去重复可以通过distinctByKey函数来实现,而复杂条件排序可以通过内嵌函数来实现,示例代码如下:

// Java program is to demonstrate the example of
// reverse(List l) method of Collections
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

class stuInfo {
    public stuInfo(long id, double score) {
		super();
        this.id = id;
		this.score = score;
	}
	long id;
    double score;
    long getId() {
		return id;
	}
	double getScore() {
		return score;
	}
}

public class SortOfCollections {
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new HashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
    public static void main(String args[]) {
        Set<Long> showSet = new HashSet<>();
        showSet.add((long) 1);
        showSet.add((long) 2);
        showSet.add((long) 3);
        for(long id : showSet) {
            System.out.println("id is : " + id);
        }
        System.out.println("*******************");
        HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("zgl");
        list1.add("zy");
        map.put("sgyy", list1);
        System.out.println("size is : " + map.get("sgyy").size());
        ArrayList<String> tmpList = map.get("sgyy");
        System.out.println("size is : " + tmpList.size());
        tmpList.add("zf");
        System.out.println("size is : " + tmpList.size());
        List < stuInfo > arr_l = new ArrayList < stuInfo > ();
        double []scores = new double[] {0.2, 0.35, 1.32, 0.93, 2.39, 0.23, 0.90};
        long []ids = new long[] {0, 1, 2, 3, 2, 4, 2}; 
        for (int i = 0; i < scores.length; i++) {
		stuInfo tmpInfo = new stuInfo(ids[i], scores[i]);
		arr_l.add(tmpInfo);
        }
        for (stuInfo tmpInfo : arr_l) {
       		System.out.println("Collections.reverse(arr_l): " + tmpInfo.id + " " + tmpInfo.score);
	}
	System.out.println("**********************************");
        arr_l = arr_l.stream().sorted((e1, e2) -> Double.compare(e2.score, e1.score)).collect(Collectors.toList());
        arr_l = arr_l.stream().filter(distinctByKey(e -> e.getId())).collect(Collectors.toList());
        for (stuInfo tmpInfo : arr_l) {
       		System.out.println("Collections.reverse(arr_l): " + tmpInfo.getId() + " " + tmpInfo.score);
	}
    arr_l = arr_l.stream().sorted((e1, e2) -> {
        double score1 = e1.getScore();
        double score2 = e2.getScore();
        if (e1.getId() % 2 == 0) {
            score1 = score1 * 2.0;
        }
        if (e2.getId() % 2 == 0) {
            score2 = score2 * 2.0;
        }
        return Double.compare(score1, score2);
     }).collect(Collectors.toList());
     System.out.println("**********************************");
     for (stuInfo tmpInfo : arr_l) {
         double score =  tmpInfo.score;
         if (tmpInfo.getId() % 2 == 0) {
             score = score * 2.0;
         }
        System.out.println("Collections.reverse(arr_l): " + tmpInfo.getId() + " " + score);
    }
    }
}

结果如下:

id is : 1
id is : 2
id is : 3
*******************
size is : 2
size is : 2
size is : 3
Collections.reverse(arr_l): 0 0.2
Collections.reverse(arr_l): 1 0.35
Collections.reverse(arr_l): 2 1.32
Collections.reverse(arr_l): 3 0.93
Collections.reverse(arr_l): 2 2.39
Collections.reverse(arr_l): 4 0.23
Collections.reverse(arr_l): 2 0.9
**********************************
Collections.reverse(arr_l): 2 2.39
Collections.reverse(arr_l): 3 0.93
Collections.reverse(arr_l): 1 0.35
Collections.reverse(arr_l): 4 0.23
Collections.reverse(arr_l): 0 0.2
**********************************
Collections.reverse(arr_l): 1 0.35
Collections.reverse(arr_l): 0 0.4
Collections.reverse(arr_l): 4 0.46
Collections.reverse(arr_l): 3 0.93
Collections.reverse(arr_l): 2 4.78



版权声明:本文为fangfanglovezhou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。