偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

開(kāi)發(fā) 開(kāi)發(fā)工具
作為Java開(kāi)發(fā)者,我們還沒(méi)有完全掌握J(rèn)ava Streams這個(gè)多功能工具的威力。在這里,你將發(fā)現(xiàn)一些有價(jià)值的技巧,可以作為參考并應(yīng)用到你的下一個(gè)項(xiàng)目中。

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

在下面的示例中,我們將使用以下類。

@Getter
class Company {
  private String name;
  private Address address;
  private List personList;
}

@Getter
class Person {
  private Long id;
  private String name;
}

@Getter
class Address {
  private String street;
  private City city;
}

@Getter
class City {
  private String name;
  private State state;
}

@Getter
class State{
  private String name;
}

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

以下代碼可獲取公司地址的城市名稱。

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

可以替換為以下更具可讀性的版本。

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

2. 空值檢查

上述代碼加上空值檢查。

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. 從流的流到流

以下代碼獲取所有公司的人員名單列表。

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)相同的功能。

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

4. 按屬性分組

以下代碼將返回一張地圖,其中包含每個(gè)城市的公司列表。

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

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

以下代碼會(huì)檢查是否有公司在某個(gè)城市。

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

同樣的方法也適用于noneMatch,如果你想檢查某個(gè)城市是否有公司。

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

6. 記錄日志

使用peek方法為每個(gè)返回的城市名記錄日志。

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ù)的城市名稱。

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

以上就是通過(guò)實(shí)例展示的7個(gè)技巧,希望對(duì)你有所幫助。

責(zé)任編輯:趙寧寧 來(lái)源: Java學(xué)研大本營(yíng)
相關(guān)推薦

2015-06-11 13:34:54

編程編程階段

2023-09-07 16:28:46

JavaScrip

2021-11-22 12:13:54

Linuxwget 命令

2022-11-21 17:58:23

編程語(yǔ)言技巧

2021-08-17 10:08:44

HTML網(wǎng)站網(wǎng)絡(luò)

2022-04-14 10:40:11

領(lǐng)導(dǎo)者IT團(tuán)隊(duì)遠(yuǎn)程團(tuán)隊(duì)

2023-05-30 09:59:38

2018-05-24 08:47:15

數(shù)據(jù)存儲(chǔ)技巧

2019-09-09 10:32:51

基于意圖的網(wǎng)絡(luò)IBN網(wǎng)絡(luò)

2016-12-13 10:06:25

編寫Java單元測(cè)試技巧

2024-11-08 16:24:39

2023-12-15 08:51:48

2015-11-30 17:12:31

Git使用技巧

2012-09-17 10:57:39

郵件安全

2022-08-26 08:00:00

數(shù)字時(shí)代IT首席信息官

2021-12-17 10:29:38

CIOITCFO

2021-06-10 08:00:00

首席信息安全官IT數(shù)據(jù)

2021-03-02 10:54:08

高管IT投資首席信息官

2023-04-19 15:29:53

通信技巧Vue 3開(kāi)發(fā)

2022-07-14 10:34:13

IT領(lǐng)導(dǎo)者CIO首席信息官
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)