1.去重复,返回List集合
public class TestStream {
@Test
void test01() {
People p = new People(1, "北京");
People p1 = new People(1, "北京");
People p2 = new People(1, "上海");
People p3 = new People(1, "天津");
People p4 = new People(2, "武汉");
People p5 = new People(2, "天津");
People p6 = new People(2, "武汉");
People p7 = new People(2, "合肥");
List<People> people = Arrays.asList(p, p1, p2, p3, p4, p5, p6, p7);
// 去重
ArrayList<People> result = people.stream().collect(collectingAndThen(
toCollection(
() -> new TreeSet<>(Comparator.comparing(l->l.getId()+"-"+l.getAddress()))
), ArrayList::new
)
);
result.forEach(s-> System.out.println(s));
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class People {
private int id;
private String address;
}