list转map
@Data
public class Person {
private String id;
private String name;
private int age;
public Person(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
public class StreamTest {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
Person person1 = new Person("1", "张三", 10);
Person person2 = new Person("2", "李四", 20);
Person person3 = new Person("3", "王五", 10);
people.add(person1);
people.add(person2);
people.add(person3);
// list 转 map
Map<String, Person> collect = people.stream().collect(Collectors.toMap(Person::getId, k -> k));
// System.out::println 可以看作 lambda表达式 e -> System.out.println(e) 的缩写形式。
collect.values().forEach(System.out::println);
}
}
过滤
public class StreamTest {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
Person person1 = new Person("1", "张三", 10);
Person person2 = new Person("2", "李四", 20);
Person person3 = new Person("3", "王五", 10);
people.add(person1);
people.add(person2);
people.add(person3);
// list 过滤年龄==10的
List<Person> collect = people.stream().filter(p -> p.getAge() == 10).collect(Collectors.toList());
collect.forEach(System.out::println);
}
}
分组操作
Map<String, List<DoExtchCfg>> channelMap = doExtchCfgs.stream().collect(Collectors.groupingBy(DoExtchCfg::getExtChannelCode));