The HashSet collection class:
Sometimes you want to store data in such a way that you do not care in which order it is stored, but you do want to be able to find it again quickly. Arrays and lists maintain data in a certain order, and if you do not know exactly where the data is then searching for it can be rather slow. The hash table, and its Java implementation by class HashMap, which we discussed earlier, is just right for this sort of application.
The mathematical idea of a set is a collection of unique items, with operations to add and remove items and to test whether a given item is in the set. In this case, unique items means that the set holds no duplicate items.
The class HashSet is a particular implementation of the set concept. It implements the Set interface, making use of the same underlying mechanisms found in the HashMap class for adding and quickly retrieving items (in fact, it uses a HashMap object). This is useful because the order of items in a set is not important, only the fact that these items are present (or members of the set, in mathematical terms). Table 10 (overleaf) summarizes some of the methods of the HashSet class.
Table Some methods of the HashSet class
The example code below illustrates simple use of the methods of the HashSet class:
// create a new set with no members
// note use of variable of interface type 'Set'
Set<Integer> s = new HashSet<Integer>();
if (s.isEmpty())
{
System.out.println("Set initially empty");
}
s.add(23); s.add(25); s.add(11);
// three integers added
System.out.println("The set has " + s.size() + " elements");
s.remove(23);
// there are now only two integers in the set
System.out.println("The set now has " + s.size() +
"elements");
if (s.contains(25))
{
System.out.println("25 is in the set");
}
s.clear(); // delete all items from the set
if (s.contains(25))
{
System.out.println("Something wrong!");
}
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The HashSet collection class homework and assignments? Live The HashSet collection class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The HashSet collection class homework help, java assignment help and The HashSet collection class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours