Java : AbstractSet with Examples
AbstractSet (Java SE 18 & JDK 18) API Examples.
You will find code examples on most AbstractSet methods.
Summary
class MySet extends AbstractSet<String> {
private final String[] elements;
MySet(String e1, String e2, String e3) {
if (Objects.equals(e1, e2) || Objects.equals(e1, e3) || Objects.equals(e2, e3)) {
throw new IllegalArgumentException("duplicate element");
}
elements = new String[]{e1, e2, e3};
}
@Override
public Iterator<String> iterator() {
return new Iterator<>() {
private int index;
@Override
public boolean hasNext() {
return elements.length != index;
}
@Override
public String next() {
final var value = elements[index];
index++;
return value;
}
};
}
@Override
public int size() {
return elements.length;
}
}
final var set = new MySet("aaa", "bbb", "ccc");
System.out.println(set); // [aaa, bbb, ccc]
System.out.println(set.size()); // 3
System.out.println("-- elements --");
for (final var element : set) {
System.out.println(element);
}
// Result
// ↓
//-- elements --
//aaa
//bbb
//ccc
Constructors
AbstractSet ()
For invocation by subclass constructors, typically implicit. Therefore, the code example is omitted.
Methods
boolean equals (Object o)
final AbstractSet<String> set = new HashSet<>();
Collections.addAll(set, "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 AbstractSet<String> set = new HashSet<>();
Collections.addAll(set, "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 AbstractSet<String> set1 = new HashSet<>();
Collections.addAll(set1, "a", "b", "c");
System.out.println(set1.hashCode()); // 294
final AbstractSet<Integer> set2 = new HashSet<>();
Collections.addAll(set2, 1, 2, 3);
System.out.println(set2.hashCode()); // 6
final AbstractSet<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 AbstractSet<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 removeAll (Collection<?> c)
final var src = Set.of("a", "b", "c");
{
final AbstractSet<String> set = new HashSet<>(src);
System.out.println(set); // [a, b, c]
System.out.println(set.removeAll(Set.of())); // false
System.out.println(set); // [a, b, c]
}
{
final AbstractSet<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 AbstractSet<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 AbstractSet<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 AbstractSet<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 AbstractSet<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 AbstractSet<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]
}
Methods declared in AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, toArray, toArray, toString
Please see the link below.
Methods declared in Collection
Methods declared in Iterable
Methods declared in Set
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, size, spliterator, toArray, toArray
Please see the link below.
Related posts
- API Examples