質問

最近、私は同僚と会話します。変換する最適な方法は何ですか ListMap Javaで、そしてそうすることに具体的な利点がある場合。

最適な変換アプローチを知りたいのですが、誰かが私を導くことができれば本当に感謝しています。

これは良いアプローチですか:

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);

もちろん、各アイテムにはを持っていると仮定します getKey() 適切なタイプのキーを返す方法。

他のヒント

, 、あなたはこれを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値の場合、Modulo 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)

それが役に立てば幸い! :)

この質問が複製として閉じられていない場合に備えて、 正しい答えは、Googleコレクションを使用することです:

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

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);

Java 8を使用すると、次のことができます。

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

Value 使用するすべてのオブジェクトにすることができます。

a ListMap 概念的に異なっています。 a List 注文されたアイテムのコレクションです。アイテムには複製が含まれている場合があり、アイテムには一意の識別子(キー)の概念がない場合があります。 a Map キーにマッピングされた値があります。各キーは1つの値のみを指すことができます。

したがって、あなたに依存します Listのアイテム、それをに変換することが可能かもしれないし、そうでないかもしれない Map. 。あなたはしますか Listアイテムには複製がありませんか?各アイテムには一意のキーがありますか?もしそうなら、それらをに置くことが可能です Map.

これを使用する簡単な方法もあります maps.uniqueIndex(...) Googleから ライブラリ

アレクシスはすでに答えを投稿しています Java 8 メソッドを使用します toMap(keyMapper, valueMapper). 。に従って doc この方法の実装については:

返されたマップのタイプ、可変性、シリアル化可能性、またはスレッドセーフティに関する保証はありません。

したがって、私たちがの特定の実装に興味がある場合に備えて Map インターフェースEG 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 これに従ってメモリを保存する可能性があります 答え.

ユニバーサル方法

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がなければ、1行のコモンズコレクションと閉鎖クラスでこれを行うことができます

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

リスト要素にはそれらを調べるものがあり、それらがユニークであるという保証はありません:グーグルを使用する マルチマップ

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

すべての要素にキーとしての位置を与える:

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

...

それは本当にあなたが痛むものに依存します。

例からわかるように、マップはキーから値までのマッピングであり、リストはそれぞれ位置を持つ一連の要素です。したがって、それらは単に自動的に変換可能ではありません。

これが私がまさにこの目的のために書いた小さな方法です。 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;
}

Java 8のStreams 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にはコレクターによる簡潔な解決策があります。

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

また、複数のグループを2番目のパラメーターとして他のGroupingByメソッドを渡す複数のグループをネストすることができます。

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

このようにして、このようなマルチレベルマップがあります。 Map<key, Map<key, List<Item>>>

aを変換するためのJava 8例 List<?> aへのオブジェクトの Map<k, v>:

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/

私はKango_Vの答えが好きですが、複雑すぎると思います。これは簡単だと思います - たぶん単純すぎるでしょう。傾斜した場合、文字列を一般的なマーカーに置き換えて、キータイプで機能させることができます。

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

Apache Commons Maputils.populatemap

Java 8を使用せず、何らかの理由で明示的なループを使用したくない場合は、試してみてください MapUtils.populateMap Apache Commonsから。

maputils.populatemap

のリストがあるとします Pairs。

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)}

そうは言っても、a for ループは理解しやすいかもしれません。 (これは以下で同じ出力を与えます):

Map<String, Pair<String, String>> map = new HashMap<>();
for (Pair<String, String> pair : pairs) {
  map.put(pair.getKey(), pair);
}
System.out.println(map);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top