private static final List<User> users;
static {
users = new ArrayList<>();
users.add(new User("1", "zs", "山东", "111", 20, 200));
users.add(new User("2", "ls", "山东", "222", 30, 700));
users.add(new User("3", "ww", "河南", "333", 40, 2000));
users.add(new User("4", "zl", "广州", "444", 50, 2700));
users.add(new User("5", "lq", "广州", "555", 60, 3500));
users.add(new User("6", "xb", "山东", "222", 70, 4200));
users.add(new User("7", "lj", "北京", "666", 80, 1100));
}
- StreamApi中间操作 filter distinct skip limit map sorted peed
@Test
public void test1() throws Exception {
users.stream().filter(u -> u.getSalary() > 1500)
.map(User::getSalary)
.sorted((s1, s2) -> s2 - s1)
.distinct()
.skip(1)
.limit(2)
.forEach(System.out::println);
}
- 终止操作 forEach allMatch noneMatch anyMatch findFirst findAny count max min
@Test
public void test2() throws Exception {
boolean allMatch = users.stream().allMatch(u -> "山东".equals(u.getAddress()));
System.out.println("allMatch = " + allMatch);
boolean noneMatch = users.stream().noneMatch(u -> u.getSalary() > 10000);
System.out.println("noneMatch = " + noneMatch);
boolean anyMatch = users.stream().anyMatch(u -> "河南".equals(u.getAddress()));
System.out.println("anyMatch = " + anyMatch);
User maxSal = users.stream().sorted((u1, u2) -> u2.getSalary() - u1.getSalary()).findFirst().get();
System.out.println("maxSal = " + maxSal);
User userAny = users.stream().findAny().get();
System.out.println("userAny = " + userAny);
long count = users.stream().filter(u -> u.getSalary() > 2000).count();
System.out.println("count = " + count);
int maxSalInt = users.stream().mapToInt(User::getSalary).max().getAsInt();
int minAge = users.stream().mapToInt(User::getAge).min().getAsInt();
System.out.println("minAge = " + minAge);
}
- 分组与分区 partitioninng groupby
@Test
public void test3() throws Exception {
Map<Boolean, List<User>> collect = users.stream().collect(Collectors.partitioningBy(u -> u.getSalary() > 3000));
System.out.println("大于3000的人员:" + collect.get(true));
System.out.println("小于3000的人员:" + collect.get(false));
Map<String, List<User>> listMap = users.stream().collect(Collectors.groupingBy(User::getAddress));
listMap.forEach((key, value) -> {
System.out.println(key + value);
});
Map<String, List<User>> ageMap = users.stream().collect(Collectors.groupingBy(u -> {
Integer age = u.getAge();
if ((age > 0) && (age <= 20)) {
return "0-20";
} else if ((age > 20) && (age <= 60)) {
return "20-60";
} else {
return "60以上";
}
}));
Map<String, Long> data = new HashMap<>();
ageMap.forEach((key, value) -> {
long count = value.stream().count();
data.put(key, count);
});
data.keySet().stream().forEach(k -> {
System.out.println(k + " : " + data.get(k));
});
}
@Test
public void test4() throws Exception {
users.stream().collect(Collectors.toList()).forEach(System.out::println);
users.stream().collect(Collectors.toSet()).forEach(System.out::println);
users.stream().collect(Collectors.toMap( (User::getId),( (u) -> u ) )).forEach(
(k,v) -> {
System.out.println(k + v);
}
);
}