最近,我和一位同事讨论了在Java中将List转换为Map的最佳方法,以及这样做是否有任何具体的好处。

我想知道最佳的转换方法,如果有人能指导我,我将非常感激。

这是一个好方法吗?

List<Object[]> results;
Map<Integer, String> resultsMap = new HashMap<Integer, String>();
for (Object[] o : results) {
    resultsMap.put((Integer) o[0], (String) o[1]);
}

当前回答

List<Item> list;
Map<Key,Item> map = new HashMap<Key,Item>();
for (Item i : list) map.put(i.getKey(),i);

当然,假设每个Item都有一个getKey()方法,该方法返回一个正确类型的键。

其他回答

List<Item> list;
Map<Key,Item> map = new HashMap<Key,Item>();
for (Item i : list) map.put(i.getKey(),i);

当然,假设每个Item都有一个getKey()方法,该方法返回一个正确类型的键。

根据你想要达到的目标,你会想到许多解决方案:

每个List项都是键和值

for( Object o : list ) {
    map.put(o,o);
}

列表元素有一些可以查找的东西,可能是一个名称:

for( MyObject o : list ) {
    map.put(o.name,o);
}

列表元素有一些可以查找的东西,并且不能保证它们是唯一的:使用google MultiMaps

for( MyObject o : list ) {
    multimap.put(o.name,o);
}

将所有元素的位置作为键值:

for( int i=0; i<list.size; i++ ) {
    map.put(i,list.get(i));
}

...

这取决于你想要达到什么目标。

从示例中可以看到,Map是从键到值的映射,而列表只是一系列元素,每个元素都有一个位置。所以它们不能自动兑换。

public class EmployeeDetailsFetchListToMap {
  public static void main(String[] args) {
    List<EmployeeDetailsFetch> list = new ArrayList<>();
    list.add(new EmployeeDetailsFetch(1L, "vinay", 25000F));
    list.add(new EmployeeDetailsFetch(2L, "kohli", 5000000F));
    list.add(new EmployeeDetailsFetch(3L, "dhoni", 20000000F));

    //adding id as key and map of id and student name
    Map<Long, Map<Long, String>> map1 = list.stream()
        .collect(
            Collectors.groupingBy(
                EmployeeDetailsFetch::getEmpId,
                Collectors.toMap(
                    EmployeeDetailsFetch::getEmpId,
                    EmployeeDetailsFetch::getEmployeeName
                )
            )
        );
    System.out.println(map1);

    //converting list into map of Student
    //Adding id as Key and Value as Student into a map
    Map<Long, EmployeeDetailsFetch> map = list.stream()
        .collect(
            Collectors.toMap(
                EmployeeDetailsFetch::getEmpId, 
                EmployeeDetailsFetch -> EmployeeDetailsFetch
            )
        );

    for(Map.Entry<Long, EmployeeDetailsFetch> m : map.entrySet()) {
      System.out.println("key :" + m.getKey() + "  Value : " + m.getValue());
    }
  }
}

如果没有java-8,你可以在一行Commons集合和Closure类中完成这些

List<Item> list;
@SuppressWarnings("unchecked")
Map<Key, Item> map  = new HashMap<Key, Item>>(){{
    CollectionUtils.forAllDo(list, new Closure() {
        @Override
        public void execute(Object input) {
            Item item = (Item) input;
            put(i.getKey(), item);
        }
    });
}};

在java-8中,您可以使用流和collections类在一行中完成此操作。

Map<String, Item> map = 
    list.stream().collect(Collectors.toMap(Item::getKey, item -> item));

简短的演示:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Test{
    public static void main (String [] args){
        List<Item> list = IntStream.rangeClosed(1, 4)
                                   .mapToObj(Item::new)
                                   .collect(Collectors.toList()); //[Item [i=1], Item [i=2], Item [i=3], Item [i=4]]

        Map<String, Item> map = 
            list.stream().collect(Collectors.toMap(Item::getKey, item -> item));

        map.forEach((k, v) -> System.out.println(k + " => " + v));
    }
}
class Item {

    private final int i;

    public Item(int i){
        this.i = i;
    }

    public String getKey(){
        return "Key-"+i;
    }

    @Override
    public String toString() {
        return "Item [i=" + i + "]";
    }
}

输出:

Key-1 => Item [i=1]
Key-2 => Item [i=2]
Key-3 => Item [i=3]
Key-4 => Item [i=4]

如注释中所述,您可以使用Function.identity()而不是item -> item,尽管我发现I -> I相当显式。

为了完整起见,如果你的函数不是双射的,你可以使用二元算子。例如,让我们考虑这个List和映射函数,对于一个int值,计算它的模3的结果:

List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6);
Map<String, Integer> map = 
    intList.stream().collect(toMap(i -> String.valueOf(i % 3), i -> i));

当运行这段代码时,您将得到一个错误,说java.lang.IllegalStateException: Duplicate key 1。这是因为1% 3与4% 3相同,因此在给定键映射函数时具有相同的键值。在这种情况下,您可以提供一个合并操作符。

这是两个值的总和;(i1, i2) -> i1 + i2;可以用引用Integer::sum方法替换。

Map<String, Integer> map = 
    intList.stream().collect(toMap(i -> String.valueOf(i % 3), 
                                   i -> i, 
                                   Integer::sum));

现在输出:

0 => 9 (i.e 3 + 6)
1 => 5 (i.e 1 + 4)
2 => 7 (i.e 2 + 5)