■ Mapの初期化
// Case2 : 匿名内部クラスを利用した方法(★以下「注意:匿名内部クラスを利用した方法について」を参照★) Map<String, String> maps = new HashMap<String, String>() { { put("X001", "first"); put("X002", "second"); put("X003", "third"); } };http://pgnote.net/?p=32
注意:匿名内部クラスを利用した方法について
* 『パターン3 : 匿名内部クラスを利用した方法』と『パターン1』/『パターン2』は 上記の例だけ言えば、得られる結果は同じであるが、インスタンス生成で違いがある。 => 詳細は以下の関連記事を参照のことhttps://blogs.yahoo.co.jp/dk521123/37174760.html
■ 読み取り専用(Readonly)のMapにするには
* 「Collections.unmodifiableMap」「Collections.unmodifiableSortedMap」を使用するMap<Integer, String> readonlyMaps = Collections.unmodifiableMap(maps);http://docs.oracle.com/javase/jp/6/api/java/util/Collections.html
■ マップの定数化
* finalを付けても要素の追加や変更はできてしまうので、Collections.unmodifiableMap()を付けるimport java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; private static final Map<String, String> PRODUCT_MAP; static { PRODUCT_MAP = Collections.unmodifiableMap(new ConcurrentHashMap<String, String>() { private static final long serialVersionUID = -5026360498609632073L; { put("X0001", "Apple"); put("X0002", "Orange"); put("X0003", "Melon"); } }); }http://sakuramochi702.hatenablog.com/entry/2013/10/08/114958
■ Mapをループさせる
キー・バリューどっちも使いたい場合
http://d.hatena.ne.jp/yamama_lanchester/20080530/1212122258より for(Map.Entry<String, String> map : maps.entrySet()) { System.out.println(map.getKey() + " : " + map.getValue()); }
キー・バリューどっちかを使いたい場合
http://yonchu.hatenablog.com/entry/20110216/1297857882for (String key : map.keySet()) { System.out.println("キー : " + key); } for (String value : map.values()) { System.out.println("バリュー : " + value); }
From Java1.8
map.forEach((key, value) -> { System.out.println(key + " : " + value); });ただし制限あり:ラムダ式内からクロージャ変数に対して変更を行うことはできない
String returnValue = targetValueInKatakana; map.forEach((key, value) -> { returnValue = returnValue.replaceAll(key, value); // コンパイルエラー });
■ Map内のMax値の要素を取得する
Entry<String, Integer> maxEntry = Collections.max(maps.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue());http://stackoverflow.com/questions/5911174/finding-key-associated-with-max-value-in-a-java-map
■ Mapの最初と最後の要素を取得する
Set<Entry<String, Integer>> mapValues = maps.entrySet(); Entry<String, Integer>[] entryArrays = new Entry[mapValues.size()]; mapValues.toArray(entryArrays); // 最初の要素 Entry<String, Integer> firstEntry = entryArrays[0]; // 最後の要素 Entry<String, Integer> lastEntry = entryArrays[mapValues.size() - 1];http://blogs.candoerz.com/question/148143/java-linkedhashmap-get-first-or-last-entry.aspx
サンプル
Map内のMax値の要素を取得するMapの最初と最後の要素を取得する
import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class Main { @SuppressWarnings("unchecked") public static void main(String[] args) { Map<String, Integer> maps = new HashMap<String, Integer>() { private static final long serialVersionUID = -1L; { put("X001", 76); put("X002", 96); put("X003", 56); put("X004", 100); put("X005", 28); put("X006", 87); } }; // Max System.out.println("********* Max ***********"); Entry<String, Integer> maxEntry = Collections.max(maps.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue()); System.out.println(maxEntry.getKey() + " " + maxEntry.getValue()); // First/Last System.out.println("********* First/Last ***********"); final Set<Entry<String, Integer>> mapValues = maps.entrySet(); final Entry<String, Integer>[] entryArrays = new Entry[mapValues.size()]; mapValues.toArray(entryArrays); Entry<String, Integer> firstEntry = entryArrays[0]; System.out.println("First Key : " + firstEntry.getKey()); System.out.println("First Value : " + firstEntry.getValue()); Entry<String, Integer> lastEntry = entryArrays[mapValues.size() - 1]; System.out.println("Last Key : " + lastEntry.getKey()); System.out.println("Last Value : " + lastEntry.getValue()); } }出力結果
********* Max *********** X004 100 ********* First/Last *********** First Key : X001 First Value : 76 Last Key : X006 Last Value : 87
■ Mapを変換する
Map => List
import java.util.ArrayList; import java.util.List; import java.util.Map; public class MapUtil { public static <T, V> List<V> toListValues(Map<T, V> map) { return new ArrayList<V>(map.values()); } public static <T, V> List<T> toListKeyValues(Map<T, V> map) { return new ArrayList<T>(map.keySet()); } }http://the-pleiades.hateblo.jp/entry/2014/02/14/003533