Java Reference
In-Depth Information
«interface»
java.util.Collection
<
E
>
«interface»
java.util.Set
<
E
>
java.util.AbstractSet
<
E
>
«interface»
java.util.SortedSet<E>
+
first(): E
+
last(): E
+
headSet(toElement: E): SortedSet
<
E
>
+
tailSet(fromElement: E): SortedSet
<
E
>
java.util.HashSet<E>
+HashSet()
+HashSet(c: Collection<? extends E>)
+HashSet(initialCapacity: int)
+HashSet(initialCapacity: int, loadFactor: float)
«interface»
java.util.NavigableSet<E>
java.util.LinkedHashSet<E>
+
pollFirst(): E
+
pollLast(): E
+
lower(e: E): E
+
higher(e: E):E
+
floor(e: E): E
+
ceiling(e: E): E
+LinkedHashSet()
+LinkedHashSet(c: Collection<? extends E>)
+LinkedHashSet(initialCapacity: int)
+LinkedHashSet(initialCapacity: int, loadFactor: float)
java.util.TreeSet<E>
+TreeSet()
+TreeSet(c: Collection<? extends E>)
+TreeSet(comparator: Comparator<?
super E>)
+TreeSet(s: SortedSet<E>)
F
IGURE
21.1
The Java Collections Framework provides three concrete set classes.
Listing 21.1 gives a program that creates a hash set to store strings and uses a foreach loop
to traverse the elements in the set.
L
ISTING
21.1
TestHashSet.java
1
import
java.util.*;
2
3
public class
TestHashSet {
4
public static void
main(String[] args) {
5
// Create a hash set
6
Set<String> set =
new
HashSet<>();
create a set
7
8
// Add strings to the set
9 set.add(
"London"
);
10 set.add(
"Paris"
);
11 set.add(
"New York"
);
12 set.add(
"San Francisco"
);
13 set.add(
"Beijing"
);
add element
Search WWH ::
Custom Search