In addition to the List and Map interface, the third interface in the Java Collection Framework is Set. A Set is an unordered collection of objects with no duplicates. This interface models, as expected, the mathematical set. Two classes that implement the Set interface in JCF are TreeSet and HashSet. Here’s a simple example of using Set:
Set<String> = new HashSet <String>();
set.add("ape");
set.add("bee");
set.add("ape"); //duplicate, so it won't be added
set.add("cat");
set.remove("bee");
set.remove("dog"); //not in the set, nothing happens
System.out.println("Set = " + set);
The output from the code will be
To access the individual elements of a set, call the iterator method in the manner identical to the one we used for the List interface.
Rewrite the AddressBook class by using the HashSet instead of an array to maintain a collection of Person object.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.