突っ走り書き

見せるほどのものでは..

Setsを使って初期化


// 今まではこれで初期化してました
Set<Integer> s = new HashSet<>(Arrays.asList(1, 2));

// Guava を使えば,少しだけ短くなります
Set<Integer> s = Sets.newHashSet(1, 2);

// 空集合の初期化はCollectionsを使うほうが好き
// 意図が伝わる気がするので.
Set<Integer> s = Collections.emptySet();
Set<Integer> s = Collections.EMPTY_SET;

// 要素が1つだけの集合もCollectionsを使うのが好き
// 意図が伝わる気がするので.
Set<Integer> s = Collections.singleton(1);