Java : Set (セット) - API使用例
Set (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
Set は、Java コレクション・フレームワークの1つです。
重複のない複数の要素を管理します。
大量の要素を追加しても、基本操作である add や remove, contains などのパフォーマンス低下が起こらないのが特徴です。
関連記事:Set(セット)の基本
final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("X")); // false
メソッド
boolean add (E e)
final Set<String> set = new HashSet<>();
System.out.println(set); // []
System.out.println(set.add("a")); // true
System.out.println(set); // [a]
System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]
System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]
System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]
boolean addAll (Collection<? extends E> c)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
System.out.println(set); // [a, b, c, d, e, f]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
System.out.println(set); // [a, b, c, e, f]
}
void clear ()
final Set<String> set = new HashSet<>();
System.out.println(set); // []
set.add("a");
System.out.println(set); // [a]
set.add("b");
System.out.println(set); // [a, b]
set.clear();
System.out.println(set); // []
boolean contains (Object o)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true
System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false
boolean containsAll (Collection<?> c)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [c, b, a]
System.out.println(set.containsAll(Set.<String>of())); // true
System.out.println(set.containsAll(Set.of("a"))); // true
System.out.println(set.containsAll(Set.of("a", "b"))); // true
System.out.println(set.containsAll(Set.of("b", "a"))); // true
System.out.println(set.containsAll(Set.of("c", "b", "a"))); // true
System.out.println(set.containsAll(Set.of("X"))); // false
System.out.println(set.containsAll(Set.of("a", "X"))); // false
System.out.println(set.containsAll(Set.of("a", "b", "c", "X"))); // false
static <E> Set<E> copyOf (Collection<? extends E> coll)
final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var copiedSet = Set.copyOf(set);
System.out.println(copiedSet); // [a, b, c]
set.add("d");
System.out.println(set); // [a, b, c, d]
System.out.println(copiedSet); // [a, b, c]
try {
copiedSet.add("d");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
boolean equals (Object o)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
System.out.println(set.equals(Set.of("a"))); // false
System.out.println(set.equals(Set.of("a", "b"))); // false
System.out.println(set.equals(Set.of("a", "b", "c"))); // true
System.out.println(set.equals(Set.of("c", "b", "a"))); // true
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
final Set<String> hashSet = new HashSet<>(set);
System.out.println(hashSet); // [a, b, c]
System.out.println(set.equals(hashSet)); // true
final Set<String> treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [a, b, c]
System.out.println(set.equals(treeSet)); // true
final var list = new ArrayList<>(set);
System.out.println(list); // [a, b, c]
System.out.println(set.equals(list)); // false
int hashCode ()
final var set1 = Set.of("a", "b", "c");
System.out.println(set1.hashCode()); // 294
final var set2 = Set.of(1, 2, 3);
System.out.println(set2.hashCode()); // 6
final Set<String> set1 = new LinkedHashSet<>();
set1.add("a");
set1.add("b");
set1.add("c");
System.out.println(set1); // [a, b, c]
System.out.println(set1.hashCode()); // 294
final Set<String> set2 = new LinkedHashSet<>();
set2.add("c");
set2.add("b");
set2.add("a");
System.out.println(set2); // [c, b, a]
System.out.println(set2.hashCode()); // 294
boolean isEmpty ()
final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false
set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
Iterator<E> iterator ()
final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]
final var it = set.iterator();
System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);
// 結果
// ↓
//-- forEachRemaining --
//aaa
//ccc
//bbb
static <E> Set<E> of ()
final var set = Set.of();
System.out.println(set); // []
System.out.println(set.size()); // 0
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1)
final var set = Set.of("a");
System.out.println(set); // [a]
System.out.println(set.size()); // 1
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E... elements)
final var set = Set.of(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n"
);
System.out.println(set); // [c, d, e, f, g, h, i, j, k, l, m, n, a, b]
System.out.println(set.size()); // 14
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2)
final var set = Set.of("a", "b");
System.out.println(set); // [b, a]
System.out.println(set.size()); // 2
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [b, a, c]
System.out.println(set.size()); // 3
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4)
final var set = Set.of("a", "b", "c", "d");
System.out.println(set); // [b, a, d, c]
System.out.println(set.size()); // 4
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5)
final var set = Set.of("a", "b", "c", "d", "e");
System.out.println(set); // [e, d, c, b, a]
System.out.println(set.size()); // 5
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6)
final var set = Set.of("a", "b", "c", "d", "e", "f");
System.out.println(set); // [f, e, d, c, b, a]
System.out.println(set.size()); // 6
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g");
System.out.println(set); // [g, f, e, d, c, b, a]
System.out.println(set.size()); // 7
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h");
System.out.println(set); // [h, g, f, e, d, c, b, a]
System.out.println(set.size()); // 8
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i");
System.out.println(set); // [a, i, h, g, f, e, d, c, b]
System.out.println(set.size()); // 9
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
System.out.println(set); // [j, i, h, g, f, e, d, c, b, a]
System.out.println(set.size()); // 10
try {
set.add("z");
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// 結果
// ↓
//UnsupportedOperationException!
boolean remove (Object o)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("")); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("a")); // true
System.out.println(set); // [b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("b")); // true
System.out.println(set); // [a, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.remove("X")); // false
System.out.println(set); // [a, b, c]
}
boolean removeAll (Collection<?> c)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.<String>of())); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a"))); // true
System.out.println(set); // [b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "b"))); // true
System.out.println(set); // [c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("b", "a"))); // true
System.out.println(set); // [c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "b", "c"))); // true
System.out.println(set); // []
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("X", "Y"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of("a", "X"))); // true
System.out.println(set); // [b, c]
}
boolean retainAll (Collection<?> c)
final var src = Set.of("a", "b", "c");
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.<String>of())); // true
System.out.println(set); // []
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a"))); // true
System.out.println(set); // [a]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b"))); // true
System.out.println(set); // [a, b]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("b", "a"))); // true
System.out.println(set); // [a, b]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b", "c"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("a", "b", "c", "X", "Y"))); // false
System.out.println(set); // [a, b, c]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "a", "b", "Y"))); // true
System.out.println(set); // [a, b]
}
{
final Set<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.retainAll(Set.of("X", "Y"))); // true
System.out.println(set); // []
}
int size ()
final var set1 = Set.of();
System.out.println(set1); // []
System.out.println(set1.size()); // 0
final var set2 = Set.of("a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3
default Spliterator<E> spliterator ()
final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(set); // [bbb, ccc, aaa]
final var spliterator = set.spliterator();
System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(System.out::println);
// 結果
// ↓
//-- forEachRemaining --
//bbb
//ccc
//aaa
Object[] toArray ()
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
final Object[] array = set.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]
<T> T[] toArray (T[] a)
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
final String[] ret = set.toArray(new String[0]);
System.out.println(Arrays.toString(ret)); // [a, b, c]
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]
{
final String[] array = new String[3];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
final String[] array = new String[5];
System.out.println(Arrays.toString(array)); // [null, null, null, null, null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
final String[] array = new String[1];
System.out.println(Arrays.toString(array)); // [null]
final var ret = set.toArray(array);
System.out.println(Arrays.toString(array)); // [null]
System.out.println(Arrays.toString(ret)); // [a, b, c]
}
Collectionで宣言されたメソッド
parallelStream, removeIf, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Iterableで宣言されたメソッド
forEach
「Java API 使用例 : Iterable」をご参照ください。
関連記事
- API 使用例
- Collection (コレクション)
- Collections (コレクション操作)
- Comparable
- Comparator
- Iterator
- List (リスト)
- Map (マップ)
- Map.Entry (キーと値のペア)
- Queue (キュー)
- AbstractQueue
- ArrayBlockingQueue (ブロッキング・配列キュー)
- ArrayDeque (両端キュー)
- BlockingDeque (ブロッキング・両端キュー)
- BlockingQueue (ブロッキング・キュー)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- Deque (両端キュー)
- LinkedBlockingDeque (ブロッキング・リンク両端キュー)
- LinkedBlockingQueue (ブロッキング・リンクキュー)
- LinkedList (二重リンク・リスト)
- PriorityBlockingQueue (ブロッキング・優先度付きキュー)
- PriorityQueue (優先度付きキュー)
- RandomAccess
- Set (セット)
- Spliterator
- Iterable
- Iterator
- Spliterator