Skip to main content

Posts

Implement Maps in java

  Implement Maps in java It contains values on the basis of key i.e. key and value pair. Each key and value pair is known as an entry. It contains only unique keys. It is useful if you have to search, update or delete elements on the basis of key.   Methods of Map interface ·            put(Object key,Object value)-inserts an entry into map ·            get(Object key)-returns value for specified key ·            remove(Object key)-removes an entry for the specified key ·            containsKey(Object key)-search specified key from the map ·            keySet()-returns Set view containing all keys ·            entrySet()-returns Set view containing all keys and values ·            putAll(Map m)-inserts specified map in this map . Implementation of Map interface ·               HashMap ·               LinkedHashMap ·               TreeMap TreeMap It implements Map interface using a tree which provides efficient way of storing key/value pairs in sorted or

Implement Set using java

  Implement Set using java  HashSet    It is used to create collection which uses hash table for storage of data and implements Set Interface.   Features ·                           It stores the elements by using mechanism called Hashing ·                           It contains unique elements only. ·                           It does not maintain insertion order   Constructors ·                       HashSet()-initializes empty HashSet ·                       HashSet(Collection c)-constructs HashSet by using the elements of Collection   Methods ·                    add(Object o)-adds the specified element to this set if it is not already present ·                    remove(Object o)-removes specified element from set ·                    clear()-removes all elements from the set ·                    size()-counts the number of elements in set ·                    iterator()- iterates over the elements in set Program on HashSet import java.util.Set;