标签:Java stream People list System println 操作 public
public class JDK8Test { @Test public void test1() { System.err.println("======================集合排序======================"); List<People> list = getPeopleList(); list.sort((a,b) -> a.getAge()-b.getAge()); System.out.println(list); } @Test public void test2() { System.err.println("======================数据重构======================"); String longStr = "152,135,1561,121,131513,545,4566,1321,1321,151515"; List<Integer> result1 = Arrays.stream(longStr.split(",")).map(s-> Integer.parseInt(s.trim())).collect(Collectors.toList()); System.out.println(result1); List<People> list = getPeopleList(); String result2 = list.stream().map(p->p.getName()).collect(Collectors.joining(",", "{", "}")); System.out.println(result2); } @Test public void test3() { System.err.println("======================求最大值======================"); final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点 List<People> list = getPeopleList(); Optional<People> people = list.stream().max((a,b) -> a.getAge()-b.getAge()); System.out.println(people.get()); int max = list.stream().mapToInt(p->p.getAge()).max().getAsInt(); System.out.println("最大值:"+max); int min = list.stream().mapToInt(p->p.getAge()).min().getAsInt(); System.out.println("最小值:"+min); int sum = list.stream().mapToInt(p->p.getAge()).sum(); System.out.println("求和值:"+sum); double ave = list.stream().mapToDouble(p->p.getAge()).average().getAsDouble(); System.out.println("平均值;"+df.format(ave)); } @Test public void test4() { System.err.println("======================Map-操作======================"); List<People> list = getPeopleList(); List<Integer> collect1 = list.stream().map(People::getId).collect(Collectors.toList()); List<Integer> collect2 = list.stream().map(a-> a.getId()).collect(Collectors.toList()); List<Map<String,Object>> collect3 = list.stream().map(p->{ HashMap<String, Object> map = new HashMap<String,Object>(); map.put("id", p.getId()); map.put("age", p.getAge()); return map; }).collect(Collectors.toList()); Map<String, Integer> collect4 = list.stream().collect(Collectors.toMap(People::getName, People::getAge)); Map<String, Integer> collect5 = list.stream().collect(Collectors.toMap(key->key.getName(), value->{return value.getAge();})); System.out.println(collect5); } @Test public void test5() { System.err.println("======================数据分组======================"); List<People> list = getPeopleList(); Map<Integer, List<People>> list1 = list.stream().collect(Collectors.groupingBy(People::getAge)); Set<Integer> set = list1.keySet(); set.stream().forEach( s -> System.out.println(list1.get(s).toString())); } @Test public void test6() { System.err.println("======================数据过滤======================"); List<People> list = getPeopleList(); List<People> list1 = list.stream().filter(a -> a.getAge() == 18).filter(p -> p.getName().equals("小A")).collect(Collectors.toList()); List<People> list2 = list.stream().filter(a->a.getAge() == 18 && a.getName().equals("小A")).collect(Collectors.toList()); System.out.println(list1); } private List<People> getPeopleList(){ List<People> list = new ArrayList<>(); People p1 = new People(1,"小A",18); People p2 = new People(2,"小B",19); People p3 = new People(3,"小C",20); People p4 = new People(4,"小D",25); People p5 = new People(5,"小E",23); People p6 = new People(6,"小F",15); People p7 = new People(7,"小G",24); People p8 = new People(8,"小H",23); People p9 = new People(9,"小I",18); list = Arrays.asList(p1, p2, p3, p4, p5,p6, p7, p8, p9); return list; } } class People { private Integer id; private String name; private Integer age; public People(Integer id, String name, Integer age) { this.id = id;this.name = name;this.age = age; } @Override public String toString() { return "\n" + "People{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}' + "\n"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
标签:Java,stream,People,list,System,println,操作,public 来源: https://www.cnblogs.com/lbky/p/12461364.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。