簡體   English   中英

如何將列表轉換為地圖?

[英]How to convert List to Map?

最近,我與一位同事討論了在 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]);
}

使用 ,您將能夠使用Collectors類在一行中完成此操作。

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)

希望能幫助到你! :)

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

當然,假設每個 Item 都有一個getKey()方法,該方法返回正確類型的鍵。

以防萬一這個問題沒有作為重復關閉,正確的答案是使用 Google Collections

Map<String,Role> mappedRoles = Maps.uniqueIndex(yourList, new Function<Role,String>() {
  public String apply(Role from) {
    return from.getName(); // or something else
  }});

簡短而甜蜜。

使用 Java 8,您可以執行以下操作:

Map<Key, Value> result= results
                       .stream()
                       .collect(Collectors.toMap(Value::getName,Function.identity()));

Value可以是您使用的任何對象。

從 Java 8 開始, @ZouZou使用Collectors.toMap器的答案當然是解決這個問題的慣用方法。

由於這是一項如此常見的任務,我們可以將其變成一個靜態實用程序。

這樣,解決方案就真正變成了單線解決方案。

/**
 * Returns a map where each entry is an item of {@code list} mapped by the
 * key produced by applying {@code mapper} to the item.
 *
 * @param list the list to map
 * @param mapper the function to produce the key from a list item
 * @return the resulting map
 * @throws IllegalStateException on duplicate key
 */
public static <K, T> Map<K, T> toMapBy(List<T> list,
        Function<? super T, ? extends K> mapper) {
    return list.stream().collect(Collectors.toMap(mapper, Function.identity()));
}

以下是在List<Student>上使用它的方法:

Map<Long, Student> studentsById = toMapBy(students, Student::getId);

Alexis 已經使用toMap(keyMapper, valueMapper)方法在Java 8 中發布了一個答案。 根據此方法實現的文檔

對返回的 Map 的類型、可變性、可序列化性或線程安全性沒有任何保證。

因此,如果我們對Map接口的特定實現(例如HashMap感興趣,那么我們可以使用重載形式:

Map<String, Item> map2 =
                itemList.stream().collect(Collectors.toMap(Item::getKey, //key for map
                        Function.identity(),    // value for map
                        (o,n) -> o,             // merge function in case of conflict with keys
                        HashMap::new));         // map factory - we want HashMap and not any Map implementation

盡管使用Function.identity()i->i都很好,但似乎Function.identity()而不是i -> i根據此相關答案, i -> i可能會節省一些內存。

ListMap在概念上是不同的。 List是項目的有序集合。 項目可以包含重復項,並且項目可能沒有任何唯一標識符(鍵)的概念。 Map具有映射到鍵的值。 每個鍵只能指向一個值。

因此,根據您的List的項目,可能會也可能不會將其轉換為Map 您的List的項目是否沒有重復項? 每個項目都有唯一的鍵嗎? 如果是這樣,則可以將它們放在Map

還有一種使用 Google 庫中的Maps.uniqueIndex(...)的簡單方法

使用 java-8 流

Map<Integer, String> map = results.stream().collect(Collectors.toMap(e -> ((Integer) e[0]), e -> (String) e[1]));

通用方法

public static <K, V> Map<K, V> listAsMap(Collection<V> sourceList, ListToMapConverter<K, V> converter) {
    Map<K, V> newMap = new HashMap<K, V>();
    for (V item : sourceList) {
        newMap.put( converter.getKey(item), item );
    }
    return newMap;
}

public static interface ListToMapConverter<K, V> {
    public K getKey(V item);
}

如果沒有 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);
        }
    });
}};

根據您想要實現的目標,您會想到許多解決方案:

每個列表項都是鍵和值

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

列表元素有一些東西可以查找它們,也許是一個名稱:

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

列表元素有一些東西可以查找,並且不能保證它們是唯一的:使用 Googles 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 是從鍵到值的映射,而列表只是一系列元素,每個元素都有一個位置。 所以它們根本不能自動轉換。

您可以利用 Java 8 的流 API。

public class ListToMap {

  public static void main(String[] args) {
    List<User> items = Arrays.asList(new User("One"), new User("Two"), new User("Three"));

    Map<String, User> map = createHashMap(items);
    for(String key : map.keySet()) {
      System.out.println(key +" : "+map.get(key));
    }
  }

  public static Map<String, User> createHashMap(List<User> items) {
    Map<String, User> map = items.stream().collect(Collectors.toMap(User::getId, Function.identity()));
    return map;
  }
}

更多詳情請訪問: http : //codecramp.com/java-8-streams-api-convert-list-map/

就像已經說過的,在 java-8 中我們有 Collectors 的簡潔解決方案:

  list.stream().collect(
         groupingBy(Item::getKey)
        )

而且,您可以嵌套多個組,傳遞另一個 groupingBy 方法作為第二個參數:

  list.stream().collect(
         groupingBy(Item::getKey, groupingBy(Item::getOtherKey))
        )

這樣,我們就有了多級映射,像這樣: Map<key, Map<key, List<Item>>>

這是我為此目的編寫的一個小方法。 它使用來自 Apache Commons 的驗證。

隨意使用它。

/**
 * Converts a <code>List</code> to a map. One of the methods of the list is called to retrive
 * the value of the key to be used and the object itself from the list entry is used as the
 * objct. An empty <code>Map</code> is returned upon null input.
 * Reflection is used to retrieve the key from the object instance and method name passed in.
 *
 * @param <K> The type of the key to be used in the map
 * @param <V> The type of value to be used in the map and the type of the elements in the
 *            collection
 * @param coll The collection to be converted.
 * @param keyType The class of key
 * @param valueType The class of the value
 * @param keyMethodName The method name to call on each instance in the collection to retrieve
 *            the key
 * @return A map of key to value instances
 * @throws IllegalArgumentException if any of the other paremeters are invalid.
 */
public static <K, V> Map<K, V> asMap(final java.util.Collection<V> coll,
        final Class<K> keyType,
        final Class<V> valueType,
        final String keyMethodName) {

    final HashMap<K, V> map = new HashMap<K, V>();
    Method method = null;

    if (isEmpty(coll)) return map;
    notNull(keyType, Messages.getString(KEY_TYPE_NOT_NULL));
    notNull(valueType, Messages.getString(VALUE_TYPE_NOT_NULL));
    notEmpty(keyMethodName, Messages.getString(KEY_METHOD_NAME_NOT_NULL));

    try {
        // return the Method to invoke to get the key for the map
        method = valueType.getMethod(keyMethodName);
    }
    catch (final NoSuchMethodException e) {
        final String message =
            String.format(
                    Messages.getString(METHOD_NOT_FOUND),
                    keyMethodName,
                    valueType);
        e.fillInStackTrace();
        logger.error(message, e);
        throw new IllegalArgumentException(message, e);
    }
    try {
        for (final V value : coll) {

            Object object;
            object = method.invoke(value);
            @SuppressWarnings("unchecked")
            final K key = (K) object;
            map.put(key, value);
        }
    }
    catch (final Exception e) {
        final String message =
            String.format(
                    Messages.getString(METHOD_CALL_FAILED),
                    method,
                    valueType);
        e.fillInStackTrace();
        logger.error(message, e);
        throw new IllegalArgumentException(message, e);
    }
    return map;
}

將對象的List<?>轉換為Map<k, v>的 Java 8 示例:

List<Hosting> list = new ArrayList<>();
list.add(new Hosting(1, "liquidweb.com", new Date()));
list.add(new Hosting(2, "linode.com", new Date()));
list.add(new Hosting(3, "digitalocean.com", new Date()));

//example 1
Map<Integer, String> result1 = list.stream().collect(
    Collectors.toMap(Hosting::getId, Hosting::getName));

System.out.println("Result 1 : " + result1);

//example 2
Map<Integer, String> result2 = list.stream().collect(
    Collectors.toMap(x -> x.getId(), x -> x.getName()));

代碼復制自:
https://www.mkyong.com/java8/java-8-convert-list-to-map/

Apache Commons MapUtils.populateMap

如果您不使用 Java 8 並且出於某種原因不想使用顯式循環,請嘗試來自 Apache Commons 的MapUtils.populateMap

MapUtils.populateMap

假設您有一個Pair列表。

List<ImmutablePair<String, String>> pairs = ImmutableList.of(
    new ImmutablePair<>("A", "aaa"),
    new ImmutablePair<>("B", "bbb")
);

而你現在想要一個地圖的Pair的關鍵Pair對象。

Map<String, Pair<String, String>> map = new HashMap<>();
MapUtils.populateMap(map, pairs, new Transformer<Pair<String, String>, String>() {

  @Override
  public String transform(Pair<String, String> input) {
    return input.getKey();
  }
});

System.out.println(map);

給出輸出:

{A=(A,aaa), B=(B,bbb)}

話雖如此, for循環可能更容易理解。 (下面給出了相同的輸出):

Map<String, Pair<String, String>> map = new HashMap<>();
for (Pair<String, String> pair : pairs) {
  map.put(pair.getKey(), pair);
}
System.out.println(map);

如果使用Kotlin,有一個例子:

listOf("one", "two").mapIndexed { i, it -> i to it }.toMap()
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());
    }
  }
}

我喜歡 Kango_V 的回答,但我認為它太復雜了。 我認為這更簡單——也許太簡單了。 如果願意,您可以將 String 替換為 Generic 標記,並使其適用於任何 Key 類型。

public static <E> Map<String, E> convertListToMap(Collection<E> sourceList, ListToMapConverterInterface<E> converterInterface) {
    Map<String, E> newMap = new HashMap<String, E>();
    for( E item : sourceList ) {
        newMap.put( converterInterface.getKeyForItem( item ), item );
    }
    return newMap;
}

public interface ListToMapConverterInterface<E> {
    public String getKeyForItem(E item);
}

像這樣使用:

        Map<String, PricingPlanAttribute> pricingPlanAttributeMap = convertListToMap( pricingPlanAttributeList,
                new ListToMapConverterInterface<PricingPlanAttribute>() {

                    @Override
                    public String getKeyForItem(PricingPlanAttribute item) {
                        return item.getFullName();
                    }
                } );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM