Stream的创建方式大体为四种 1.Collection.stream()/Collection.parallelSteam();
List<User> userList = Arrays.asList(
new User("zs", 23, "bj"),
new User("zs1", 23, "bj"),
new User("zs3", 23, "bj"),
new User("zs4", 23, "bj")
);
Stream<User> stream = userList.stream();
Stream<User> userStream = userList.parallelStream();
2.Array.stream()
User[] users = userList.toArray(new User[4]);
Stream<User> stream1 = Arrays.stream(users);
3.Stream.of(数组对象)
Stream<String> a = Stream.of("a", "b", "c");
Stream<User> users1 = Stream.of(users);
4.Stream.iterate()/Stream.generate()
Stream.generate(()->Math.random()).limit(10).forEach(System.out::println);
Stream.generate(()->new Random().nextInt(100)).limit(10).forEach(System.out::println);
Stream API的操作分为1.创建 2.中间操作 3.终止操作 上面演示了如何创建Stream,接下里演示如何对Stream进行中间操作。 中间操作先介绍 筛选与切片类 filter(),limit(),skip(),distinct();看起来是不是很像在写sql语句?
@Test
public void test02(){
List<User> userList = Arrays.asList(
new User("zs", 23, "bj"),
new User("zs1", 33, "bj"),
new User("zs3", 43, "bj"),
new User("zs3", 43, "bj"),
new User("zs3", 43, "bj"),
new User("zs4", 53, "bj")
);
userList.stream()
.filter((x)->{ System.out.println("短路了");
return x.getAge() > 30;
})
.limit(1)
.forEach(System.out::println);
System.out.println("----------");
userList.stream().filter((x)->{
return x.getAge() > 20;
}).distinct().skip(1).forEach(System.out::println);
}
filter()负责过滤Stream中的对象,类似sql中的查询条件 limit()负责决定满足条件的数据中,最后返回几条。写过sql应该能很简单的理解这个概念。 skip()负责决定跳过最前面的几条数据,与limit()相反。 distinct()负责过滤重复的对象,判断重复的条件是对象hashcode()和equals()方法,所以记得重写对象的这两个方法。
接下来介绍Stream中间操作的映射方法,map()、flatMap()、sorted()
map()负责通过重写的方法将Stream中的每一个元素进行转换,对应的方法会作用于每一个被筛选出来的元素。
@Test
public void test05(){
List<String> stringList = Arrays.asList("aaa","bbb","ccc","ddd","eee");
stringList.stream().map((x)->x.toUpperCase()).forEach(System.out::println);
System.out.println("----------");
List<User> userList = Arrays.asList(
new User("zs", 23, "bj"),
new User("zs1", 33, "bj"),
new User("zs3", 43, "bj"),
new User("zs4", 53, "bj")
);
userList.stream().map(User::getName).forEach(System.out::println);
}
flatMap()基本与map()一样,不过flatMap()会将Stream中的所有元素全部整合到一个流中。
@Test
public void test05(){
List<String> stringList = Arrays.asList("aaa","bbb","ccc","ddd","eee");
stringList.stream().map((x)->x.toUpperCase()).forEach(System.out::println);
System.out.println("----------");
List<User> userList = Arrays.asList(
new User("zs", 23, "bj"),
new User("zs1", 33, "bj"),
new User("zs3", 43, "bj"),
new User("zs4", 53, "bj")
);
userList.stream().map(User::getName).forEach(System.out::println);
System.out.println("-----------");
List<String> stringList2 = Arrays.asList("aaa","bbb","ccc","ddd","eee");
stringList2.stream().map(
String2CharacterUtils::string2Character
).forEach(((sm)->sm.forEach(System.out::println)));
System.out.println("-----------000----------");
stringList2.stream().flatMap(String2CharacterUtils::string2Character).forEach(System.out::println);
}
sorted()负责将元素进行排序,不带参数则按元素的compareTo()进行排序,带参数,则按编写的比较器规则进行排序。
@Test
public void sorted(){
List<User> userList = Arrays.asList(
new User("zs", 24, "bj"),
new User("zs1", 33, "bj"),
new User("zs3", 23, "bj"),
new User("zs32", 23, "bj"),
new User("zs31", 23, "bj"),
new User("zs4", 13, "bj")
);
userList.stream().sorted().forEach(System.out::println);
System.out.println("------------");
userList.stream().sorted(
(x,y)->{
if(x.getName().equals(y.getName())){
return x.getAge() - y.getAge();
}
return x.getName().compareTo(y.getName());
}
).forEach(System.out::println);
}
常用终止操作:
@Test
public void test06(){
List<User> userList = Arrays.asList(
new User("zs", 23, "bj", User.Status.FREE),
new User("zs1", 33, "bj", User.Status.BUSY),
new User("zs3", 43, "bj",User.Status.VOCATION),
new User("zs4", 53, "bj",User.Status.FREE)
);
boolean b = userList.stream().allMatch(x -> x.getStatus().equals(User.Status.FREE));
System.out.println("allMatch :" + b);
boolean b1 = userList.stream().anyMatch(x -> x.getStatus().equals(User.Status.FREE));
System.out.println("anyMatch:" + b1);
System.out.println("----------");
boolean b2 = userList.stream().noneMatch(x -> x.getStatus().equals(User.Status.VOCATION));
System.out.println(b2);
Optional<User> first = userList.stream().findFirst();
System.out.println(first.get());
Optional<User> any = userList.parallelStream().findAny();
System.out.println(any.get());
long count = userList.stream().count();
System.out.println(count);
Optional<User> max = userList.stream().max((x, y) -> x.getAge() - y.getAge());
System.out.println(max.get());
Optional<User> min = userList.stream().min((x, y) -> x.getAge() - y.getAge());
System.out.println(min.get().getAge());
Optional<Integer> min1 = userList.stream().map(User::getAge).min(Integer::compareTo);
System.out.println(min1.get());
}
@Test
public void reduce(){
List<Integer> integerList = Arrays.asList(1,2,3,4,5,6,7,8,9);
Integer reduce = integerList.stream().reduce(0, (x, y) -> x + y);
System.out.println(reduce);
List<User> userList = Arrays.asList(
new User("zs", 23, "bj", User.Status.FREE),
new User("zs1", 33, "bj", User.Status.BUSY),
new User("zs3", 43, "bj",User.Status.VOCATION),
new User("zs4", 53, "bj",User.Status.FREE)
);
Optional totalAge = userList.stream().map(User::getAge).reduce(Integer::sum);
System.out.println(totalAge.get());
}
收集操作 collect()
@Test
public void collect(){
List<User> userList = Arrays.asList(
new User("zs", 23, "bj", User.Status.FREE),
new User("zs1", 33, "bj", User.Status.BUSY),
new User("zs3", 43, "bj",User.Status.VOCATION),
new User("zs4", 53, "bj",User.Status.FREE),
new User("zs4", 53, "bj",User.Status.FREE)
);
List<String> collect = userList.stream().map(User::getName).collect(Collectors.toList());
collect.forEach(System.out::println);
System.out.println("-------------");
Set<String> collect1 = userList.stream().map(User::getName).collect(Collectors.toSet());
collect1.forEach(System.out::println);
System.out.println("-----------");
HashSet<String> hs = userList.stream().map(User::getName).collect(Collectors.toCollection(HashSet::new));
hs.forEach(System.out::println);
System.out.println("--------------");
Long collect2 = userList.stream().collect(Collectors.counting());
System.out.println(collect2);
System.out.println("------");
Double collect3 = userList.stream().collect(Collectors.averagingDouble(User::getAge));
System.out.println(collect3);
System.out.println("---------");
Integer collect4 = userList.stream().collect(Collectors.summingInt(User::getAge));
System.out.println(collect4);
Map<User.Status, List<User>> collect5 = userList.stream().collect(Collectors.groupingBy(User::getStatus));
System.out.println(collect5);
Map<User.Status, Map<Integer, List<User>>> collect6 = userList.stream().collect(Collectors.groupingBy(User::getStatus, Collectors.groupingBy(User::getAge)));
System.out.println(collect6);
Map<Boolean, List<User>> collect7 = userList.stream().collect(Collectors.partitioningBy((x) -> x.getAge() > 30));
System.out.println(collect7);
DoubleSummaryStatistics collect8 = userList.stream().collect(Collectors.summarizingDouble(User::getAge));
System.out.println(collect8);
String name = userList.stream().map(User::getName)
.collect(Collectors.joining(","));
System.out.println(name);
}
|