java中实现list或set转map的方法

2022-10-18,,

这篇文章主要介绍了java中实现list或set转map的方法的相关资料,需要的朋友可以参考下

java中实现list或set转map的方法

在开发中我们有时需要将list或set转换为map(比如对象属性中的唯一键作为map的key,对象作为map的value),一般的想法就是new一个map,然后把list或set中的值一个个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); 
} 

是否还有更优雅的写法呢?答案是有的。

guava提供了集合(实现了Iterables接口或Iterator接口)转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(); 
} 

这样我们就可以很方便的进行转换了,如下:

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返回的结果产生了重复的key,将会抛出异常。

java8也提供了转换的方法,这里直接照搬别人博客的代码:

@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());  
}  

参考:https://www.jb51.net/article/104114.htm

您可能感兴趣的文章:

  • Java集合Set、List、Map的遍历方法
  • list,set,map,数组之间的相互转换详细解析
  • java list,set,map,数组间的相互转换详解
  • Java集合定义与用法实例总结【Set、List与Map】
  • 详解Java中list,set,map的遍历与增强for循环
  • 一段代码搞懂关于Java中List、Set集合及Map的使用
  • Java中List Set和Map之间的区别_动力节点Java学院整理
  • Java中的Set、List、Map的用法与区别介绍
  • Java中Collection、List、Set、Map之间的关系总结
  • Java中集合List、Set和Map的入门详细介绍

《java中实现list或set转map的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。