package com.hj.test.stream;
import com.alibaba.fastjson.JSONObject;
import com.hj.test.bean.Person;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DistinctPerson {
public static void main(String[] args) {
List<Person> persons = new ArrayList();
Person person1 = new Person("黄杰1", 19);
Person person2 = new Person("黄杰1", 18);
Person person3 = new Person("黄杰2", 12);
Person person4 = new Person("黄杰3", 12);
Person person5 = new Person("黄杰3", 13);
Person person6 = new Person("黄杰4", 25);
Person person7 = new Person("黄杰4", 17);
persons.add(person1);
persons.add(person2);
persons.add(person3);
persons.add(person4);
persons.add(person5);
persons.add(person6);
persons.add(person7);
List<Person> studentDistinctList = persons.stream().sorted(Comparator.comparing(Person::getAge).reversed())
.collect(Collectors.collectingAndThen
(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(Person::getName))),
ArrayList::new
)
);
System.out.println(JSONObject.toJSON(studentDistinctList).toString());
Random random = new Random();
random.ints().limit(10).sorted().forEach(System.out::println);
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
System.out.println("筛选列表: " + filtered);
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// 奇偶数分组:奇数分一组,偶数分一组
// groupingBy(Function<? super T, ? extends K> classifier) 参数是Function类型,Function返回值可以是要分组的条件,也可以是要分组的字段
// 返回的结果是Map,其中key的数据类型为Function体中计算类型,value是List<T>类型,为分组的结果
Map<Boolean, List<Integer>> result = list.stream().collect(Collectors.groupingBy(item -> item % 2 == 0));
// {false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8, 10]}
System.out.println(result);
// partitioningBy 用于分成两组的情况
Map<Boolean, List<Integer>> twoPartiton = list.stream().collect(Collectors.partitioningBy(item -> item % 2 == 0));
System.out.println(twoPartiton);
User user = new User(100, "zhangsan", 1);
User user2 = new User(200, "lisi", 2);
User user3 = new User(300, "wangwu", 3);
User user4 = new User(400, "fengliu", 1);
User user5 = new User(700, "fengliu1", 2);
User user6 = new User(60, "fengliu2", 3);
List<User> users = Arrays.asList(user, user2, user3, user4,user5, user6);
// 根据某个字段进行分组
Map<Integer, List<User>> userGroup = users.stream().collect(Collectors.groupingBy(item -> item.type));
List<Integer> result1 = users.stream().collect(Collectors.groupingBy(User::getType,
Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(User::getId)), optional->optional.get().getId())))
.values().stream().collect(Collectors.toList());
System.out.println(result1);
List<User> list1 = new ArrayList<>(users.stream().collect(Collectors.toMap(User::getType, Function.identity(), BinaryOperator.maxBy(Comparator.comparingInt(User::getId)))).values());
// users.stream().collect(Collectors.partitioningBy())
System.out.println(JSONObject.toJSON(result1).toString());
System.out.println(JSONObject.toJSON(list1).toString());
/**
* key 为要分组的字段
* value 分组的结果
* {
* 1=[User{id=1, username='zhangsan', type=1}, User{id=4, username='fengliu', type=1}],
* 2=[User{id=2, username='lisi', type=2}],
* 3=[User{id=3, username='wangwu', type=3}]
* }
*/
// System.out.println(JSONObject.toJSON(userGroup).toString());
}
public static class User {
private Integer id;
private String username;
private Integer type;
// Getter & Setter & toString
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public User(Integer id, String username, Integer type) {
this.id = id;
this.username = username;
this.type = type;
}
}
}