标签:stream People list System 特性 Steam println Java8 out
package com.demo;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
@Test
public void test1() {
//实例化Steam
List<People> list = People.getPeopleList();
Stream<People> stream = list.stream();
// 1. 筛选与切片
System.out.println("只打印第一个=============");
stream.limit(1).forEach(System.out::println);
// 流被终止后只能重新实例化
System.out.println("跳过前两个============");
list.stream().skip(2).forEach(System.out::println);
System.out.println("筛选出性别为女的============");
list.stream().filter(e -> e.getSex().equals("女")).forEach(System.out::println);
// 2. 映射,可以理解为会对集合里每个元素都进行的一个操作
System.out.println("打印出每个对象的名字============");
list.stream().map(People::getName).forEach(System.out::println);
System.out.println("在每个对象名字后面加上一个+号并打印出来============");
list.stream().map(People::getName).map(e -> e += "+").forEach(System.out::println);
//3. 排序
// System.out.println("排序会报错,因为People没有实现comparable接口============");
// list.stream().sorted().forEach(System.out::println);
System.out.println("根据年龄排序============");
list.stream().sorted(
(o1, o2) -> {
if (o1.getAge() == o2.getAge()) {
return o1.getName().compareTo(o2.getName());
}
return Integer.compare(o1.getAge(), o2.getAge());
}
).forEach(System.out::println);
//4. 匹配与查找
System.out.println("是否所有的对象都是男性============");
System.out.println(list.stream().allMatch(e -> e.getSex().equals("男")));
System.out.println("是否有一个对象是男性============");
System.out.println(list.stream().anyMatch(e -> e.getSex().equals("男")));
System.out.println("是否没有一个对象是男性============");
System.out.println(list.stream().noneMatch(e -> e.getSex().equals("男")));
System.out.println("打印出第一个对象============");
System.out.println(list.stream().findFirst());
//5.规约
System.out.println("求所有对象年龄的综合============");
Optional<Integer> reduce = list.stream().map(People::getAge).reduce(Integer::sum);
System.out.println(reduce);
System.out.println("把所有性别为男的对象的年龄放到一个新的列表里面============");
List<Integer> collect = list.stream().filter(e -> e.getSex().equals("男")).map(People::getAge).collect(Collectors.toList());
System.out.println(collect);
}
}
class People {
String name;
String sex;
int age;
public People(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public static List<People> getPeopleList() {
List<People> peopleList = new ArrayList<>();
People people = new People("雷军", "男", 45);
People people1 = new People("马云", "男", 45);
People people2 = new People("任正非", "男", 77);
People people3 = new People("董明珠", "女", 44);
peopleList.add(people);
peopleList.add(people1);
peopleList.add(people2);
peopleList.add(people3);
return peopleList;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
标签:stream,People,list,System,特性,Steam,println,Java8,out
来源: https://www.cnblogs.com/yan061/p/16553739.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。