Posts

Interfaces in java with example

Interface concept included in java to overcome the Diamond problem of multiple inheritance in java. In other words, Interface was introduced in java to define objects outside the world through methods. For example, buttons on the front of the television set. That are the interfaces between you and the electrical set. You press the power button to turn the television to on or off. An interface is a specification of method prototypes. Prototypes will be in the interface. All the methods of the interface are abstract. For the polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract class. Through interfaces we can achieve multiple inheritance in java. Because there is a possibility of inheriting more than one interface into java class by using the keyword called implements. When we ...

Map interface with implementation classes

Map interface stores elements in the form of key-value pairs in java. If the key is provided then its corresponding value can be obtained. Of course, the keys should have unique values. Map interface implemented classes are, HashMap HashTable LinkedHashMap TreeMap Methods of Map implemented classes: value put(key, value) : This method stores key-value pairs. value get(Object key) : This method returns corresponding value when key is given. If the key does not have a value associated with it, then it returns null. Set<K> keySet() : This method, when applied on a map converts it into a Set where only keys will be stored. Collection<V> values() : This method, when applied on a HashMap objects returns all the values of the HashMap into Collection objects. value remove(Object key) : This method removes the key and corresponding value from the map. void clear() : This method removes all the key-value pairs. boolean isEmpty() : This met...

Set interface in java with example

Read this article to know what is Set interface and its implemented classes in java with examples programs.  Set interface  represents a group of elements arranged as like an array. Set interface will grown dynamically when the elements are stored into it. Set will not allow duplicate elements to store into it. Set interface implemented classes are, HashSet<T> TreeSet<T> LinkedHashSet<T>           <T> represents the generic type of the storing element in set. Set interface methods: boolean add(Object) : Ensures the Set holds the argument. The Object is added only if it isn't already in the Set. Returns false if the Object was not added to the Set. void clear() : Removes all elements from the Set. boolean contains(Object) : Returns true if the Set contains the argument. boolean isEmpty() : Returns true if the Set contains no elements. ...