新聞中心
本篇內容主要講解“JDK8怎么通過Stream對List、Map操作和互轉”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“JDK8怎么通過Stream對List、Map操作和互轉”吧!
1、Map數據轉換為自定義對象的List,例如把map的key,value分別對應Person對象兩個屬性:
List
以上三種方式不同之處在于排序的處理。參考鏈接:
/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轉換為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
到此,相信大家對“JDK8怎么通過Stream對List、Map操作和互轉”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
分享標題:JDK8怎么通過Stream對List、Map操作和互轉-創(chuàng)新互聯(lián)
分享路徑:http://fisionsoft.com.cn/article/ceohsg.html