In java
Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface. The important points about Java HashSet class are: HashSet stores the elements by using a mechanism called hashing. HashSet contains unique elements only. Write a Java program totesta Hash set is empty or not a. Use add() method to add values in the hash set b. Print the Hash set with inserted values c. Check to see if it is empty or not, print the result d. Remove all the elements from the Hash set, print a message after removing Write a Java program to compare two Hashsets. Write a Java program to get the number of elements in a Hashset. Write a Java program to clone a Hash set to another Hash set. Use iterator with both for each and while loop, print out the result. You can complete all the above 4 in one single program. HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap
or HashMap. This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits null null values and null key). Write a Java program to test if a map contains a mapping for the specified key a. Add values in the Hash map b. Print the map c. Check for a specified key print result Write a Java program to copy all of the mappings from the specified map to another map. Use iterator with both for each and while loop, print out the key with the matching value. You can complete all the above 2 in one single program The following are the important differences between HashMap and HashSet. HashMap HashSet No. 1 Implementation Hashmap is the implementation of Map interface. Hashset on other hand is the implementation of set interface. 2 Internal Hashmap internally do not Hashset internally uses implementation implements hashset or any set for its Hashmap for its implementation implementation Storage of elements 3 HashMap Stores elements in form of HashSet stores only objects no key-value pair i.e each element has such key value pairs maintained. its corresponding key which is required for its retrieval during iteration 4 Method to add Put method of hash map is used to element add element in hashmap. On other hand add method of hashset is used to add element in hashset. 5 5 Index performance Hashmap due to its unique key is HashSet is completely based on faster in retrieval of element during its object so compared to hashmap iteration. is slower. Null Allowed 6 Single null key and any number of On other hand Hashset allows null value can be inserted in hashmap only one null value in its without any restriction collection after which no null value is allowed to be added