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

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

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

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

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

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

f5928資訊網(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)化地圖

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

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

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

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

2. 空值檢查

上述代碼加上空值檢查。f5928資訊網(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. 從流的流到流

以下代碼獲取所有公司的人員名單列表。f5928資訊網(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)相同的功能。f5928資訊網(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è)城市的公司列表。f5928資訊網(wǎng)——每日最新資訊28at.com

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

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

以下代碼會(huì)檢查是否有公司在某個(gè)城市。f5928資訊網(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è)城市是否有公司。f5928資訊網(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è)返回的城市名記錄日志。f5928資訊網(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ù)的城市名稱。f5928資訊網(wǎng)——每日最新資訊28at.com

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

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

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

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quá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 主站蜘蛛池模板: 溧水县| 贵阳市| 利津县| 兖州市| 昌吉市| 巫溪县| 南召县| 宜城市| 平顺县| 望奎县| 石渠县| 招远市| 边坝县| 乳山市| 朝阳县| 吴旗县| 扶余县| 雷州市| 墨江| 漠河县| 喜德县| 堆龙德庆县| 华池县| 台中市| 宁晋县| 兴城市| 神木县| 凤翔县| 英吉沙县| 视频| 且末县| 徐汇区| 阿勒泰市| 望城县| 青阳县| 永济市| 密云县| 吉安市| 北京市| 光山县| 新巴尔虎左旗|