Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akishin/401951 to your computer and use it in GitHub Desktop.
Save akishin/401951 to your computer and use it in GitHub Desktop.
// java.util.List の初期化を一行で書く
List<String> list = new ArrayList<String>() {{add("a"); add("b"); add("c");}};
// 変更不可能な List で良い場合は
List list = Arrays.asList("a", "b", "c");
// Arrays.asList をジェネリックスを使って書くと
List<Integer> list = Arrays.<Integer>asList(1, 2, 3);
// asList を使いつつ、追加可能な List を作るには、冗長だが以下のようにする
List<Integer> list = new ArrayList<Integer>(Arrays.<Integer>asList(1, 2, 3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment