Created
December 7, 2011 17:37
-
-
Save kyonmm/1443730 to your computer and use it in GitHub Desktop.
JavaのListリテラルとMapリテラル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String... a){ | |
HashMap<String, Integer> m = map($("hoge1",1),$("hoge2",2)); | |
System.out.println(m); | |
ArrayList<String> l = list("hoge","fuga"); | |
System.out.println(l); | |
} | |
public static <K, V>HashMap<K, V> map(Map.Entry<K, V>... e){ | |
HashMap<K, V> m = new HashMap<K, V>(); | |
for(Map.Entry<K, V> each : e){ | |
m.put(each.getKey(),each.getValue()); | |
} | |
return m; | |
} | |
public static <K, V>Map.Entry<K, V>$(K k, V v){ | |
return new AbstractMap.SimpleEntry<K, V>(k, v); | |
} | |
public static <T>ArrayList<T>list(T... t){ | |
ArrayList<T> l = new ArrayList<T>(); | |
for(T e : t){ | |
l.add(e); | |
} | |
return l; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment