本博客数据来源:示例数据java
public static void main(String[] args) { List<Dept> deptList = Arrays.asList(new Dept(1, "OPERATION", "BOSTON"), new Dept(3, "SALES", "CHICAGO"), new Dept(4, "ACCOUNTING", "NEW YORK"), new Dept(2, "RESEARCH", "DALLAS")); deptList.forEach(item -> System.out.println(item)); deptList.forEach(dept-> dept.getEmps().forEach(System.out::println)); //遍历双重循环 }
public class LambdaTest{ List<Dept> deptList; @Before public void before() { deptList = Arrays.asList(new Dept(1, "OPERATION", "BOSTON"), new Dept(2, "RESEARCH", "TOKYO"), new Dept(3, "SALES", "CHICAGO"), new Dept(4, "ACCOUNTING", "NEW YORK"), new Dept(2, "RESEARCH", "DALLAS")); } @Test public void fun1() { //基本排序 deptList.sort((dept1, dept2) -> dept1.getDname().compareTo(dept2.getDname())); deptList.forEach(item -> System.out.println(item)); } @Test public void fun2() { //排序完逆序 Comparator<Dept> comparator = (dept1, dept2) -> dept1.getDname().compareTo(dept2.getDname()); deptList.sort(comparator.reversed()); deptList.forEach(item -> System.out.println(item)); } public static int compareByDnameThenLoc(Dept dept1, Dept dept2) { if (dept1.getDname().equals(dept2.getDname())) { return dept1.getLoc().compareTo(dept2.getLoc()); } else { return dept1.getDname().compareTo(dept2.getDname()); } } @Test public void fun3() {//使用静态方法的引用来排序 deptList.sort(LambdaTest::compareByDnameThenLoc); deptList.forEach(item -> System.out.println(item)); } @Test public void fun4() {//多条件排序 deptList.sort((dept1, dept2) -> { if (dept1.getDname().equals(dept2.getDname())) { return dept1.getLoc().compareTo(dept2.getLoc()); } else { return dept1.getDname().compareTo(dept2.getDname()); } }); deptList.forEach(item -> System.out.println(item)); } @Test public void fun5() {//提取Comparator排序 Collections.sort(deptList, Comparator.comparing(Dept::getDname)); deptList.forEach(item -> System.out.println(item)); } @Test public void fun6() {//多条件组合排序 Collections.sort(deptList, Comparator.comparing(Dept::getDname) .thenComparing(Dept::getLoc)); deptList.forEach(item -> System.out.println(item)); } }
public static void main(String[] args) throws IOException { List<String> list = Arrays.asList("aa","bb","cc","aa","dd"); list.stream().distinct().collect(Collectors.toList()) .forEach(System.out::println); }
public static void main(String[] args) { List<String> list = Arrays.asList("zhangsan", "lisi", "wanger", "mzai"); list.stream().map(item -> item+" : "+item.toUpperCase()).forEach(System.out::println); }
public static void main(String[] args) throws IOException { Dept accounting = new Dept((byte)10, "ACCOUNTING","NewYork"); Dept research = new Dept((byte)20, "RESEARCH","Dallas"); Dept sales = new Dept((byte)30, "SALES","Chicago"); Dept operations = new Dept((byte)40, "OPERATIONS","Boston"); List<Dept> list = new ArrayList<>(); list.add(accounting); list.add(research); list.add(sales); list.add(operations); List<String> dnameList = list.stream().map(item->item.getDname()) .collect(Collectors.toList()); dnameList.forEach(System.out::println); }
public static void main(String[] args) { List<String> list = Arrays.asList("zhangsan", "lisi", "wanger", "mzai"); List<String> res = list.stream().filter(item -> item.length() > 5) .collect(Collectors.toList()); res.forEach(System.out::println); List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5); numberList.stream().filter(i -> i % 2 == 0).forEach(System.out::println); }
实现一web
public static void main(String[] args) { List<String> list = Arrays.asList("USA", "FRANCE", "GERMANY", "ITALY", "CANADA"); String res = list.stream().map(x -> x.toLowerCase()).collect(Collectors.joining("#")); System.out.println(res); }
实现二:数组
public class LambdaTest { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); filter1(numbers, x -> (int) x % 2 == 0); // 2 4 List<String> words = Arrays.asList("a", "ab", "abc", "abcd", "abcde"); filter2(words, x -> ((String) x).length() > 2); //abc abcd abcde } public static void filter1(List list, Predicate condition) { list.forEach(x -> { if (condition.test(x)) { System.out.print(x + " "); } }); } public static void filter2(List list, Predicate condition) { //更佳的方式 list.stream().filter(x -> condition.test(x)).forEach(x -> { System.out.print(x + " "); }); } }
实现三:app
public static void main(String[] args) throws IOException { Dept accounting = new Dept((byte)10, "ACCOUNTING","NewYork"); Dept research = new Dept((byte)10, "RESEARCH","Dallas"); Dept sales = new Dept((byte)10, "SALES","Chicago"); Dept operations = new Dept((byte)10, "OPERATIONS","Boston"); List<Dept> list = new ArrayList<>(); list.add(accounting); list.add(research); list.add(sales); list.add(operations); List<Emp> empList = list.stream().filter(item->item.getDname().contains("E")) .map(item->{ Emp emp = new Emp(); emp.setDeptno(item.getDeptno()); return emp; }).collect(Collectors.toList()); empList.forEach(System.out::println); }
实现四:ide
public static void main(String[] args) throws IOException { Dept accounting = new Dept((byte)10, "ACCOUNTING","NewYork"); Dept research = new Dept((byte)20, "RESEARCH","Dallas"); Dept sales = new Dept((byte)30, "SALES","Chicago"); Dept operations = new Dept((byte)40, "OPERATIONS","Boston"); Map<Integer, Dept> map = new HashMap<>(); map.put(10, accounting); map.put(20, research); map.put(30, sales); map.put(40, operations); List<Dept> empList = map.entrySet().stream().map(item->{ Dept temp = item.getValue(); temp.setDname(temp.getDname().toLowerCase()); return temp; }).collect(Collectors.toList()); empList.forEach(System.out::println); }
public static void main(String[] args) { List<Integer> list = Arrays.asList(10,20,30,40,50); Integer res = list.stream().reduce((sum, item) -> sum + item).get(); System.out.println(res); }
public static void main(String[] args) { List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); Stream<Integer> stream = list.stream(); // stream.reduce(new BinaryOperator<Integer>() { // @Override // public Integer apply(Integer a, Integer b) { // return a+b; // } // }); Optional<Integer> optional = stream.reduce((a, b) -> a + b); Integer integer = optional.get(); System.out.println(integer); }
public static void main(String[] args) { List<Integer> list = Arrays.asList(3, 5, 29, 7,17, 2, 19); IntSummaryStatistics stats = list.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("最大值:" + stats.getMax()); System.out.println("最小值:" + stats.getMin()); System.out.println("和:" + stats.getSum()); System.out.println("平均值:" + stats.getAverage()); }
public static void main(String[] args) { Map<String,Integer> map =new HashMap<>(); map.put("zhangsan",17); map.put("lisi",18); map.put("wanger",16); map.put("mazi",19); map.forEach((k,v)->System.out.println(k+" : "+v)); }
public static void main(String[] args) throws IOException { Dept accounting = new Dept((byte) 10, "ACCOUNTING", "NewYork"); Dept research = new Dept((byte) 20, "RESEARCH", "Dallas"); Dept sales = new Dept((byte) 30, "SALES", "Chicago"); Dept operations = new Dept((byte) 40, "OPERATIONS", "Boston"); List<Dept> list = new ArrayList<>(); list.add(accounting); list.add(research); list.add(sales); list.add(null); list.add(operations); Optional.ofNullable(list).orElse(Collections.emptyList()) //集合null过滤 .stream().filter(Objects::nonNull).forEach(item->{ //集合元素null过滤 System.out.println(item.getDname()); }); }
public static void main(String[] args) throws IOException { Dept accounting = new Dept((byte) 10, "ACCOUNTING", "NewYork"); Dept research = new Dept((byte) 20, "RESEARCH", "Dallas"); Dept sales = new Dept((byte) 30, "SALES", "Chicago"); Dept operations = new Dept((byte) 40, "OPERATIONS", "Boston"); Map<Integer, Dept> map = new HashMap<>(); map.put(10, accounting); map.put(20, research); map.put(30, sales); map.put(null, operations); map.keySet().stream().filter(Objects::nonNull) .forEach(System.out::println); }
使用java.util.Arrays工具类中的asList()方法(这个不是Java8中新增的内容):svg
String[] arrays = new String[]{"a", "b", "c"}; List<String> listStrings = Arrays.asList(arrays);
使用Stream中的Collector收集器,代码:工具
String[] arrays = new String[]{"a", "b", "c"}; List<String> listStringOne = Stream.of(arrays).collector(Collectors.toList()); List<String> listStringTwo = Arrays.stream(arrays).collector(Collectors.toList());
使用Stream:spa
String[] ss = listStrings.stream().toArray(String[]::new);
使用List中的toArray()方法.net
String[] sss = listStrings.toArray(new String[listStrings.size()]);
public static void main(String[] args) { String[] array = List.of("aa", "bb").toArray(String[]::new); for (String item : array) System.out.println(item); }