溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java 8中使用Lambda表達(dá)式實現(xiàn)將實體映射到DTO中

發(fā)布時間:2020-11-07 16:46:09 來源:億速云 閱讀:636 作者:Leah 欄目:開發(fā)技術(shù)

Java 8中使用Lambda表達(dá)式實現(xiàn)將實體映射到DTO中?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

當(dāng)我們需要將DTO轉(zhuǎn)換為實體(Hibernate實體等)并向后轉(zhuǎn)換時,我們都會面臨混亂的開銷代碼。

在我的示例中,我將用Java 8演示代碼如何變得越來越短。

讓我們創(chuàng)建目標(biāo)DTO:

public class ActiveUserListDTO { 
  public ActiveUserListDTO() {
  }
 
  public ActiveUserListDTO(UserEntity userEntity) { 
    this.username = userEntity.getUsername(); 
   ...
  }
}

使用Spring數(shù)據(jù)JPA API檢索所有實體的簡單查找方法:

userRepository.findAll();
Problem:
Find.All() method signature (like many others) returns java.lang.Iterable<T>
java.lang.Iterable<T> findAll(java.lang.Iterable<ID> iterable)

我們不能使用java.lang.Iterable(*在集合上運(yùn)行的Streams來制作Stream。每個Collection都是Iterable,但并不是每個Iterable都是必需的Collection)。

那么,如何獲取Stream對象以獲得Java8 Lambda的Power?

讓我們使用StreamSupport對象將Iterable轉(zhuǎn)換為Stream:

Stream<UserEntity> userEntityStream = StreamSupport.stream(userRepository.findAll().spliterator(), false);

大。 現(xiàn)在,我們掌握了Stream,這是Java 8 Labmda的關(guān)鍵!

剩下的就是地圖和收集:

List<ActiveUserList> activeUserListDTOs =

userEntities.stream().map(ActiveUserList::new).collect(Collectors.toList());

我正在使用Java 8 Method Reference,因此將每個實體初始化(和映射)到dto中。

因此,讓我們對所有內(nèi)容進(jìn)行簡短介紹:

List<ActiveUserList> activeUserListDTOs=StreamSupport.stream(userRepository.findAll().spliterator(), false).map(ActiveUserList::new).collect(Collectors.toList());

那很整齊?。?/p>

補(bǔ)充知識:java8中使用Lambda表達(dá)式將list中實體類的兩個字段轉(zhuǎn)Map

代碼:

List<Entity> list = new ArrayList<>();

Map<Integer, String> map = list.stream().collect(Collectors.toMap(Entity::getId, Entity::getType));

常用的lambda表達(dá)式:

**
 * List -> Map
 * 需要注意的是:
 * toMap 如果集合對象有重復(fù)的key,會報錯Duplicate key ....
 * apple1,apple12的id都為1。
 * 可以用 (k1,k2)->k1 來設(shè)置,如果有重復(fù)的key,則保留key1,舍棄key2
 */
Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));
 
安照某一字段去重
list = list.stream().filter(distinctByKey(p -> ((ModCreditColumn) p).getFieldCode())).collect(Collectors.toList());
 
List<Double> unitNetValue = listIncreaseDto.stream().map(IncreaseDto :: getUnitNetValue).collect(Collectors.toList());
 
//求和 對象List
BigDecimal allFullMarketPrice = entityList.stream().filter(value -> value.getFullMarketPrice()!= null).map(SceneAnalysisRespVo::getFullMarketPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
 
 List<BigDecimal> naturalDayList;
 BigDecimal total = naturalDayList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
 
分組函數(shù)
Map<String, List<SceneAnalysisRespVo>> groupMap = total.getGroupList().stream().collect(Collectors.groupingBy(SceneAnalysisRespVo::getVmName));
 
//DV01之和
BigDecimal allDV01 = values.stream().filter(sceneAnalysisRespVo -> sceneAnalysisRespVo.getDv() != null).map(SceneAnalysisRespVo::getDv).reduce(BigDecimal.ZERO, BigDecimal::add);

看完上述內(nèi)容,你們掌握J(rèn)ava 8中使用Lambda表達(dá)式實現(xiàn)將實體映射到DTO中的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI