Sunday, January 3, 2016

Java Map.Entry Interface Quick Reference Guide

Java Map.Entry Interface Quick Reference Guide

What is Map.Entry Interface?
This interface represents the entry in Map i.e. key-value pair. entrySet() method of Map interface returns Set<Map.Entry<K, V>>. This interface has methods that work on entries.


Method and Description

K getKey()
getKey() returns the key corresponding to this entry.
                                                                                                                                                
V getValue()
getValue() returns the value corresponding to this entry.
                                                                                                                                                
V setValue(V value)
setValue() method is used to replace the value corresponding to this entry with specified value.
                                                                                                                                                
boolean equals(Object o)
equals() method is used to compare the specified Object with this entry for equality. So it will return true if the Object is entry and contains same mapping with this entry.
                                                                                                                                                
int hashCode()
hashCode() method is used to generate hashCode for this entry.
                                                                                                                                                
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey()
This method returns a Comparator that compares Map.Entry in natural order on key.
                                                                                                                                                
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue()
This method returns a Comparator that compares Map.Entry in natural order on value.
                                                                                                                                                
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp)
This method returns a Comparator that compares Map.Entry by key using the given Comparator.
                                                                                                                                                
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp)
This method returns a Comparator that compares Map.Entry by value using the given Comparator.




Java Map Interface Quick Reference Guide

Java Map Interface Quick Reference Guide

What is Map<K, V> Interface?
               
K will map to V. That means Key will map to Value. Map Interface is used to map one key to one value. A map cannot contain duplicate keys. It can have duplicate values. Map can have one null key. Map can have duplicate values. Map Interface has some extremely popular concrete implementations such as HashMap, ConcurrentHashMap, LinkedHashMap, TreeMap, Properties and others. You can find entire list here.

Method and Description

int size()
size() method returns the total number of key-value mappings in this map.
                                                                                                                                                
boolean isEmpty()
isEmpty() method returns if map is empty or not i.e. is there are no key-value mappings then it returns true else it returns false.
                                                                                                                                                
boolean containsKey(Object key)
containsKey() method is used to check whether this map contains a mapping for the key provided in argument. So it returns true if and only if (key == null ? k == null : key.equals(k)). If such mapping exists, then it returns true else it returns false. There can be at most one such mapping.
                                                                                                                                               
boolean containsValue(Object value)
containsValue() method is used to check whether map contains the value passed as parameter or not. So it returns true if (value == null ? v == null : value.equals(v)). This method requires scan of entire map at worst case so it might require a linear time to search a value. As Map implementations allow duplicate values there can be more than one values in this map.
                                                                                                                                               
V get(Object key)
get() method is one of the most used method of Map. It is used to get the value that is mapped to the key. So if this map contains a mapping of key k to value v such that
(key == null ? k== null : key.equals(k)), then this method returns value v, otherwise it will returns null.
Now Map allows you to insert null values. Then return value null does not necessarily mean that key does not exists. So using containsKey() method prior to get() method would be better.
                                                                                                                                               
V put(K key, V value)
Creates a mapping for given key to value. If there exists a key in Map, then old value is replaced with specified value in argument.
                                                                                                                                               
V remove(Object key)
remove() method is used to remove the mapping of key-value from this Map. So, if this map contains a mapping from key k to value v such that 
(key == null ? k == null : key.equals(k)) then this mapping is removed. It returns the value associated with the key that is removed else it returns null.
If this map allows null values, then return value null does not indicate that key k was not found in this map. It is possible that key is explicitly mapped to a null value.
                                                                                                                                               
void putAll(Map<? extends K, ? extends V> m)
putAll method is used to copy all the mapping from map m to this map. The effect of this method is same as calling put(K k, V v) method called for every mapping of map m.
                                                                                                                                               
void clear()
clear() method is used to remove all the mappings from map. This will also set the size of map as 0.
                                                                                                                                               
Set<K> keySet()
keySet() method is View method of Map. It returns a Set<E> view of keys contained in this particular map. As Set is backed by Map the changes to Map are reflected to Set and changes to Set are reflected to Map.
                                                                                                                                               
Collection<V> values()
values() method is also a View method of Map. This method returns Collection view of values of Map. The Collection is backed by Map so changes made to Map are visible in Collection and changes made to Collection are visible in Map.
                                                                                                                                               
Set<Map.Entry<K, V>> entrySet()
entrySet() is 3rd and final View method of Map. This method returns Set<> of type Map.Entry<K, V>. Entry is sub-interface of Map interface i.e. Entry interface inside Map interface. As Set is backed by Map the changes to Map are reflected to Set and changes to Set are reflected to Map.
                                                                                                                                               
boolean equals(Object o)
equals() method is overridden method from Object class. equals() and hashCode() are used for comparison and hashing. equals() method compares specified Object with this Map for equality. This method will return true if and only if specified Object is Map and both Map has same Key-Value mappings.
                                                                                                                                               
int hashCode()
hashCode() method is used to return hash code of this Map. The hash code of this map is defined as summation of all hash code of each entry in this map. Entries are taken from entrySet() method.
                                                                                                                                               
default V getOrDefault(Object key, V defaultValue)
getOrDefault() method is used to return the value of key mapped to value or returns defaultValue as passed in parameter.
                                                                                                                                               
default void forEach(BiConsumer<? super K, ? super V> action)
BiConsumer interface performs given action on all the entries of this map until all entries are processed or throws an Exception.
                                                                                                                                               
default void replaceAll(BiFunction<? super K,
                                                             ? super V,
                                                             ? extends V> function)
BiFunction interface in replaceAll method will replace each entry’s value with result of invoking the given function on that entry until all entries are processed or function throws an Exception.
                                                                                                                                               
default V putIfAbsent (K key, V value)
putIfAbsent() method is used to put the value in Map if the key is not associated with a value or if key is mapped to null.
                                                                                                                                               
default boolean remove (Object key, Object value)
remove() method is used to remove the entry for the key if it is mapped to specified value.
                                                                                                                                               
default boolean replace(K key, V oldValue, V newValue)
replace() method is used to replace the entry for the key if it is mapped to specified value.
                                                                                                                                               
default V replace(K key, V value)
replace() method is used to replace the entry for the key if it is mapped to some value.
                                                                                                                                               
default V computeIfAbsent(K key, Function<? super K, 
                                                                             ? extends V> mappingFunction)
computeifAbsent() method is used to map a key that is not associated with value or mapped to null. It attempts to compute its value using given mapping function.
                                                                                                                                               
default V 
computeIfPresent(K key, BiFunction<? super K, 
                                                                 ? super V, 
                                                                 ? extends V> remappingFunction)
             computeIfPresent() method is opposite of computeIfAbsent(). If the value of key is              present and not-null then it attempts to compute new mapping given the key and                 its current mapped value.
                                                                                                                                               
default V 
compute(K key, BiFunction<? super K,
                                                  ? super V,
                                                  ? extends V> remappingFunction)
compute() method is used to compute a mapping for specified key and its current mapped value if there is no current mapping.
                                                                                                                                               
default V 
merge(K key, V value, BiFunction<? super V, 
                                                            ? super V, 
                                                            ? extends V> remappingFunction)
merge() method is helpful when combining multiple mapped values for key. If the key is not already associated with a value or is associate with null, then it is associated with non-null value. Otherwise it replaces the associated value with results of remapping function or removes it result is null. If the function returns null, the mapping is removed.



                

Saturday, January 2, 2016

Java Deque Interface Quick Reference Guide

Java Deque Interface Quick Reference Guide

Deque<E> interface extends Queue<E> interface. Queue<E> interface extends Collection<E> interface. This interface provides additional functionalities to the Queue<E> interface. It is a Double Ended Queue. It allows element insertion and removal at both the ends.


Method

Description
void addFirst(E e)

Inserts the element at the front of the Deque. It throws IllegalStateException if currently no space is available in Deque.

void addLast(E e)

Inserts the element at the end of the Deque. It throws IllegalStateException if currently no space is available in Deque.

boolean offerFirst(E e)

Inserts the element at the front of the Deque. Returns true if the element is added to the Deque else returns false.

boolean offerLast(E e)

Inserts the element at the front of the Deque. Returns true if the element is added to the Deque else returns false.

E removeFirst()

Retrieves and removes the first element of this Deque. This method differs from pollFirst in a way that this method throws NoSuchElementException if this Deque is empty whereas pollFirst returns null.


E removeLast()

Retrieves and removes the last element of this Deque. This method differs from pollLast in a way that this method throws NoSuchElementException if this Deque is empty whereas pollLast returns null.

E pollFirst()

Retrieves and removes the first element of this Deque. If the Deque is empty, it returns null.

E pollLast()

Retrieves and removes the last element of this Deque. If the Deque is empty, it returns null.

E getFirst()

Retrieves but does not removes the first(head) element of this Deque. This method differs from peekFirst in a way that this method throws NoSuchElementException if Deque is empty whereas peekFirst returns null.

E getLast()

Retrieves but does not removes the last(tail) element of this Deque. This method differs from peekLast in a way that this method throws NoSuchElementException if Deque is empty whereas peekLast returns null.

E peekFirst()

Returns but does not remove the first(head) element of this Deque. If this Deque is empty, then it returns null.

E peekLast()

Returns but does not remove the last(tail) element of this Deque. If this Deque is empty, then it returns null.

boolean removeFirstOccurrence(Object o)

Removes the first occurrence of the specified element from this Deque such that
o == null ? e == null : o.equals(e)
Returns true if the element as removed as a result of this call, else returns false.

boolean removeLastOccurrence(Object o)

Removes the last occurrence of the specified element from this Deque such that
o == null ? e == null : o.equals(e)
Returns true if the element as removed as a result of this call, else returns false.

boolean add(E e)

Inserts the specified elements into the queue. Returns true if element is inserted. It throws IllegalArgumentException if space is not available.

boolean offer(E e)

Inserts the specified elements into the queue. Returns true if element is inserted else returns false.

E remove()

Retrieves and removes the head of the Queue. Throws NoSuchElementException if the Queue is empty.

E poll()

Retrieves and removed the head of the Queue. Returns null if the Queue is empty.

E element()

Retrieves but does not removes the head of the Queue. Throws NoSuchElementException if the Queue is empty.

E peek()

Retrieves but does not removes the head of the Queue. Returns null if the Queue is empty.

void push(E e)

This method is identical to addFirst method. It pushes the element in front of this Deque. It throws IllegalStateException if element is not added into the Deque.

E pop()

This method is identical to removeFirst method. It removes the element from front of this Deque. It throws NoSuchElementException if this Deque is empty.

boolean remove(Object o)

This method removes the Object o from this Deque. This method removes single element from Deque such that
o == null ? e == null : o.equals(e). Returns true if the element is removed from Deque else returns false.

boolean contains(Object o)

Returns true if this Deque contains Object o such that
o == null ? e == null : o.equals(e)

int size()

Returns the size of the Deque.

Iterator<E> iterator()

Returns the iterator that will step over or iterate over the elements of Deque. It guarantees the order in which it will iterate i.e. from head to tail.

Iterator<E> descendingIterator()

Returns the iterator that will iterate through the elements in reverse order i.e. from tail to head.



                

Java NavigableSet Interface Quick Reference Guide

Java NavigableSet Interface Quick Reference Guide

What is NavigableSet<E> Interface?

NavigableSet<E> extends SortedSet<E> interface. SortedSet<E> extends Set<E> interface. Set<E> interface extends Collection<E> interface. NavigableSet returns elements less than, less than or equal, greater than or equal, and greater. If no such element is found, then it returns null.


Method
Description

E lower(E e)

Returns the element that is greatest element in the set and less than the given element. If no such element is found, then it returns null.

E floor(E e)

Returns the element that is greatest element in the set and less than or equal to the given element. If no such element is found, then it returns null.

E ceiling(E e)

Returns the least element in the set greater than or equal to the given element. If no such element is found, then it returns null.

E higher(E e)

Returns the least element in this set greater than the given element. If no such element is found, then it returns null.

E pollFirst()

Retrieves and removes the first element. If the set is empty, it returns null.

E pollLast()

Retrieves and removes the first element. If the set is empty, it returns null.

Iterator<E> iterator()

Returns the Iterator over the elements in ascending order.

NavigableSet<E> descendingSet()

This method returns the reverse order of the set. This descending set is backed by this set so any changes in this set will also be reflected in descending set and vice-versa.

Iterator<E> descendingIterator()

Returns the iterator that will iterate through the elements in descending order.

NavigableSet<E>
subSet(E fromElement, 
            boolean fromInclusive,
            E toElement, 
            boolean toInclusive)

Returns a navigable sub set from fromElement to toElement.
NavigableSet<E>
headSet(E toElement, 
              boolean inclusive)

Returns the set whose elements are less than (or equal to if inclusive is true). The returned navigable set is backed by this set so changes in this set are reflected in navigable set and vice-versa.

NavigableSet<E>
tailSet(E toElement, 
            boolean inclusive)

Returns the set whose elements are greater than (or equal to if inclusive is true). The returned navigable set is backed by this set so changes in this set are reflected in navigable set and vice-versa.


Java SortedSet Interface Quick Reference Guide

Java SortedSet Interface Quick Reference Guide

What is SortedSet<E> Interface?

                SortedSet<E> interface extends Set<E> interface which in turn extends Collection<E> interface. SortedSet<E> is a type of Set that provides total ordering on its elements. The elements in this Set are ordered using the Comparable (natural ordering) or by providing the Comparator. Also the iterator returned by SortedSet<E> implementation steps through the elements in ascending order.


Method
Description

Comparator<? super E> comparator()
This method returns the Compartor used to order the elements in this set. It returns null if this set uses comparable.

SortedSet<E>
subset(E fromElement, E toElement)
Returns a sub set of this set fromElement inclusive and toElement exclusive. The returned set is backed by this set so if changes in this set are reflected in sub set and vice-versa.

SortedSet<E> headset(E toElement)

headSet method is used to return all the elements from the set which are less than toElement. The returned set is backed by this set so if changes in this set are reflected in sub set and vice-versa.

SortedSet<E> tailSet(E fromElement)

tailSet method is used to return all the elements from the set which are greater than or equal to fromElement. The returned set is backed by this set so if changes in this set are reflected in sub set and vice-versa.

E first()

Returns the first i.e. lowest element currently in set.

E last()

Returns the last i.e. highest element currently in set.

default Spliterator<E> spliterator()
Create a Spliterator over the elements in sorted set.


Java Queue Interface Quick Reference Guide

Java Queue<E> Interface Quick Reference Guide

What is Queue<E> Interface?

Queue<E> interface extends Collection<E> interface. Queue<E> interface is designed or used for holding elements prior to processing. Besides methods inherited from parent interface i.e Collection<E> interface, Queue<E> interface provides additional insertions, extractions and inspection operations. These methods exist in two forms: one form returns special value i.e null or false, if it fails and other one throws Exception.


Method
Description

boolean add(E e)
Inserts the specified elements into the queue. Returns true if element is inserted. It throws IllegalArgumentException if space is not available.

boolean off(E e)
Inserts the specified elements into the queue. Returns true if element is inserted else returns false.

E remove()
Retrieves and removes the head of the Queue. Throws NoSuchElementException if the Queue is empty.

E poll()
Retrieves and removed the head of the Queue. Returns null if the Queue is empty.

E element()
Retrieves but does not removes the head of the Queue. Throws NoSuchElementException if the Queue is empty.

E peek()
Retrieves but does not removes the head of the Queue. Returns null if the Queue is empty.



Ads Inside Post