您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么使用Lambda表達式簡化代碼提高生產力”,在日常操作中,相信很多人在怎么使用Lambda表達式簡化代碼提高生產力問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用Lambda表達式簡化代碼提高生產力”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
未使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); for (String fruit : list) { System.out.println(fruit); }
使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); list.forEach(fruit -> System.out.println(fruit));
未使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); Collections.sort(list, new Comparator<String>() { public int compare(String s1, String s2) { return s1.compareTo(s2); } });
使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
未使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); List<String> filteredList = new ArrayList<String>(); for (String fruit : list) { if (fruit.startsWith("a")) { filteredList.add(fruit); } }
使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); List<String> filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).collect(Collectors.toList());
未使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); List<Integer> lengths = new ArrayList<Integer>(); for (String fruit : list) { lengths.add(fruit.length()); }
使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); List<Integer> lengths = list.stream().map(fruit -> fruit.length()) .collect(Collectors.toList());
未使用Lambda表達式:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int sum = 0; for (int i : list) { sum += i; }
使用Lambda表達式:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); int sum = list.stream().reduce(0, (a, b) -> a + b);
未使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); Map<Integer, List<String>> grouped = new HashMap<Integer, List<String>>(); for (String fruit : list) { int length = fruit.length(); if (!grouped.containsKey(length)) { grouped.put(length, new ArrayList<String>()); } grouped.get(length).add(fruit); }
使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); Map<Integer, List<String>> grouped = list.stream().collect(Collectors.groupingBy(fruit -> fruit.length()));
未使用Lambda表達式:
public interface MyInterface { public void doSomething(String input); } MyInterface myObject = new MyInterface() { public void doSomething(String input) { System.out.println(input); } }; myObject.doSomething("Hello World");
使用Lambda表達式:
MyInterface myObject = input -> System.out.println(input); myObject.doSomething("Hello World");
未使用Lambda表達式:
Thread thread = new Thread(new Runnable() { public void run() { System.out.println("Thread is running."); } }); thread.start();
使用Lambda表達式:
Thread thread = new Thread(() -> System.out.println("Thread is running.")); thread.start();
未使用Lambda表達式:
String str = "Hello World"; if (str != null) { System.out.println(str.toUpperCase()); }
使用Lambda表達式:
Optional<String> str = Optional.ofNullable("Hello World"); str.map(String::toUpperCase).ifPresent(System.out::println);
未使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); List<String> filteredList = new ArrayList<String>(); for (String fruit : list) { if (fruit.startsWith("a")) { filteredList.add(fruit.toUpperCase()); } } Collections.sort(filteredList);
使用Lambda表達式:
List<String> list = Arrays.asList("apple", "banana", "orange"); List<String> filteredList = list.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList());
到此,關于“怎么使用Lambda表達式簡化代碼提高生產力”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。