java8 streamAPI常用片段

list转set集合(有序)

List<String> list = Arrays.asList("1", "2", "2", "3", "5", "2");

// LinkedHashSet<String> set = new LinkedHashSet<>(list);
LinkedHashSet<String> set = list.stream()
.collect(Collectors.toCollection(LinkedHashSet::new));

list对象集合转set集合(有序)

List<Object> list = Arrays.asList(new Object(), new Object());
List<String> another = list.stream()
.map(Object::toString)
.collect(Collectors.toCollection(LinkedList::new));

list对象集合转map集合(有序)

List<Object> list = Arrays.asList(new Object(), new Object());
Map<Integer, Object> another = list.stream()
.collect(Collectors.toMap(Object::hashCode, e -> e, (e1, e2) -> e2, LinkedHashMap::new));