1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| String concat = Stream.of("A", "B", "C", "D").reduce("", String::concat);
double minValue = Stream.of(-1.5, 1.0, -3.0, -2.0).reduce(Double.MAX_VALUE, Double::min);
int sumValue = Stream.of(1, 2, 3, 4).reduce(0, Integer::sum);
sumValue = Stream.of(1, 2, 3, 4).reduce(Integer::sum).get();
concat = Stream.of("a", "B", "c", "D", "e", "F"). filter(x -> x.compareTo("Z") > 0). reduce("", String::concat);
int[] ints = {1,2,3,4,5}; int sum = Arrays.stream(ints).sum(); ArrayList<User> users = new ArrayList<>(); users.add(new User("ste",12,"男")); users.add(new User("jack",14,"男")); users.add(new User("tom",16,"男"));
double avg = users.stream() .mapToInt(User::getAge) .average() .getAsDouble();
ArrayList<User> users = new ArrayList<>(); users.add(new User("ste",12)); users.add(new User("jack",14)); users.add(new User("tom",16));
User maxuser = users.stream() .max(Comparator.comparingInt(User::getAge)) .get();
User minuser = users.stream() .min(Comparator.comparingInt(User::getAge)) .get();
ArrayList<User> users = new ArrayList<>(); users.add(new User("ste",12)); users.add(new User("jack",14)); users.add(new User("tom",16)); long count = users.stream().count();
|