Showing posts with label java Collection framework. Show all posts
Showing posts with label java Collection framework. Show all posts

Monday, August 20, 2018

Java Arraylist Class

Java Arraylist Class

  • ArrayList course of teaching is sub course of teaching of collection interface which implements to List interface.
  • ArrayList course of teaching provides resizable-array thus it tin grow automatically every bit per requirement which resolves fixed Array limitation where you lot convey to pre-define the size of array.
  • It tin agree zippo elements too.
  • It is non synchronized implementation thus if multiple threads accessing it concurrently in addition to whatsoever ane from them modifies the entry in addition to then it must live synchronized externally.
  • Also it tin agree duplicate elements.
ArrayList course of teaching is sub course of teaching of collection interface which implements to List interface Java ArrayList Class

Important methods of ArrayList course of teaching are every bit below.
  • boolean add(E e) : It volition append given chemical component division at the cease of this ArrayList.
  • void add(int index, due east element) : It volition add together specified chemical component division at the given index inwards list.
  • boolean addAll(Collection<? extends E> c) : It volition append all the elements of given collection at the cease of array list. Appending fellowship volition live every bit per returned yesteryear collection's iterator.
  • boolean addAll(int index, Collection<? extends E> c) : It volition insert all elements of specified collection inwards ArrayList starting from given index.
  • void clear() : It volition clear List yesteryear removing all elements from it.
  • Object clone() : It volition render shallow re-create of ArrayList.
  • boolean contains(Object o) : It volition render truthful if given chemical component division is acquaint inwards list.
  • void ensureCapacity(int minCapacity) : It volition increases the capacity of array listing if required to brand certain that it tin shop the elements every bit per given minCapacity argument.
  • E get(int index) : It volition decease chemical component division from specified inced from array list.
  • int indexOf(Object o) : It volition render the index of specified object(first occurrence) from array list.
  • boolean isEmpty() : It volition banking concern jibe in addition to render truthful if array listing is empty.
  • Iterator<E> iterator() : It volition render an iterator over the elements from array list.
  • int lastIndexOf(Object o) : It volition render index of terminal occurrence of specified element.
  • ListIterator<E> listIterator() : It volition render the listing iterator over the elements of array list.
  • ListIterator<E> listIterator(int index) : It volition render the listing iterator over the elements of array listing starting from specified index.
  • E remove(int index) : It volition take chemical component division from array listing which is stored at specified index.
  • boolean remove(Object o) : It volition take specified element(first occurrence) from array list.
  • boolean removeAll(Collection<?> c) : It volition take all elements from listing which are specified inwards given collection.
  • protected void removeRange(int fromIndex, int toIndex) : It volition take elements from listing starting from fromIndex to toIndex.
  • boolean retainAll(Collection<?> c) : It volition retain exclusively those elements inwards array listing which are specified inwards given collection.
  • E set(int index, due east element) : It volition supervene upon given chemical component division inwards array listing at specified position.
  • int size() : It volition render size of array list.
  • List<E> subList(int fromIndex, int toIndex) : It volition render sub listing of elements from this listing starting from fromIndex(inclusive) to toIndex(exclusive).
  • Object[] toArray() :It volition render proper sequential array of all array listing elements.
  • void trimToSize() :It volition cut size of array listing every bit per it's electrical flow size.
Below given event volition demo you lot exercise of ArrayList class's unlike methods.

ArrayListExample
package JAVAExamples;  import java.util.ArrayList; import java.util.Iterator;  populace course of teaching ArrayListClassExample {   populace static void main(String[] args) {   // Create array list.   ArrayList<String> a = novel ArrayList<String>();   // Add elements inwards array list.   a.add("one");   a.add("two");   a.add("three");   a.add("four");   a.add("one");   a.add("six");   a.add("seven");   a.add("eight");    //Iterating over the arraylist elements.   Iterator itr = a.iterator();   System.out.println("ArrayList items are : ");   piece (itr.hasNext()) {    System.out.println(itr.next());   }      //Add special at specified index.   a.add(5, "nine");   System.out.println("Now ArrayList items are : "+a);      //Get special from specified index.   System.out.println("Item at index 6 is : "+a.get(6));      //Remove special from array list.   a.remove("three");   System.out.println("Now ArrayList items are : "+a);      //Get index of chemical component division from array list.   System.out.println("Index of ane is : "+a.indexOf("one"));      //Get terminal index of chemical component division from array list.   System.out.println("Last Index of ane is : "+a.lastIndexOf("one"));  } }

Output :
ArrayList items are :  ane ii iii iv ane 6 7 8 Now ArrayList items are : [one, two, three, four, one, nine, six, seven, eight] Item at index 6 is : 6 Now ArrayList items are : [one, two, four, one, nine, six, seven, eight] Index of ane is : 0 Last Index of ane is : 3

Thursday, February 25, 2016

Java Priorityqueue Class

Java Priorityqueue Class

  • PriorityQueue Class is 1 of the collection framework degree which implements Queue, Serializable, Iterable too Collection interfaces.
  • It is using natural ordering to lodge thus elements of PriorityQueue.Or elements volition hold out ordered based on Comparator provided at queue construction fourth dimension depending on which constructor is used.
  • PriorityQueue is based on a priority heap. Heap is tree based information construction where all the nodes of tree are inward a specific order. Please read almost Heap to larn how it works.
  • PriorityQueue does non permit non-comparable objects too it may throw ClassCastException if you lot volition endeavour to create so.
  • PriorityQueue does non permit zippo elements every bit well.
  • PriorityQueue implementation is non synchronized. So multiple threads should non access it concurrently.
PriorityQueue Class is 1 of the collection framework degree which implements Queue Java PriorityQueue Class

Few methods which are used alongside PriorityQueue are every bit below.
  • boolean add(E e) : It volition insert specified chemical constituent inward PriorityQueue.
  • void clear() : It volition take away all elements from it to clear PriorityQueue.
  • boolean contains(Object o) : It volition banking corporation lucifer too render truthful if specified chemical constituent is available inward PriorityQueue. Else it volition render false.
  • boolean offer(E e) : It volition insert specified chemical constituent inward PriorityQueue.
  • E peek() : It volition render head(first) of the chemical constituent from queue. If PriorityQueue is empty thus it volition render null.
  • E poll() : It volition take away head(first) of the chemical constituent from queue. If PriorityQueue is empty thus it volition render null.
  • boolean remove(Object o) : It volition take away specified element's unmarried event from PriorityQueue if it is present.
  • int size() : It volition render size of PriorityQueue.
  • Object[] toArray() : It volition render elements array from queue.
  • <T> T[] toArray(T[] a) : It volition render elements array from queue. The runtime type of the returned array is that of the specified array.
Below given instance volition demo you lot how to operate PriorityQueue methods.

PriorityQueueExample :
package JAVAExamples;  import java.util.PriorityQueue;  populace degree PriorityQueueExample {   populace static void main(String[] args) {   //Create PriorityQueue.   PriorityQueue q=new PriorityQueue();   //Add items to PriorityQueue.   q.add("one");   q.add("two");   q.add("three");   q.add("four");   q.add("five");   q.add("six");   q.add("seven");      //Get size of PriorityQueue.   System.out.println("Size of PriorityQueue is : "+q.size());      //Print values of PriorityQueue.   System.out.println("PriorityQueue elements are : "+q);      //Get caput of PriorityQueue.   System.out.println("Head of PriorityQueue is : "+q.peek());      //Remove caput of PriorityQueue.   q.poll();   System.out.println("Now PriorityQueue elements are : "+q);    } }

Output :
Size of PriorityQueue is : 7 PriorityQueue elements are : [five, four, seven, two, one, three, six] Head of PriorityQueue is : 5 Now PriorityQueue elements are : [four, one, seven, two, six, three]

Java Deque Interface

Java Deque Interface

  • Deque is 1 of the collection interface which extends Queue interface.
  • Deque is linear collection which allow us to add together together with take chemical cistron from both ends of the queue.
  • It is "double ended queue" that's why it is called Deque together with commonly pronounced equally "deck".
  • Deque has dissimilar methods to perform insert, delete together with bear witness the chemical cistron inward queue.
  • Each of these methods convey 2 dissimilar forms. One volition throw an exception if it fails during performance together with other volition furnish a particular value similar naught or false.
Deque is 1 of the collection interface which extends Queue interface Java Deque Interface

Different methods of Deque interface are described below.

  • boolean add(E e) : It volition insert specified chemical cistron at the tail of this deque if infinite is currently available. Return truthful if performance is success or throw an IllegalStateException if fails to perform operation.
  • void addFirst(E e) : It volition insert specified chemical cistron at the get-go of the queue.
  • void addLast(E e) : It volition insert specified chemical cistron at the destination of the queue.
  • boolean contains(Object o) : It volition furnish truthful if specified chemical cistron is available inward queue.
  • E element() : It volition yell back head(first) chemical cistron from queue.
  • E getFirst() : It volition yell back inaugural of all chemical cistron from queue.
  • E getLast() : It volition yell back concluding chemical cistron from queue.
  • boolean offer(E e) : It volition insert specified chemical cistron at the tail of this deque. It volition furnish truthful if success together with faux if performance fails.
  • boolean offerFirst(E e) : It volition insert specified chemical cistron at the get-go of queue if infinite is available.
  • boolean offerLast(E e) : It volition insert specified chemical cistron at the destination of queue if infinite is available.
  • E peek() : It volition yell back head(first) chemical cistron from queue. Returns naught if queue is empty.
  • E peekFirst() : It volition yell back inaugural of all chemical cistron from queue. Returns naught if queue is empty.
  • E peekLast() : It volition yell back concluding chemical cistron from queue. Returns naught if queue is empty.
  • E poll() : It volition take head(first) chemical cistron from queue. Returns naught if queue is empty.
  • E pollFirst() : It volition take inaugural of all chemical cistron from queue. Returns naught if queue is empty.
  • E pollLast() : It volition take concluding chemical cistron from queue. Returns naught if queue is empty.
  • E pop() : It volition pops the chemical cistron from queue.
  • void push(E e) : It volition pushes the chemical cistron onto queue. It volition furnish truthful on success together with throe an exception if performance fails.
  • E remove() : It volition take inaugural of all chemical cistron from queue.
  • boolean remove(Object o) : It volition take inaugural of all occurrence of the given chemical cistron from queue.
  • E removeFirst() : It volition take inaugural of all chemical cistron from queue.
  • boolean removeFirstOccurrence(Object o) : It volition take inaugural of all occurrence of the given chemical cistron from queue.
  • E removeLast() : It volition take concluding chemical cistron from queue.
  • boolean removeLastOccurrence(Object o) : It volition take concluding occurrence of the given chemical cistron from queue.
  • int size() : It volition furnish size of queue.
I convey prepared representative of ArrayDeque to explore you lot how dissimilar methods of Deque works. Also you lot tin work LinkedList() using Deque.

Deque Example

package JAVAExamples;  import java.util.ArrayDeque; import java.util.Deque;  world flat DequeExample {   world static void main(String[] args) {   //Create ArrayDeque.   Deque<String> d = novel ArrayDeque<String>();   //Add elements inward Deque.   d.add("one");   d.add("two");   d.add("three");   d.add("four");   d.add("five");   d.add("four");   d.add("six");   //Print Deque elements.   System.out.println("Deque elements are : "+d);      //Add chemical cistron at inaugural of all of Deque.   d.addFirst("First Added");   System.out.println("Deque elements afterwards addFirst are : "+d);      //Add chemical cistron at concluding of Deque.   d.addLast("Last Added");   System.out.println("Deque elements afterwards addLast are : "+d);      //Get inaugural of all chemical cistron from Deque.   System.out.println("First chemical cistron inward deque is : "+d.getFirst());      //Get concluding chemical cistron from Deque.   System.out.println("Last chemical cistron inward deque is : "+d.getLast());      //Get inaugural of all chemical cistron from Deque using peek.   System.out.println("First chemical cistron inward deque using peek is : "+d.peek());      //Get concluding chemical cistron from Deque using peekLast.   System.out.println("Last chemical cistron inward deque using peekLast is : "+d.peekLast());      //Remove inaugural of all chemical cistron from deque.   d.removeFirst();   System.out.println("Deque elements afterwards removeFirst are  : "+d);      //Using pop.   d.pop();   System.out.println("Deque elements afterwards popular are  : "+d);      //Using push.   d.push("First");   System.out.println("Deque elements afterwards force are  : "+d);      //Remove LastOccurrence of chemical cistron from deque using removeLastOccurrence.   d.removeLastOccurrence("four");   System.out.println("Deque elements afterwards removeLastOccurrence are  : "+d);      //Get size of deque.   System.out.println("Size of Deque is  : "+d.size());  } }

Output :
Deque elements are : [one, two, three, four, five, four, six] Deque elements afterwards addFirst are : [First Added, one, two, three, four, five, four, six] Deque elements afterwards addLast are : [First Added, one, two, three, four, five, four, six, Last Added] First chemical cistron inward deque is : First Added Last chemical cistron inward deque is : Last Added First chemical cistron inward deque using peek is : First Added Last chemical cistron inward deque using peekLast is : Last Added Deque elements afterwards removeFirst are  : [one, two, three, four, five, four, six, Last Added] Deque elements afterwards popular are  : [two, three, four, five, four, six, Last Added] Deque elements afterwards force are  : [First, two, three, four, five, four, six, Last Added] Deque elements afterwards removeLastOccurrence are  : [First, two, three, four, five, six, Last Added] Size of Deque is  : 7

Java Gear Upward Interface As Well As Methods

Java Gear Upward Interface As Well As Methods

  • Set is 1 of the collection framework interface which extends origin Collections interface.
  • Set collection tin non comprise duplicate elements.
  • Set interface has all methods which are inherited from Collection interface amongst special restriction of non allowing duplicate elements.
  • If 2 Set interface contains same elements together with then both are equal.
  • Set interface is implemented past times LinkedHashSet, HashSet classes together with extended past times SortedSet interface which is implemented past times TreeSet.
Set is 1 of the collection framework interface which extends origin Collections interface Java Set Interface And Methods
Important Methods of Set Interface
  • boolean add(E e) : It volition insert specified chemical cistron inwards laid if it is non available inwards set.
  • boolean addAll(Collection<? extends E> c) : It volition add together all elements of specified collection inwards laid if non already acquaint inwards set.
  • void clear() : It volition take away all elements from set.
  • boolean contains(Object o) : It volition provide truthful if  given chemical cistron is available inwards set.
  • boolean containsAll(Collection<?> c) : It volition provide truthful if all the elements of given collection are available inwards set.
  • boolean equals(Object o) :It volition compare specified object amongst laid to banking concern jibe equality.
  • boolean isEmpty() : It volition provide truthful if laid is empty.
  • int hashCode() :It volition returns hash code value for this set.
  • Iterator<E> iterator() : It volition provide an iterator over the elements inwards set.
  • boolean remove(Object o) : It volition take away specified chemical cistron from the laid if it is present.
  • boolean removeAll(Collection<?> c) : It volition take away all those elements from laid which are specified inwards collection.
  • int size() : It volition provide size of Set.
Below given Set interface event volition demo y'all demo of Set interface's few of import methods.

Java Set Interface Example
package JAVAExamples;  import java.util.HashSet; import java.util.Iterator; import java.util.Set;  world cast SetInterfaceExample {  world static void main(String args[]) {   Set s1 = novel HashSet();   // Check initial size of set.   System.out.println("Initial size of laid : " + s1.size());   // Add items inwards set. Try to add together duplicate exceptional inwards laid every bit laid tin non convey duplicate items.   s1.add("Item1");   s1.add("Item2");   s1.add("Item1");   s1.add("Item3");   s1.add("Item4");   // Print set. It volition demo solely iv items every bit laid tin non concur duplicate items.   System.out.println("Set items are : " + s1);   // Check if laid is empty. It volition provide false.   System.out.println("Set is empty? : " + s1.isEmpty());    // access laid items through Iterator.   Iterator iterator = s1.iterator();                 System.out.println("Set items are : ");   piece (iterator.hasNext()) {    String chemical cistron = (String) iterator.next();    System.out.println(element);   }    // Remove exceptional from set.   s1.remove("Item3");   // Set items afterwards removing Item3 from set.   System.out.println("Now Set items are : " + s1);   // Get laid size.   System.out.println("Set size is : " + s1.size());  } }

Output :
Initial size of laid : 0 Set items are : [Item1, Item2, Item3, Item4] Set is empty? : imitation Set items are :  Item1 Item2 Item3 Item4 Now Set items are : [Item1, Item2, Item4] Set size is : 3

This way, You tin role coffee laid to shop non duplicate items.

Wednesday, February 24, 2016

Java Linkedhashset Class

Java Linkedhashset Class

  • LinkedHashSet Class extends extends HashSet shape together with Implements Set interface inward collection interface hierarchy.
  • LinkedHashSet implementation differs from HashSet every bit it maintains doubly-linked listing running through all of its entries.
  • It maintains element's entries inward laid upwards every bit per it's insertion order. So it volition permit y'all insertion companionship iteration over the set. So when y'all iterate over LinkedHashSet, In which companionship it volition provide elements? Answer is As per it's insertion order.
  • So y'all volition function out same re-create of elements amongst the companionship how they inserted inward set. 
  • LinkedHashSetis non synchronized.
extends HashSet shape together with Implements Set interface inward collection interface hierarchy Java LinkedHashSet Class



Bellow given example volition demonstrate y'all basic methods of  LinkedHashSet together with it's usage.

package JAVAExamples;  import java.util.Iterator; import java.util.LinkedHashSet;  populace shape LinkedHashSetExample {   populace static void main(String[] args) {   LinkedHashSet<String> lhs = novel LinkedHashSet<String>();   // Add items to LinkedHashSet   lhs.add("one");   lhs.add("two");   lhs.add("three");   lhs.add("four");   // Print LinkedHashSet.   System.out.println(lhs);   // Get size of LinkedHashSet.   System.out.println("LinkedHashSet size Is : " + lhs.size());   // Check if LinkedHashSet is empty?   System.out.println("Is LinkedHashSet emplty? : " + lhs.isEmpty());   // Iterate through LinkedHashSet.   Iterator<String> itr = lhs.iterator();   System.out.println("LinkedHashSet Items are : ");   acre (itr.hasNext()) {    System.out.println(itr.next());   }   // Remove special from LinkedHashSet.   lhs.remove("three");   // Print LinkedHashSet.   System.out.println("Now LinkedHashSet items are : " + lhs);  } }

Output :
[one, two, three, four] LinkedHashSet size Is : 4 Is LinkedHashSet emplty? : fake LinkedHashSet Items are :  i 2 3 4 Now LinkedHashSet items are : [one, two, four]

Hashset Inwards Coffee Collection Example

Hashset Inwards Coffee Collection Example

  • HashSet is a cast of collection framework which extends AbstractSet cast in addition to implements the Set interface.
  • HasSet doesn't guarantee that elements guild volition stay same over the fourth dimension in addition to returned inwards whatever random order.
  • HasSet doesn't permit duplicate values. If you lot endeavour to insert duplicate, It volition overwrite.
  • HasSet allows to shop nothing values.
  • HasSet implementation is non synchronized.
HashSet is a cast of collection framework which extends AbstractSet cast in addition to implements  HashSet inwards Java Collection Example

Important Methods of HashSet
  • boolean add(E e) : To add together elements inwards to laid if it is non already present.
  • void clear() : Remove all entries from set.
  • Object clone() : It volition returns shallow re-create of HasSet instance.
  • boolean contains(Object o) : It volition provide truthful if given values is introduce inwards HashSet.
  • boolean isEmpty() : It volition provide truthful if HasSet is empty.
  • Iterator<E> iterator() : It volition returns an iterator over the elements inwards Set.
  • boolean remove(Object o) : It volition take specified elements from laid if it is available inwards HashSet.
  • int size() : It volition provide size of HashSet.
Bellow given illustration volition demo you lot utilise of unlike HashSet methods.
Example Of HashSet inwards Java
package JAVAExamples;  import java.util.HashSet;  populace cast HashSetJavaExample {  populace static void main(String args[]) {  HashSet hset = novel HashSet();  //Check if HashSet is empty.  System.out.println("HashSet is empty? : "+hset.isEmpty());  //Add elements inwards HashSet.  hset.add("One");  hset.add("Two");  hset.add("Three");  hset.add(null); //HassSet allows nothing values.  hset.add("Four");  //Print HashSet.  System.out.println("HashSet elements are : "+hset);  //Check HashSet size.  System.out.println("Size of HashSet is : "+hset.size());  //Removing chemical cistron from HashSet.  hset.remove("Two");  System.out.println("Now HashSet elements are : "+hset);  } }

Output :
HashSet is empty? : truthful HashSet elements are : [null, One, Four, Two, Three] Size of HashSet is : five Now HashSet elements are : [null, One, Four, Three]

This way, You tin post away role HashSet to shop values inwards random guild including null.

Friday, February 19, 2016

no image

Java Hashtable Class

  • Hashtable Class implements Map, Cloneable in addition to Serializable interfaces in addition to likewise extends Dictionary class.
  • It maps keys to values inwards tabular array format. Every fundamental is linked to it's value.
  • In Hashtable, whatever object which is not naught volition last used every bit a fundamental or it's value.
  • Initial capacity in addition to charge gene are 2 parameters of Hashtable that deport upon its performance.
  • Meaning of capacity is the release of buckets inwards Hashtable in addition to Meaning of initial capacity is release of buckets inwards Hashtable at the fourth dimension of its creation.
  • Load gene is parameter which mensurate how much amount Hashtable is in addition to when to increment it's capacity automatically.
  • In social club to successfully cry back in addition to shop object inwards Hashtable, the object which is used every bit fundamental must implement the equals in addition to the hashCode methods.
Important methods of Hashtable Class are given below.
  • void clear() : It volition take all fundamental in addition to it's values from Hashtable.
  • Object clone() : It volition exercise in addition to provide shallow re-create of this hashtable.
  • boolean contains(Object value) :It volition banking concern fit inwards Hashtable for whatever fundamental maps to the specified value.
  • boolean containsKey(Object key) : It volition banking concern fit if whatever fundamental is available inwards Hashtable which is same every bit specified object.
  • boolean containsValue(Object value) : It volition provide truthful if Hashtablehas whatever fundamental which maps to specified value.
  • Enumeration<V> elements() : Returns an enumeration of the values inwards this Hashtable.
  • Set<Map.Entry<K,V>> entrySet() : It volition provide laid upwards persuasion of Hashtable alongside mapping.
  • boolean equals(Object o) : It volition banking concern fit in addition to compare given object alongside map for equality.
  • V get(Object key) : It volition provide value which is mapped alongside specified fundamental inwards map. Return naught if no mapping found.
  • int hashCode() : It volition provide has code value for this map.
  • boolean isEmpty() : It volition banking concern fit if Hashtable is empty in addition to no mapping found.
  • Enumeration<K> keys() : It volition returns an enumeration of the keys inwards this Hashtable.
  • Set<K> keySet() : It volition provide laid upwards persuasion of keys contained inwards this map.
  • V put(K key, V value) : Map in addition to insert specified fundamental alongside value inwards map.
  • void putAll(Map<? extends K,? extends V> t) : It volition re-create all key-values mapping from specified map to this map.
  • protected void rehash() : It volition increases the capacity of in addition to internally reorganizes this Hashtable, inwards social club to adapt in addition to access its entries to a greater extent than efficiently.
  • V remove(Object key) : It volition take specified fundamental in addition to its mapped value from
  • int size() : It volition provide size of Hashtable.
  • Collection<V> values() : It volition provide collection persuasion of map.
I convey created sample illustration for Hashtable to demo usage of basic methods.

HashTableExample
package JAVAExamples;  import java.util.Hashtable; import java.util.Map;  world shape HashTableExample {   world static void main(String[] args) {   Hashtable<Integer, String> ht = novel Hashtable<Integer, String>();    ht.put(1000, "London");   ht.put(1001, "Mumbai");   ht.put(1002, "Chicago");   ht.put(1003, "Tokyo");   ht.put(1004, "Paris");   ht.put(1005, "Moscow");   ht.put(1006, "Berlin");    for (Map.Entry m : ht.entrySet()) {    System.out.println(m.getKey() + " " + m.getValue());   }      //Check if at that topographic point is whatever fundamental inwards map which maps to specified value.   System.out.println("Map has whatever fundamental for Chicago? : "+ht.contains("Chicago"));      //Check if specified fundamental is available inwards map.   System.out.println("Map has fundamental 1009? : "+ht.contains(1009));      //Get size of map   System.out.println("Map size is : "+ht.size());      }  }

Output :
1004 Paris 1003 Tokyo 1002 Chicago 1001 Bombay Map has whatever fundamental for Chicago? : truthful Map has fundamental 1009? : imitation Map size is : 7



no image

Java Hashmap Class

  • Is is pronounced every bit HashMap because It is hash tabular array based implementation of map interface.
  • It extends AbstractMap shape together with implements Map interface.
  • It allows to shop null fundamental together with null values.
  • It is non synchronized implementation hence if multiple threads accessing it concurrently together with whatever i of them modifies it together with then it must live on synchronized externally.
  • It does non guarantee of maintaining the map order.
Important methods of HashMap Class are every bit below.
  • void clear() : It volition take away together with clear all mapping entries from map.
  • Object clone() : It volition supply you lot shallow re-create of this map.
  • boolean containsKey(Object key) : It volition depository fiscal establishment tally together with render truthful if specified fundamental has whatever mapping inwards this map.
  • boolean containsValue(Object value) : It volition depository fiscal establishment tally together with render truthful if whatever fundamental has mapping alongside given value.
  • Set<Map.Entry<K,V>> entrySet() : It volition render the gear upwards thought of mapping.
  • V get(Object key) : teach method is used for retrieving value of specified fundamental from this map. If no mapping found, It volition render null.
  • boolean isEmpty() : It volition depository fiscal establishment tally together with render truthful if map is empty.
  • Set<K> keySet() : It volition render gear upwards thought of the keys of this map.
  • V put(K key, V value) : It volition associate specified fundamental value mapping inwards this map.
  • void putAll(Map<? extends K,? extends V> m) : It volition re-create all key-value mapping yoke of specified map to this map.
  • V remove(Object key) : It volition take away mapping of specified fundamental from this map.
  • int size() :It volition render size of this map.
  • Collection<V> values() : It volition render collection thought of map values.

HashMapExample :
package JAVAExamples;  import java.util.HashMap; import java.util.Iterator; import java.util.Map;  world shape HashMapExample {   world static void main(String[] args) {   // practise Map   Map<Integer, String> k = novel HashMap<Integer, String>();   // Add fundamental alongside value inwards map.   m.put(3,"Chicago");   m.put(7,"Mumbai");   m.put(1,"Tokyo");   m.put(5,"Delhi");   m.put(2,"Paris");    m.put(8,"Beijing");   m.put(11,"Berlin");    //Print map   System.out.println("Map : "+m);   // Get size of map.   System.out.println("Map size is : " + m.size());     //Check if map is empty.   System.out.println("Map is empty? : "+m.isEmpty());      //Remove mapping from map.   m.remove(3);   System.out.println("Map value later removing mapping of fundamental three : "+m);      //Get value from map for specified key.   System.out.println("Value for fundamental v is : "+m.get(5));      //Iterating over map.   Iterator<Integer> keySetIterator = m.keySet().iterator();   while(keySetIterator.hasNext()){     Integer fundamental = keySetIterator.next();     System.out.println("key: " + fundamental + " value: " + m.get(key));   }    //Check if map contains specified key.   System.out.println("Map has fundamental vii ? : "+m.containsKey(7));    //Check if map contains specified value.   System.out.println("Map has value xx ? : "+m.containsValue("Ahmedabad"));  } }

Output :
Map : {1=Tokyo, 2=Paris, 3=Chicago, 5=Delhi, 7=Mumbai, 8=Beijing, 11=Berlin} Map size is : vii Map is empty? : fake Map value later removing mapping of fundamental three : {1=Tokyo, 2=Paris, 5=Delhi, 7=Mumbai, 8=Beijing, 11=Berlin} Value for fundamental v is : Delhi key: 1 value: Tokyo key: ii value: Paris key: v value: Delhi key: vii value: Bombay key: 8 value: Beijing key: eleven value: Berlin Map has fundamental vii ? : truthful Map has value xx ? : false

Thursday, February 18, 2016

no image

Linkedhashmap Course Of Report Inwards Java

  • LinkedHashMap cast is i of the cast of map interface which extends HashMap cast as well as implements Map interface.
  • It is combination of linked listing and Hash table implementation of the Map interface as well as that's why it is called LinkedHashMap.
  • It is maintaining doubly-linked listing insertion companionship inwards which keys were inserted into the map.
  • It allows to shop nix elements as well as optional Map operations are likewise available.
  • It is non synchronized implementation therefore if whatever thread modifies map structurally when map is accessed past times multiple threads as well as therefore it must live on synchronized externally.
Important methods of LinkedHashMap class

  • void clear() : It volition take away all mappings from map.
  • boolean containsValue(Object value) : It volition banking concern stand upward for for value inwards map as well as supply truthful if values be inwards map.
  • V get(Object key) : It volition supply value which is mapped amongst specified fundamental inwards map. Else it volition supply null.
  • protected boolean removeEldestEntry(Map.Entry<K,V> eldest) : It will returns truthful if this map should take away its eldest entry.
ExampleLinkedHashMap
package JAVAExamples;  import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set;  populace cast ExampleLinkedHashMap {   populace static void main(String[] args) {   // Create a hash map.   LinkedHashMap<String, Double> fifty = novel LinkedHashMap<String, Double>();   // Put elements to the map.   l.put("Bicycle", novel Double(134.33));   l.put("Car", novel Double(353.12));   l.put("Bus", novel Double(-598.23));   l.put("Train", novel Double(193.56));   l.put("Motorcycle", novel Double(39.48));    // Print LinkedHashMap.   System.out.println("LinkedHashMap elements are : " + l);    // Get size of LinkedHashMap.   System.out.println("Size of LinkedHashMap is : " + l.size());    // Get value of key.   System.out.println("Value of Bus is " + l.get("Bus"));    // Check if LinkedHashMap is empty.   System.out.println("LinkedHashMap is empty? : " + l.isEmpty());    // Check of LinkedHashMap has given value or not   System.out.println("LinkedHashMap has 353.12 value? : "+ l.containsValue(353.12));    // Check of LinkedHashMap has given fundamental or not   System.out.println("LinkedHashMap has taxi key? : "+ l.containsKey("taxi"));    // Iterate over LinkedHashMap.   Set<Entry<String, Double>> second = l.entrySet();   // Get an iterator   Iterator<Entry<String, Double>> i = s.iterator();   // Display elements   acre (i.hasNext()) {    Map.Entry one thousand = (Map.Entry) i.next();    System.out.print(m.getKey() + ": ");    System.out.println(m.getValue());   }   System.out.println();  } }

Output : 
LinkedHashMap elements are : {Bicycle=134.33, Car=353.12, Bus=-598.23, Train=193.56, Motorcycle=39.48} Size of LinkedHashMap is : five Value of Bus is -598.23 LinkedHashMap is empty? : imitation LinkedHashMap has 353.12 value? : truthful LinkedHashMap has taxi key? : imitation Bicycle: 134.33 Car: 353.12 Bus: -598.23 Train: 193.56 Motorcycle: 39.48
<< PREVIOUS || NEXT >>