public class Demo {
private static final String STRINGWORD
= "好好,学习,天天,向上,积极,乐观,善良,勇敢,博爱,自由";
public static void main(String[] args) {
Student student张三 = new Student();
student张三.setName("张三");
student张三.setAge(19);
student张三.setClassRoom("高三5班");
student张三.setSay("向上");
Student student张三1 = new Student();
student张三1.setName("张三");
student张三1.setAge(16);
student张三1.setClassRoom("高一5班");
student张三1.setSay("自由");
Student student李四 = new Student();
student李四.setName("李四");
student李四.setAge(18);
student李四.setClassRoom("高二7班");
student李四.setSay("天天");
Student student王五 = new Student();
student王五.setName("王五");
student王五.setAge(16);
student王五.setClassRoom("高一2班");
student王五.setSay("乐观");
Student student赵六 = new Student();
student赵六.setName("赵六");
student赵六.setAge(15);
student赵六.setClassRoom("高一1班");
student赵六.setSay("学习");
Student student刘七 = new Student();
student刘七.setName("刘七");
student刘七.setAge(18);
student刘七.setClassRoom("高二5班");
student刘七.setSay("博爱");
Student student刘七1 = new Student();
student刘七1.setName("刘七");
student刘七1.setAge(19);
student刘七1.setClassRoom("高三9班");
student刘七1.setSay("好好");
Student student周八 = new Student();
student周八.setName("周八");
student周八.setAge(17);
student周八.setClassRoom("高二3班");
student周八.setSay("勇敢");
Student student钱九 = new Student();
student钱九.setName("钱九");
student钱九.setAge(16);
student钱九.setClassRoom("高一1班");
student钱九.setSay("善良");
Student student孙十 = new Student();
student孙十.setName("孙十");
student孙十.setAge(20);
student孙十.setClassRoom("高三10班");
student孙十.setSay("积极");
List<Student> list = new ArrayList<>();
list.add(student张三);
list.add(student张三1);
list.add(student李四);
list.add(student王五);
list.add(student赵六);
list.add(student刘七);
list.add(student刘七1);
list.add(student周八);
list.add(student钱九);
list.add(student孙十);
//方法使用灵活多变,可以自由组合。下列示例仅供参考。
//1、遍历
list.stream().forEach(student -> {
System.out.println(JsonUtil.serialize(student));
});
//2、过滤 按条件对属性进性过滤
List<Student> filter = list.stream().
filter(pojo -> pojo.getClassRoom().contains("三")).
filter(pojo -> pojo.getClassRoom().contains("5")).
collect(Collectors.toList());
System.out.println("2、过滤 按条件对属性进性过滤:" + JsonUtil.serialize(filter));
//3、获取某个属性集合
List<String> shuXing = list.stream().map(pojo -> pojo.getName())
.collect(Collectors.toList());
System.out.println("3、获取某个属性集合:" + JsonUtil.serialize(shuXing));
//4、分隔 对某个属性分隔
String joining = list.stream().map(pojo -> pojo.getName())
.collect(Collectors.joining("---"));
System.out.println("4、分隔 对某个属性分隔:" + joining);
//5、去重
List<Student> distinct = list.stream().distinct()
.collect(Collectors.toList());
System.out.println("5、去重:" + JsonUtil.serialize(distinct));
//6、去重 根据某个属性进性去重
List<String> distinctList = list.stream().map(Student::getName)
.distinct().collect(Collectors.toList());
System.out.println("6、去重 根据某个属性进性去重:" + JsonUtil.serialize(distinctList));
//7、set list转成set
Set<Student> set = list.stream().collect(Collectors.toSet());
System.out.println("7、set list转成set:" + JsonUtil.serialize(set));
//8、排序 根据int类型属性排序
List<Student> sorted = list.stream().
sorted(Comparator.comparing(Student::getAge))
.collect(Collectors.toList());
System.out.println("8、排序 根据某个属性排序:" + JsonUtil.serialize(sorted));
//9、获取集合大小
long count = list.stream().count();
System.out.println(count);
//10、根据字符串类型属性对对象排序
list.sort(((o1, o2) -> o1.getClassRoom().compareTo(o2.classRoom)));
System.out.println("10、根据字符串类型属性对对象排序:" + JsonUtil.serialize(list));
//11、根据指定字符串对属性排序,获取字符串集合
List<String> stringList = list.stream().map(Student::getSay)
.collect(Collectors.toList());
stringList.sort(((o1, o2) -> STRINGWORD.indexOf(o1) - STRINGWORD.indexOf(o2)));
System.out.println("11、根据指定字符串对属性排序,获取字符串集合:" + JsonUtil.serialize(stringList));
//12、根据指定字符串对属性排序,获取对象集合
list.sort(((o1, o2) -> STRINGWORD.indexOf(o1.getSay()) - STRINGWORD.indexOf(o2.getSay())));
System.out.println("12、根据指定字符串对属性排序,获取对象集合:" + JsonUtil.serialize(list));
}
static class Student {
private String name; //姓名
private int age; //年龄
private String classRoom; //班级
private String say; //别号
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassRoom() {
return classRoom;
}
public void setClassRoom(String classRoom) {
this.classRoom = classRoom;
}
public String getSay() {
return say;
}
public void setSay(String say) {
this.say = say;
}
}
}
list遍历、过滤、去重、排序等使用jk8新特性进性编码
于 2023-06-27 11:11:32 首次发布