最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中實(shí)現(xiàn)list或set轉(zhuǎn)map的方法

 更新時(shí)間:2017年01月24日 17:12:15   投稿:lqh  
這篇文章主要介紹了java中實(shí)現(xiàn)list或set轉(zhuǎn)map的方法的相關(guān)資料,需要的朋友可以參考下

java中實(shí)現(xiàn)list或set轉(zhuǎn)map的方法

在開(kāi)發(fā)中我們有時(shí)需要將list或set轉(zhuǎn)換為map(比如對(duì)象屬性中的唯一鍵作為map的key,對(duì)象作為map的value),一般的想法就是new一個(gè)map,然后把list或set中的值一個(gè)個(gè)push到map中。

類似下面的代碼:

List<String> stringList = Lists.newArrayList("t1", "t2", "t3"); 
Map<String, String> map = Maps.newHashMapWithExpectedSize(stringList.size()); 
for (String str : stringList) { 
  map.put(str, str); 
} 

是否還有更優(yōu)雅的寫(xiě)法呢?答案是有的。

guava提供了集合(實(shí)現(xiàn)了Iterables接口或Iterator接口)轉(zhuǎn)map的方法,方法定義如下:


/** 

* Returns an immutable map for which the {@link Map#values} are the given 
 * elements in the given order, and each key is the product of invoking a 
 * supplied function on its corresponding value. 
 * 
 * @param values the values to use when constructing the {@code Map} 
 * @param keyFunction the function used to produce the key for each value 
 * @return a map mapping the result of evaluating the function {@code 
 *     keyFunction} on each value in the input collection to that value 
 * @throws IllegalArgumentException if {@code keyFunction} produces the same 
 *     key for more than one value in the input collection 
 * @throws NullPointerException if any elements of {@code values} is null, or 
 *     if {@code keyFunction} produces {@code null} for any value 
 */ 
public static <K, V> ImmutableMap<K, V> uniqueIndex( 
  Iterable<V> values, Function<? super V, K> keyFunction) { 
 return uniqueIndex(values.iterator(), keyFunction); 
} 
 
/** 
 * Returns an immutable map for which the {@link Map#values} are the given 
 * elements in the given order, and each key is the product of invoking a 
 * supplied function on its corresponding value. 
 * 
 * @param values the values to use when constructing the {@code Map} 
 * @param keyFunction the function used to produce the key for each value 
 * @return a map mapping the result of evaluating the function {@code 
 *     keyFunction} on each value in the input collection to that value 
 * @throws IllegalArgumentException if {@code keyFunction} produces the same 
 *     key for more than one value in the input collection 
 * @throws NullPointerException if any elements of {@code values} is null, or 
 *     if {@code keyFunction} produces {@code null} for any value 
 * @since 10.0 
 */ 
public static <K, V> ImmutableMap<K, V> uniqueIndex( 
  Iterator<V> values, Function<? super V, K> keyFunction) { 
 checkNotNull(keyFunction); 
 ImmutableMap.Builder<K, V> builder = ImmutableMap.builder(); 
 while (values.hasNext()) { 
  V value = values.next(); 
  builder.put(keyFunction.apply(value), value); 
 } 
 return builder.build(); 
} 

這樣我們就可以很方便的進(jìn)行轉(zhuǎn)換了,如下:

List<String> stringList = Lists.newArrayList("t1", "t2", "t3"); 
Map<String, String> map = Maps.uniqueIndex(stringList, new Function<String, String>() { 
  @Override 
  public String apply(String input) { 
    return input; 
  } 
}); 

需要注意的是,如接口注釋所說(shuō),如果Function返回的結(jié)果產(chǎn)生了重復(fù)的key,將會(huì)拋出異常。

java8也提供了轉(zhuǎn)換的方法,這里直接照搬別人博客的代碼:

@Test  
public void convert_list_to_map_with_java8_lambda () {  
    
  List<Movie> movies = new ArrayList<Movie>();  
  movies.add(new Movie(1, "The Shawshank Redemption"));  
  movies.add(new Movie(2, "The Godfather"));  
  
  Map<Integer, Movie> mappedMovies = movies.stream().collect(  
      Collectors.toMap(Movie::getRank, (p) -> p));  
  
  logger.info(mappedMovies);  
  
  assertTrue(mappedMovies.size() == 2);  
  assertEquals("The Shawshank Redemption", mappedMovies.get(1).getDescription());  
}  

參考:http://www.fzitv.net/article/104114.htm

相關(guān)文章

  • Java代理模式(Proxy)實(shí)現(xiàn)方法詳解

    Java代理模式(Proxy)實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Java代理模式(Proxy)實(shí)現(xiàn)的相關(guān)資料,代理模式是一種結(jié)構(gòu)型設(shè)計(jì)模式,通過(guò)引入代理對(duì)象來(lái)控制對(duì)目標(biāo)對(duì)象的訪問(wèn),代理模式的優(yōu)點(diǎn)包括職責(zé)清晰、擴(kuò)展性好、保護(hù)目標(biāo)對(duì)象和增強(qiáng)功能,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • java鍵盤(pán)錄入的方法舉例詳解

    java鍵盤(pán)錄入的方法舉例詳解

    這篇文章主要給大家介紹了關(guān)于java鍵盤(pán)錄入的相關(guān)資料,我們?cè)趯?xiě)程序的時(shí)候,數(shù)據(jù)值都是固定的,但是實(shí)際開(kāi)發(fā)中,數(shù)據(jù)值肯定是變化的,所以,把數(shù)據(jù)改進(jìn)為鍵盤(pán)錄入,提高程序的靈活性,需要的朋友可以參考下
    2023-10-10
  • @RequestBody,@RequestParam和@Param的區(qū)別說(shuō)明

    @RequestBody,@RequestParam和@Param的區(qū)別說(shuō)明

    這篇文章主要介紹了@RequestBody,@RequestParam和@Param的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • MybatisPlus整合Flowable出現(xiàn)的坑及解決

    MybatisPlus整合Flowable出現(xiàn)的坑及解決

    這篇文章主要介紹了MybatisPlus整合Flowable出現(xiàn)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java發(fā)送https請(qǐng)求并跳過(guò)ssl證書(shū)驗(yàn)證方法

    Java發(fā)送https請(qǐng)求并跳過(guò)ssl證書(shū)驗(yàn)證方法

    最近在負(fù)責(zé)一個(gè)對(duì)接第三方服務(wù)的事情,在對(duì)接期間因?yàn)榈谌椒?wù)為https的請(qǐng)求,這篇文章主要給大家介紹了關(guān)于Java發(fā)送https請(qǐng)求并跳過(guò)ssl證書(shū)驗(yàn)證的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 理解Java的序列化與反序列化

    理解Java的序列化與反序列化

    這篇文章主要為大家詳細(xì)介紹了Java的序列化與反序列化,序列化是一種對(duì)象持久化的手段。普遍應(yīng)用在網(wǎng)絡(luò)傳輸、RMI等場(chǎng)景中。本文通過(guò)分析ArrayList的序列化來(lái)介紹Java序列化的相關(guān)內(nèi)容,感興趣的小伙伴們可以參考一下
    2016-02-02
  • MyBatis 源碼分析 之SqlSession接口和Executor類

    MyBatis 源碼分析 之SqlSession接口和Executor類

    mybatis框架在操作數(shù)據(jù)的時(shí)候,離不開(kāi)SqlSession接口實(shí)例類的作用,下面通過(guò)本文給大家實(shí)例剖析MyBatis 源碼分析之SqlSession接口和Executor類,需要的朋友參考下吧
    2017-02-02
  • RocketMQ存儲(chǔ)文件的實(shí)現(xiàn)

    RocketMQ存儲(chǔ)文件的實(shí)現(xiàn)

    這篇文章主要介紹了RocketMQ存儲(chǔ)文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Maven配置單倉(cāng)庫(kù)與多倉(cāng)庫(kù)的實(shí)現(xiàn)(Nexus)

    Maven配置單倉(cāng)庫(kù)與多倉(cāng)庫(kù)的實(shí)現(xiàn)(Nexus)

    本文主要介紹了Maven配置單倉(cāng)庫(kù)與多倉(cāng)庫(kù)的實(shí)現(xiàn)(Nexus),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • MyBatis之insert主鍵自增和自定義主鍵詳解

    MyBatis之insert主鍵自增和自定義主鍵詳解

    本文介紹了如何使用MyBatis解決插入數(shù)據(jù)時(shí)因主鍵唯一性約束導(dǎo)致的錯(cuò)誤問(wèn)題,以及如何自定義主鍵生成規(guī)則,文中詳細(xì)解釋了如何在MyBatis中配置自增主鍵,并提供了測(cè)試示例
    2024-12-12

最新評(píng)論

丰台区| 崇明县| 镇远县| 霸州市| 泉州市| 托克逊县| 金乡县| 黎川县| 平果县| 荆门市| 柘荣县| 思南县| 盘山县| 且末县| 遂宁市| 海门市| 怀仁县| 沿河| 旺苍县| 聊城市| 阿克陶县| 兴义市| 泌阳县| 自治县| 合江县| 长泰县| 南投县| 雷州市| 普陀区| 和田市| 潢川县| 正镶白旗| 格尔木市| 青冈县| 郎溪县| 西乌珠穆沁旗| 绥中县| 陇南市| 鄱阳县| 钦州市| 汉源县|