List<String> list = Arrays.asList("app", "ban", "ora"); //循环输出 for (String s : list) { System.out.println(s); } list.forEach(System.out::println); //排序 Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); Collections.sort(list,(o1, o2) -> o1.compareTo(o2)); //过滤 List<String>list1=new ArrayList<>(); for (String s:list) { if(s.startsWith("a")) list1.add(s); } List<String>list2=list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList()); //映射 List<Integer>list3=new ArrayList<>(); for (String s:list) { list3.add(s.length()); } List<Integer>list4=list.stream().map(String::length).collect(Collectors.toList()); //分组 Map<Integer,List<String>>groups=new HashMap<>(); for (String s:list) { int l=s.length(); if(!groups.containsKey(l)){ groups.put(l,new ArrayList<>()); } groups.get(l).add(s); } Map<Integer,List<String>>groups1=list.stream().collect(Collectors.groupingBy(String::length)); //混用案例 List<String>list5=new ArrayList<>(); for (String s:list) { if(s.startsWith("a")){ list5.add(s.toUpperCase()); } } Collections.sort(list5); List<String>list6=list.stream(). filter(s -> s.startsWith("a")). map(s -> s.toUpperCase()). sorted().collect(Collectors.toList());
标签:startsWith,String,stream,collect,几个,list,有用,new,lambda From: https://www.cnblogs.com/kun1790051360/p/17895454.html