Java : AbstractCollection - API使用例
AbstractCollection (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
必要最低限の独自実装をした例です。
class MyCollection extends AbstractCollection<String> {
private final String[] values;
MyCollection(String[] values) {
this.values = Objects.requireNonNull(values);
}
@Override
public Iterator<String> iterator() {
return new Iterator<>() {
private int index;
@Override
public boolean hasNext() {
return values.length != index;
}
@Override
public String next() {
final var value = values[index];
index++;
return value;
}
};
}
@Override
public int size() {
return values.length;
}
}
final var collection = new MyCollection(new String[]{"aaa", "bbb", "ccc"});
System.out.println(collection); // [aaa, bbb, ccc]
System.out.println(collection.size()); // 3
System.out.println(collection.isEmpty()); // false
System.out.println("-- values --");
for (final var value : collection) {
System.out.println(value);
}
// 結果
// ↓
//-- values --
//aaa
//bbb
//ccc
コンストラクタ
AbstractCollection ()
class MyCollection extends AbstractCollection<String> {
private final String[] values;
MyCollection(String[] values) {
this.values = Objects.requireNonNull(values);
}
@Override
public Iterator<String> iterator() {
return new Iterator<>() {
private int index;
@Override
public boolean hasNext() {
return values.length != index;
}
@Override
public String next() {
final var value = values[index];
index++;
return value;
}
};
}
@Override
public int size() {
return values.length;
}
}
final var collection = new MyCollection(new String[]{"aaa", "bbb", "ccc"});
System.out.println(collection); // [aaa, bbb, ccc]
System.out.println(collection.size()); // 3
System.out.println(collection.isEmpty()); // false
System.out.println("-- values --");
for (final var value : collection) {
System.out.println(value);
}
// 結果
// ↓
//-- values --
//aaa
//bbb
//ccc
メソッド
boolean add (E e)
final AbstractCollection<String> collection = new ArrayList<>();
System.out.println(collection.add("aaa")); // true
System.out.println(collection.add("bbb")); // true
System.out.println(collection.add("bbb")); // true
System.out.println(collection); // [aaa, bbb, bbb]
final AbstractCollection<String> collection = new HashSet<>();
System.out.println(collection.add("aaa")); // true
System.out.println(collection.add("bbb")); // true
System.out.println(collection.add("bbb")); // false
System.out.println(collection); // [aaa, bbb]
boolean addAll (Collection<? extends E> c)
final AbstractCollection<String> collection = new ArrayList<>();
final var list1 = List.of("aaa", "bbb");
final var list2 = List.of("bbb", "ccc");
final var list3 = List.of("ccc");
System.out.println(collection.addAll(list1)); // true
System.out.println(collection); // [aaa, bbb]
System.out.println(collection.addAll(list2)); // true
System.out.println(collection); // [aaa, bbb, bbb, ccc]
System.out.println(collection.addAll(list3)); // true
System.out.println(collection); // [aaa, bbb, bbb, ccc, ccc]
final AbstractCollection<String> collection = new HashSet<>();
final var set1 = Set.of("aaa", "bbb");
final var set2 = Set.of("bbb", "ccc");
final var set3 = Set.of("ccc");
System.out.println(collection.addAll(set1)); // true
System.out.println(collection); // [aaa, bbb]
System.out.println(collection.addAll(set2)); // true
System.out.println(collection); // [aaa, ccc, bbb]
System.out.println(collection.addAll(set3)); // false
System.out.println(collection); // [aaa, ccc, bbb]
void clear ()
final AbstractCollection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
System.out.println(collection); // [aaa, bbb, ccc]
collection.clear();
System.out.println(collection); // []
System.out.println(collection.isEmpty()); // true
boolean contains (Object o)
final AbstractCollection<String> collection = new ArrayList<>();
Collections.addAll(collection, "aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]
System.out.println(collection.contains("aaa")); // true
System.out.println(collection.contains("bbb")); // true
System.out.println(collection.contains("XXX")); // false
boolean containsAll (Collection<?> c)
final AbstractCollection<String> collection = new ArrayList<>();
Collections.addAll(collection, "aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]
final var list1 = List.of("aaa", "bbb");
System.out.println(collection.containsAll(list1)); // true
final var list2 = List.of("bbb", "XXX");
System.out.println(collection.containsAll(list2)); // false
final var list3 = List.of("XXX", "YYY");
System.out.println(collection.containsAll(list3)); // false
boolean isEmpty ()
final AbstractCollection<String> collection = new ArrayList<>();
System.out.println(collection); // []
System.out.println(collection.size()); // 0
System.out.println(collection.isEmpty()); // true
collection.add("aaa");
System.out.println(collection); // [aaa]
System.out.println(collection.size()); // 1
System.out.println(collection.isEmpty()); // false
collection.add("bbb");
System.out.println(collection); // [aaa, bbb]
System.out.println(collection.size()); // 2
System.out.println(collection.isEmpty()); // false
abstract Iterator<E> iterator ()
final AbstractCollection<String> collection = new ArrayList<>();
Collections.addAll(collection, "aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]
final var iterator = collection.iterator();
iterator.forEachRemaining(System.out::println);
// 結果
// ↓
//aaa
//bbb
//ccc
boolean remove (Object o)
final AbstractCollection<String> collection = new ArrayList<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
System.out.println(collection); // [aaa, bbb, ccc]
System.out.println(collection.remove("aaa")); // true
System.out.println(collection); // [bbb, ccc]
System.out.println(collection.remove("bbb")); // true
System.out.println(collection); // [ccc]
System.out.println(collection.remove("XXX")); // false
System.out.println(collection); // [ccc]
boolean removeAll (Collection<?> c)
final var src = List.of("aaa", "bbb", "ccc");
{
final AbstractCollection<String> collection = new ArrayList<>(src);
System.out.println(collection); // [aaa, bbb, ccc]
final var list = List.of("aaa", "bbb");
System.out.println(collection.removeAll(list)); // true
System.out.println(collection); // [ccc]
}
{
final AbstractCollection<String> collection = new ArrayList<>(src);
System.out.println(collection); // [aaa, bbb, ccc]
final var list = List.of("XXX", "YYY");
System.out.println(collection.removeAll(list)); // false
System.out.println(collection); // [aaa, bbb, ccc]
}
{
final AbstractCollection<String> collection = new ArrayList<>(src);
System.out.println(collection); // [aaa, bbb, ccc]
final var list = List.of("aaa", "bbb", "ccc", "XXX");
System.out.println(collection.removeAll(list)); // true
System.out.println(collection); // []
}
boolean retainAll (Collection<?> c)
final var src = List.of("aaa", "bbb", "ccc");
{
final AbstractCollection<String> collection = new ArrayList<>(src);
System.out.println(collection); // [aaa, bbb, ccc]
final var list = List.of("aaa", "bbb");
System.out.println(collection.retainAll(list)); // true
System.out.println(collection); // [aaa, bbb]
}
{
final AbstractCollection<String> collection = new ArrayList<>(src);
System.out.println(collection); // [aaa, bbb, ccc]
final var list = List.of("XXX", "YYY");
System.out.println(collection.retainAll(list)); // true
System.out.println(collection); // []
}
{
final AbstractCollection<String> collection = new ArrayList<>(src);
System.out.println(collection); // [aaa, bbb, ccc]
final var list = List.of("aaa", "bbb", "ccc", "XXX");
System.out.println(collection.retainAll(list)); // false
System.out.println(collection); // [aaa, bbb, ccc]
}
Object[] toArray ()
final AbstractCollection<String> collection = new ArrayList<>();
Collections.addAll(collection, "aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]
final Object[] ret = collection.toArray();
System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]
<T> T[] toArray (T[] a)
final AbstractCollection<String> collection = new ArrayList<>();
Collections.addAll(collection, "aaa", "bbb", "ccc");
System.out.println(collection); // [aaa, bbb, ccc]
{
final String[] ret = collection.toArray(new String[0]);
System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]
}
{
final String[] array = new String[collection.size()];
System.out.println(Arrays.toString(array)); // [null, null, null]
final var ret = collection.toArray(array);
System.out.println(Arrays.toString(ret)); // [aaa, bbb, ccc]
System.out.println(Arrays.toString(array)); // [aaa, bbb, ccc]
}
String toString ()
final AbstractCollection<String> collection = new ArrayList<>();
final var str1 = collection.toString();
System.out.println(str1); // []
collection.add("aaa");
final var str2 = collection.toString();
System.out.println(str2); // [aaa]
collection.add("bbb");
collection.add("ccc");
final var str3 = collection.toString();
System.out.println(str3); // [aaa, bbb, ccc]
Collectionで宣言されたメソッド
equals, hashCode, parallelStream, removeIf, size, spliterator, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Iterableで宣言されたメソッド
forEach
「Java API 使用例 : Iterable」をご参照ください。
関連記事
- List(リスト)の基本
- Map(マップ)の基本
- Set(セット)の基本
- List の初期化方法いろいろ
- Map の初期化方法いろいろ
- Set の初期化方法いろいろ
- 配列 vs. List
- 配列からListへの変換
- List から配列への変換
- 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