国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java中實(shí)現(xiàn)list或set轉(zhuǎn)map的方法

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

2020-08-01 15:27java教程網(wǎng) Java教程

這篇文章主要介紹了java中實(shí)現(xiàn)list或set轉(zhuǎn)map的方法的相關(guān)資料,需要的朋友可以參考下

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

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

類似下面的代碼:

?
1
2
3
4
5
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)雅的寫法呢?答案是有的。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 
* 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)換了,如下:

?
1
2
3
4
5
6
7
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;
  }
});

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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.jfrwli.cn/article/90553.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美国产精品一区二区三区 | 国产成人久久精品一区二区三区 | 国产片在线观看免费观看 | 国产精品18久久久久久久久久久久 | 亚洲伦理一区 | 国产精品国产精品国产专区不卡 | 亚洲永久免费 | 久草在线视频免费播放 | 91精品国产高清久久久久久久久 | 中国精品自拍 | 国产在线视频网站 | 国产精品久久久久无码av | 一区二区三区国产好的精 | 午夜成人免费电影 | 看av的网址| 亚洲综合一二区 | 91视频精品 | 亚洲深深色噜噜狠狠网站 | 国产一区二区三区在线视频 | 欧美在线综合 | 夜夜骑首页 | 视频二区在线观看 | 日韩免费视频一区二区 | 精品国产91乱码一区二区三区 | 精品久久中文字幕 | 国产大片在线观看 | 日韩av中文| 色爽| 色婷婷av久久久久久久 | 久久精品综合 | 欧美日韩国产一区二区在线观看 | 久久a视频| 99久久婷婷国产精品综合 | 老女肥熟av免费观看 | 黄色小网站在线观看 | 中文字幕四虎 | 色噜噜狠狠狠综合曰曰曰 | 三级成人在线 | 中文字幕国产一区 | 日日骚网| 久久不卡 |