package com.fh; public class Student { private String age; private Integer sex; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } }
package com.fh; public class Demo { private String name; private String age; private Integer sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Demo(String age, Integer sex) { super(); this.age = age; this.sex = sex; } }
package com.fh; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Test { @org.junit.Test public void test() { List<Student> list = new ArrayList<>(); Student student1 = new Student();student1.setAge("12");student1.setSex(0); Student student2 = new Student();student2.setAge("13");student2.setSex(2); Student student3 = new Student();student3.setAge("11");student3.setSex(1); Student student4 = new Student();student4.setAge("18");student4.setSex(1); Student student5 = new Student();student5.setAge("18");student5.setSex(0); Student student6 = new Student();student6.setAge("18");student6.setSex(2); Student student7 = new Student();student7.setAge("18");student7.setSex(2); list.add(student1); list.add(student2); list.add(student3); list.add(student4); list.add(student5); list.add(student6); list.add(student7); List<Demo> demos = new ArrayList<Demo>(); demos = printData(demos, list); // printSexequal0(demos); // filterAge(demos); // sort(demos); // pour(demos); // pour2(demos); // moreSort(demos); // morePour(demos); groupByAge(demos); } /** * 数据打印 * @param demos * @param list */ public List<Demo> printData(List<Demo> demos ,List<Student> list) { demos = list.stream().map(student -> new Demo(student.getAge(),student.getSex())).collect(Collectors.toList()); /*demos.forEach(demo ->{ System.out.println(demo.getAge()); });*/ return demos; } /** * 打印性别为0的数据 * @param demos */ public void printSexequal0(List<Demo> demos) { List<Demo> collect = demos.stream().filter(demo -> demo.getSex() == 0).distinct().collect(Collectors.toList()); collect.forEach(item ->{ System.out.println(" "+item.getAge()+":"+item.getSex()); }); } /** * 过滤年龄大于12的信息 * @param demos */ public void filterAge(List<Demo> demos) { List<Demo> collect = demos.stream().filter(demo -> Integer.valueOf(demo.getAge())>12).collect(Collectors.toList()); collect.forEach(demo ->{ System.out.println(demo.getAge()+":"+demo.getSex()); }); } /** * 数据排序 * @param demos */ public void sort(List<Demo> demos) { List<Demo> collect = demos.stream().sorted((s1,s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList()); collect.forEach(demo -> { System.out.println(demo.getAge()); }); } /** * 倒叙 * @param demos */ public void pour(List<Demo> demos) { ArrayList<Demo> demoArray = (ArrayList<Demo>)demos; demoArray.sort(Comparator.comparing(Demo::getAge).reversed()); demoArray.forEach(demo -> { System.out.println(demo.getAge()); }); } /** * 倒叙2 * @param demos */ public void pour2(List<Demo> demos) { ArrayList<Demo> demoArray = (ArrayList<Demo>)demos; Comparator<Demo> comparator = (h1,h2) -> h1.getAge().compareTo(h2.getAge()); demoArray.sort(comparator.reversed()); demoArray.forEach(demo -> { System.out.println(demo.getAge()); }); } /** * 多条件排序--正序 * @param demos */ public void moreSort(List<Demo> demos) { demos.sort(Comparator.comparing(Demo::getSex).thenComparing(Demo::getAge)); demos.forEach(demo ->{ System.out.println(demo.getSex()+":"+demo.getAge()); }); } /** * 多条件倒叙 * @param demos */ public void morePour(List<Demo> demos) { demos.sort(Comparator.comparing(Demo::getSex).reversed().thenComparing(Demo::getAge)); demos.forEach(demo ->{ System.out.println(demo.getSex()+":"+demo.getAge()); }); } /** * 分组 * @param demos */ public void groupByAge(List<Demo> demos) { Map<String, List<Demo>> collect = demos.stream().collect(Collectors.groupingBy(Demo::getAge)); collect.forEach((key,value)->{ value.forEach(demo ->{ System.out.println(key+":"+demo.getSex()); }); }); } }
代码是网上找的,自己写了一遍,记录一下
@SuppressWarnings("unused") public void filterTest(List<Demo> demos) { String age = "12"; String sex = "0"; List<Demo> collect = demos.stream().filter(demo -> { if(age!=null) { return Integer.parseInt(demo.getAge())>Integer.parseInt(age); }else { return true; } }).filter(demo ->{ if(sex!=null) { return demo.getSex() ==Integer.parseInt(sex); }else { return true; } }).collect(Collectors.toList()); collect.forEach(demo ->{ System.out.println(demo.getAge()+":"+demo.getSex()); }); }
多条件排序和去重复测试
@Test public void testMoreSort() { List<Entrys> list = new ArrayList<>(); Entrys item = new Entrys(); item.setOne(1); item.setTwo(1); Entrys item1 = new Entrys(); item1.setOne(1); item1.setTwo(2); Entrys item2 = new Entrys(); item2.setOne(1); item2.setTwo(3); Entrys item3 = new Entrys(); item3.setOne(1); item3.setTwo(4); Entrys item4 = new Entrys(); item4.setOne(2); item4.setTwo(1); Entrys item5 = new Entrys(); item5.setOne(2); item5.setTwo(2); Entrys item6 = new Entrys(); item6.setOne(2); item6.setTwo(3); Entrys item7 = new Entrys(); item7.setOne(2); item7.setTwo(4); Entrys item8 = new Entrys(); item8.setOne(3); item8.setTwo(1); Entrys item9 = new Entrys(); item9.setOne(3); item9.setTwo(2); Entrys item10 = new Entrys(); item10.setOne(3); item10.setTwo(3); Entrys item11 = new Entrys(); item11.setOne(4); item11.setTwo(4); list.add(item1); list.add(item2); list.add(item3); list.add(item4); list.add(item5); list.add(item6); list.add(item7); list.add(item8); list.add(item9); list.add(item10); list.add(item11); list.sort(Comparator.comparing(Entrys::getOne).thenComparing(Entrys::getTwo)); list.forEach(demo ->{ System.out.println(demo.getOne()+":"+demo.getTwo()); }); } @Test public void testDic() { List<Models> list = new ArrayList<>(); Models model = new Models(); model.setName("111"); model.setAge("238"); model.setSemi_fid("xxxcqqccbbb"); model.setTid("111111"); Models model1 = new Models(); model1.setName("11122312"); model1.setAge("237"); model1.setSemi_fid("xxxccqcbbb"); model1.setTid("11111133"); Models model2 = new Models(); model2.setName("111333123"); model2.setAge("236"); model2.setSemi_fid("xxxccqcbbb"); model2.setTid("111111"); Models model3 = new Models(); model3.setName("1113213"); model3.setAge("236"); model3.setSemi_fid("xxxccqqcbbb"); model3.setTid("111111"); Models model4 = new Models(); model4.setName("feng"); model4.setAge("25"); model4.setSemi_fid("xxxcccqqqbbb"); model4.setTid("111111"); Models model5 = new Models(); model5.setName("hao"); model5.setAge("23"); model5.setSemi_fid("xxxcccqqqbbb"); model5.setTid("111111"); list.add(model); list.add(model1); list.add(model2); list.add(model3); list.add(model4); list.add(model5);
//顺序不变 List<Models> collect = list.stream().distinct().collect(Collectors.toList()); collect.forEach(demo ->{ System.out.println(demo.getName()+":"+demo.getAge()+":"+demo.getSemi_fid()+":"+demo.getTid()); }); //顺序发生变化 // List<ClassEntity> distinctClass = classEntities.stream(). // collect( // Collectors.collectingAndThen( // Collectors.toCollection( // () -> new TreeSet<>( // Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade() // ))), ArrayList::new)); /* ArrayList<Models> collect = list.stream(). collect( Collectors.collectingAndThen( Collectors.toCollection( () ->new TreeSet<Models>( Comparator.comparing(demo -> demo.getSemi_fid()+";"+demo.getTid()) ) ), ArrayList::new) ); collect.forEach(demo ->{ System.out.println(demo.getName()+":"+demo.getAge()+":"+demo.getSemi_fid()+":"+demo.getTid()); });*/ } } class Entrys{ private int one; private int two; public int getOne() { return one; } public void setOne(int one) { this.one = one; } public int getTwo() { return two; } public void setTwo(int two) { this.two = two; }
package com.voole.forerunner; public class Models { private String name; private String semi_fid; private String tid; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSemi_fid() { return semi_fid; } public void setSemi_fid(String semi_fid) { this.semi_fid = semi_fid; } public String getTid() { return tid; } public void setTid(String tid) { this.tid = tid; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public int hashCode() { return (this.getSemi_fid()+this.getTid()).hashCode(); } @Override public boolean equals(Object obj) { if(obj instanceof Models) { Models item = (Models)obj; if(this.getSemi_fid().equals(item.getSemi_fid()) && this.getTid().equals(item.getTid())) { return true; }else { return false; } }else { return false; } } }