java Stream操作轉換方法
流創(chuàng)建
方法 | |
---|---|
集合 | Collection.stream/parllelStream |
數(shù)組 | Arrays.stream |
數(shù)字Stream | IntStream/LongStream.range/rangeClosed/Random.inis/longs/doubles |
自己創(chuàng)建 | stream.generate/iterate |
1、list 轉 map
工作中,我們經常遇到list
轉map
的案例。Collectors.toMap
就可以把一個list
數(shù)組轉成一個Map
。代碼如下:
public class TestLambda { public static void main(String[] args) { List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "測試源碼", 18)); userInfoList.add(new UserInfo(2L, "程序員淘淘", 27)); userInfoList.add(new UserInfo(2L, "打代碼的淘淘", 26)); /** * list 轉 map * 使用Collectors.toMap的時候,如果有可以重復會報錯,所以需要加(k1, k2) -> k1 * (k1, k2) -> k1 表示,如果有重復的key,則保留第一個,舍棄第二個 */ Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo, (k1, k2) -> k1)); userInfoMap.values().forEach(a->System.out.println(a.getUserName())); } }
類似的,還有Collectors.toList()
、Collectors.toSet()
,表示把對應的流轉化為list
或者Set
。
2、filter()過濾
從數(shù)組集合中,過濾掉不符合條件的元素,留下符合條件的元素。
List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "測試源碼", 18)); userInfoList.add(new UserInfo(2L, "程序員淘淘", 27)); userInfoList.add(new UserInfo(3L, "打代碼的淘淘", 26)); /** * filter 過濾,留下超過18歲的用戶 */ List<UserInfo> userInfoResultList = userInfoList.stream().filter(user -> user.getAge() > 18).collect(Collectors.toList()); userInfoResultList.forEach(a -> System.out.println(a.getUserName())); //運行結果 程序員淘淘 打代碼的淘淘
3、foreach遍歷
foreach
遍歷list,遍歷map,真的很絲滑。
/** * forEach 遍歷集合List列表 */ List<String> userNameList = Arrays.asList("測試源碼", "程序員淘淘", "艿艿"); userNameList.forEach(System.out::println); HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("號碼你", "測試源碼"); hashMap.put("職業(yè)", "程序員淘淘"); hashMap.put("昵稱", "艿艿"); /** * forEach 遍歷集合Map */ hashMap.forEach((k, v) -> System.out.println(k + ":\t" + v)); //運行結果 測試源碼 程序員淘淘 打代碼的淘淘 職業(yè): 程序員淘淘 號碼你: 測試源碼 昵稱: 艿艿
4、groupingBy 分組
提到分組,相信大家都會想起SQL
的group by
。我們經常需要一個List做分組操作。比如,按城市分組用戶。在Java8之前,是這么實現(xiàn)的:
List<UserInfo> originUserInfoList = new ArrayList<>(); originUserInfoList.add(new UserInfo(1L, "測試源碼", 18,"深圳")); originUserInfoList.add(new UserInfo(3L, "打代碼的淘淘", 26,"湛江")); originUserInfoList.add(new UserInfo(2L, "程序員淘淘", 27,"深圳")); Map<String, List<UserInfo>> result = new HashMap<>(); for (UserInfo userInfo : originUserInfoList) { String city = userInfo.getCity(); List<UserInfo> userInfos = result.get(city); if (userInfos == null) { userInfos = new ArrayList<>(); result.put(city, userInfos); } userInfos.add(userInfo); }
而使用Java8
的groupingBy
分組器,清爽無比:
Map<String, List<UserInfo>> result = originUserInfoList.stream() .collect(Collectors.groupingBy(UserInfo::getCity));
5、sorted+Comparator 排序
工作中,排序的需求比較多,使用sorted+Comparator
排序,真的很香。
List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "測試源碼", 18)); userInfoList.add(new UserInfo(3L, "打代碼的淘淘", 26)); userInfoList.add(new UserInfo(2L, "程序員淘淘", 27)); /** * sorted + Comparator.comparing 排序列表, */ userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList()); userInfoList.forEach(a -> System.out.println(a.toString())); System.out.println("開始降序排序"); /** * 如果想降序排序,則可以使用加reversed() */ userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList()); userInfoList.forEach(a -> System.out.println(a.toString())); //運行結果 UserInfo{userId=1, userName='測試源碼', age=18} UserInfo{userId=3, userName='打代碼的淘淘', age=26} UserInfo{userId=2, userName='程序員淘淘', age=27} 開始降序排序 UserInfo{userId=2, userName='程序員淘淘', age=27} UserInfo{userId=3, userName='打代碼的淘淘', age=26} UserInfo{userId=1, userName='測試源碼', age=18} -=----------------------------------------------------------------- private static void test04(){ // 按工資升序排序(自然排序) List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName) .collect(Collectors.toList()); // 按工資倒序排序 List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()) .map(Person::getName).collect(Collectors.toList()); // 先按工資再按年齡升序排序 List<String> newList3 = personList.stream() .sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName) .collect(Collectors.toList()); // 先按工資再按年齡自定義排序(降序) List<String> newList4 = personList.stream().sorted((p1, p2) -> { if (p1.getSalary() == p2.getSalary()) { return p2.getAge() - p1.getAge(); } else { return p2.getSalary() - p1.getSalary(); } }).map(Person::getName).collect(Collectors.toList()); System.out.println("按工資升序排序:" + newList); System.out.println("按工資降序排序:" + newList2); System.out.println("先按工資再按年齡升序排序:" + newList3); System.out.println("先按工資再按年齡自定義降序排序:" + newList4); }
6、distinct去重
distinct
可以去除重復的元素:
List<String> list = Arrays.asList("A", "B", "F", "A", "C"); List<String> temp = list.stream().distinct().collect(Collectors.toList()); temp.forEach(System.out::println);
7、findFirst返回第一個
findFirst
很多業(yè)務場景,我們只需要返回集合的第一個元素即可:
List<String> list = Arrays.asList("A", "B", "F", "A", "C"); list.stream().findFirst().ifPresent(System.out::println);
8、anyMatch 是否至少匹配一個元素
anyMatch
檢查流是否包含至少一個滿足給定謂詞的元素。
Stream<String> stream = Stream.of("A", "B", "C", "D"); boolean match = stream.anyMatch(s -> s.contains("C")); System.out.println(match); //輸出 true
9、allMatch 匹配所有元素
allMatch
檢查流是否所有都滿足給定謂詞的元素。
Stream<String> stream = Stream.of("A", "B", "C", "D"); boolean match = stream.allMatch(s -> s.contains("C")); System.out.println(match); //輸出 false
10、map 轉換
map
方法可以幫我們做元素轉換,比如一個元素所有字母轉化為大寫,又或者把獲取一個元素對象的某個屬性,demo
如下:
List<String> list = Arrays.asList("jay", "tianluo"); //轉化為大寫 List<String> upperCaselist = list.stream().map(String::toUpperCase).collect(Collectors.toList()); upperCaselist.forEach(System.out::println);
11、Reduce
Reduce可以合并流的元素,并生成一個值
int sum = Stream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b); System.out.println(sum); /** * 求Integer集合的元素之和、乘積和最大值 * */ private static void test13() { List<Integer> list = Arrays.asList(1, 2, 3, 4); //求和 Optional<Integer> reduce = list.stream().reduce((x,y) -> x+ y); System.out.println("求和:"+reduce); //求積 Optional<Integer> reduce2 = list.stream().reduce((x,y) -> x * y); System.out.println("求積:"+reduce2); //求最大值 Optional<Integer> reduce3 = list.stream().reduce((x,y) -> x>y?x:y); System.out.println("求最大值:"+reduce3); } /* * 求所有員工的工資之和和最高工資 */ private static void test14() { initPerson(); Optional<Integer> reduce = personList.stream().map(Person :: getSalary).reduce(Integer::sum); Optional<Integer> reduce2 = personList.stream().map(Person :: getSalary).reduce(Integer::max); System.out.println("工資之和:"+reduce); System.out.println("最高工資:"+reduce2); }
12、peek 打印個日志
peek()
方法是一個中間Stream
操作,有時候我們可以使用peek
來打印日志。
List<String> result = Stream.of("程序員淘淘", "測試源碼", "打代碼的淘淘") .filter(a -> a.contains("芋艿")) .peek(a -> System.out.println("關注號碼你:" + a)).collect(Collectors.toList()); System.out.println(result); //運行結果 關注號碼你:程序員淘淘 關注號碼你:測試源碼 [程序員淘淘, 測試源碼]
13、Max,Min 最大最小
使用lambda流求最大,最小值,非常方便。
List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "測試源碼", 18)); userInfoList.add(new UserInfo(3L, "打代碼的淘淘", 26)); userInfoList.add(new UserInfo(2L, "程序員淘淘", 27)); Optional<UserInfo> maxAgeUserInfoOpt = userInfoList.stream().max(Comparator.comparing(UserInfo::getAge)); maxAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("max age user:" + userInfo)); Optional<UserInfo> minAgeUserInfoOpt = userInfoList.stream().min(Comparator.comparing(UserInfo::getAge)); minAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("min age user:" + userInfo)); //運行結果 max age user:UserInfo{userId=2, userName='程序員淘淘', age=27} min age user:UserInfo{userId=1, userName='測試源碼', age=18}
14、count 統(tǒng)計
一般count()
表示獲取流數(shù)據(jù)元素總數(shù)。
List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(new UserInfo(1L, "測試源碼", 18)); userInfoList.add(new UserInfo(3L, "打代碼的淘淘", 26)); userInfoList.add(new UserInfo(2L, "程序員淘淘", 27)); long count = userInfoList.stream().filter(user -> user.getAge() > 18).count(); System.out.println("大于18歲的用戶:" + count); //輸出 大于18歲的用戶:2 /** * 統(tǒng)計員工人數(shù)、平均工資、工資總額、最高工資 */ private static void test01(){ //統(tǒng)計員工人數(shù) Long count = personList.stream().collect(Collectors.counting()); //求平均工資 Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary)); //求最高工資 Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare)); //求工資之和 Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary)); //一次性統(tǒng)計所有信息 DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary)); System.out.println("統(tǒng)計員工人數(shù):"+count); System.out.println("求平均工資:"+average); System.out.println("求最高工資:"+max); System.out.println("求工資之和:"+sum); System.out.println("一次性統(tǒng)計所有信息:"+collect); }
15、提取/組合
流也可以進行合并、去重、限制、跳過等操作。
private static void test05(){ String[] arr1 = { "a", "b", "c", "d" }; String[] arr2 = { "d", "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1); Stream<String> stream2 = Stream.of(arr2); // concat:合并兩個流 distinct:去重 List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList()); // limit:限制從流中獲得前n個數(shù)據(jù) List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList()); // skip:跳過前n個數(shù)據(jù) List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList()); System.out.println("流合并:" + newList); System.out.println("limit:" + collect); System.out.println("skip:" + collect2); }
16、連接joining
joining可以將stream中的元素用特定的連接符(沒有的話,則直接連接)連接成一個字符串。
List<Integer> list = Arrays.asList(7, 6, 9, 3, 10, 2, 1); String collect = list.stream().map(String::valueOf).collect(Collectors.joining(",")); System.out.println(collect); 7,6,9,3,10,2,1
17、常用函數(shù)式接口
其實lambda離不開函數(shù)式接口,我們來看下JDK8常用的幾個函數(shù)式接口:
Function<T, R>
(轉換型): 接受一個輸入參數(shù),返回一個結果Consumer<T>
(消費型): 接收一個輸入參數(shù),并且無返回操作Predicate<T>
(判斷型): 接收一個輸入參數(shù),并且返回布爾值結果Supplier<T>
(供給型): 無參數(shù),返回結果
Function<T, R>
是一個功能轉換型的接口,可以把將一種類型的數(shù)據(jù)轉化為另外一種類型的數(shù)據(jù)
private void testFunction() { //獲取每個字符串的長度,并且返回 Function<String, Integer> function = String::length; Stream<String> stream = Stream.of("程序員淘淘", "測試源碼", "打代碼的淘淘"); Stream<Integer> resultStream = stream.map(function); resultStream.forEach(System.out::println); }
Consumer<T>
是一個消費性接口,通過傳入參數(shù),并且無返回的操作
private void testComsumer() { //獲取每個字符串的長度,并且返回 Consumer<String> comsumer = System.out::println; Stream<String> stream = Stream.of("程序員淘淘", "測試源碼", "打代碼的淘淘"); stream.forEach(comsumer); }
Predicate<T>
是一個判斷型接口,并且返回布爾值結果.
private void testPredicate() { //獲取每個字符串的長度,并且返回 Predicate<Integer> predicate = a -> a > 18; UserInfo userInfo = new UserInfo(2L, "程序員淘淘", 27); System.out.println(predicate.test(userInfo.getAge())); }
Supplier<T>
是一個供給型接口,無參數(shù),有返回結果。
private void testSupplier() { Supplier<Integer> supplier = () -> Integer.valueOf("666"); System.out.println(supplier.get()); }
這幾個函數(shù)在日常開發(fā)中,也是可以靈活應用的,比如我們DAO操作完數(shù)據(jù)庫,是會有個result的整型結果返回。我們就可以用Supplier<T>
來統(tǒng)一判斷是否操作成功。如下:
private void saveDb(Supplier<Integer> supplier) { if (supplier.get() > 0) { System.out.println("插入數(shù)據(jù)庫成功"); }else{ System.out.println("插入數(shù)據(jù)庫失敗"); } } @Test public void add() throws Exception { Course course=new Course(); course.setCname("java"); course.setUserId(100L); course.setCstatus("Normal"); saveDb(() -> courseMapper.insert(course)); }
到此這篇關于java Stream操作轉換方法的文章就介紹到這了,更多相關java Stream操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JAVA中StringBuffer與String的區(qū)別解析
這篇文章主要介紹了JAVA中StringBuffer與String的區(qū)別解析,需要的朋友可以參考下2014-02-02SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南
這篇文章主要給大家介紹了關于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置的相關資料,在Java SpringBoot項目中如果使用了Mybatis框架,默認情況下執(zhí)行的所有SQL操作都不會打印日志,需要的朋友可以參考下2023-10-10springboot用戶數(shù)據(jù)修改的詳細實現(xiàn)
用戶管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關于springboot用戶數(shù)據(jù)修改的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn)
SSH是 struts+spring+hibernate的一個集成框架,是目前比較流行的一種Web應用程序開源框架。本篇文章主要介紹了Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn),非常具有實用價值,需要的朋友可以參考下2018-04-04Spring動態(tài)注冊多數(shù)據(jù)源的實現(xiàn)方法
這篇文章主要介紹了Spring動態(tài)注冊多數(shù)據(jù)源的實現(xiàn)方法,小編覺的挺不錯的,現(xiàn)分享到腳本之家平臺,需要的朋友可以參考下2018-01-01