日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

Java 流式編程的七個(gè)必學(xué)技巧

來(lái)源: 責(zé)編: 時(shí)間:2024-06-25 17:13:49 164觀看
導(dǎo)讀Java Streams在很多年前就被引入了,但作為Java開(kāi)發(fā)者,我們還沒(méi)有完全掌握這個(gè)多功能工具的威力。在這里,你將發(fā)現(xiàn)一些有價(jià)值的技巧,可以作為參考并應(yīng)用到你的下一個(gè)項(xiàng)目中。在下面的示例中,我們將使用以下類。@Getterclass

Java Streams在很多年前就被引入了,但作為Java開(kāi)發(fā)者,我們還沒(méi)有完全掌握這個(gè)多功能工具的威力。在這里,你將發(fā)現(xiàn)一些有價(jià)值的技巧,可以作為參考并應(yīng)用到你的下一個(gè)項(xiàng)目中。JlX28資訊網(wǎng)——每日最新資訊28at.com

在下面的示例中,我們將使用以下類。JlX28資訊網(wǎng)——每日最新資訊28at.com

JlX28資訊網(wǎng)——每日最新資訊28at.com

@Getterclass Company {  private String name;  private Address address;  private List personList;}@Getterclass Person {  private Long id;  private String name;}@Getterclass Address {  private String street;  private City city;}@Getterclass City {  private String name;  private State state;}@Getterclass State{  private String name;}

1. 使用方法引用簡(jiǎn)化地圖

以下代碼可獲取公司地址的城市名稱。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getCityNames(List companyList){  return companyList.stream()    .map(company -> company.getAddress().getCity().getName())    .toList();}

可以替換為以下更具可讀性的版本。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getCityNames(List companyList){  return companyList.stream()    .map(Company::getAddress)    .map(Address::getCity)    .map(City::getName)    .toList();}

2. 空值檢查

上述代碼加上空值檢查。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getCityNames(List companyList){  return companyList.stream()    .map(Company::getAddress)    .filter(Objects::nonNull)    .map(Address::getCity)    .filter(Objects::nonNull)    .map(City::getName)    .filter(Objects::nonNull)    .toList();}

3. 從流的流到流

以下代碼獲取所有公司的人員名單列表。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getAllPerson(List companyList){  // 生成一個(gè)Person列表的列表  List> partialResult = companyList.stream()    .map(Company::getPersonList)    .toList();  // 將每個(gè)Person列表添加到結(jié)果中  List result = new ArrayList<>();  partialResult.forEach(result::addAll);  return result;}

可以用以下方式實(shí)現(xiàn)相同的功能。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getAllPerson(List companyList){  return companyList.stream()    .map(Company::getPersonList) // 返回一個(gè)Stream>    .flatMap(List::stream)  // 返回一個(gè)Stream    .toList(

4. 按屬性分組

以下代碼將返回一張地圖,其中包含每個(gè)城市的公司列表。JlX28資訊網(wǎng)——每日最新資訊28at.com

public Map> getCompaniesByCity(List companyList){  return companyList.stream()    .collect(Collectors.groupingBy(company -> company.getAddress().getCity()));}

5. 檢查流中是否有項(xiàng)目

以下代碼會(huì)檢查是否有公司在某個(gè)城市。JlX28資訊網(wǎng)——每日最新資訊28at.com

public boolean hasCompanyInCity(List companyList, String cityName){  return companyList.stream()    .map(Company::getAddress)    .map(Address::getName)    .anyMatch(cityName::equals);}

同樣的方法也適用于noneMatch,如果你想檢查某個(gè)城市是否有公司。JlX28資訊網(wǎng)——每日最新資訊28at.com

public boolean hasNoCompanyInCity(List companyList, String cityName){  return companyList.stream()    .map(Company::getAddress)    .map(Address::getName)    .noneMatch(cityName::equals);}

6. 記錄日志

使用peek方法為每個(gè)返回的城市名記錄日志。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getCityNames(List companyList){  return companyList.stream()    .map(Company::getAddress)    .map(Address::getCity)    .map(City::getName)    .peek(cityName -> log.info(cityName))    .toList();}

7. 獲取唯一的城市名稱

使用distinct從流中移除重復(fù)的城市名稱。JlX28資訊網(wǎng)——每日最新資訊28at.com

public List getUniqueCityNames(List companyList){  return companyList.stream()    .map(Company::getAddress)    .map(Address::getCity)    .map(City::getName)    .distinct()    .toList();}

以上就是通過(guò)實(shí)例展示的7個(gè)技巧,希望對(duì)你有所幫助。JlX28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-96419-0.htmlJava 流式編程的七個(gè)必學(xué)技巧

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: Vue 點(diǎn)擊彈窗外部,實(shí)現(xiàn)彈窗關(guān)閉?你有實(shí)現(xiàn)的思路嗎?

下一篇: LangChain轉(zhuǎn)換鏈:讓數(shù)據(jù)處理更精準(zhǔn)

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 额尔古纳市| 电白县| 肇州县| 彭州市| 武冈市| 大埔县| 象山县| 开远市| 崇仁县| 高雄县| 德庆县| 镇原县| 吴江市| 开远市| 读书| 黔西| 普兰店市| 丽水市| 保山市| 开远市| 彰化市| 凤翔县| 青铜峡市| 永福县| 连城县| 奉化市| 历史| 衡阳市| 盱眙县| 西乡县| 张家界市| 乌拉特前旗| 建湖县| 天全县| 峡江县| 德保县| 砚山县| 五河县| 砀山县| 成武县| 凤翔县|