本篇內(nèi)容主要講解“JDK8怎么通過Stream對List、Map操作和互轉(zhuǎn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“JDK8怎么通過Stream對List、Map操作和互轉(zhuǎn)”吧!
創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為石門企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、做網(wǎng)站,石門網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。1、Map數(shù)據(jù)轉(zhuǎn)換為自定義對象的List,例如把map的key,value分別對應(yīng)Person對象兩個屬性:
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey())) .map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)) .map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());List<Person> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey()) .map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
以上三種方式不同之處在于排序的處理。參考鏈接:
/tupian/20230522/page-not-found employees = persons.stream() .filter(p -> p.getLastName().equals("l1")) .map(p -> new Employee(p.getName(), p.getLastName(), 1000)) .collect(Collectors.toList());
3、從List中過濾出一個元素
User match = users.stream().filter((user) -> user.getId() == 1).findAny().get();
4、List轉(zhuǎn)換為Map
public class Hosting { private int Id; private String name; private long websites; public Hosting(int id, String name, long websites) { Id = id; this.name = name; this.websites = websites; } //getters, setters and toString()} Map<Integer, String> result1 = list.stream().collect( Collectors.toMap(Hosting::getId, Hosting::getName));
到此,相信大家對“JDK8怎么通過Stream對List、Map操作和互轉(zhuǎn)”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前名稱:JDK8怎么通過Stream對List、Map操作和互轉(zhuǎn)-創(chuàng)新互聯(lián)
標題來源:http://muchs.cn/article36/ceohsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、域名注冊、服務(wù)器托管、靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容