Skip to content

Instantly share code, notes, and snippets.

@kyonmm
Created December 7, 2011 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyonmm/1443730 to your computer and use it in GitHub Desktop.
Save kyonmm/1443730 to your computer and use it in GitHub Desktop.
JavaのListリテラルとMapリテラル
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