NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Collection Framework
--------------------
Collection vs Collections
Array vs ArrayList

List vs LinkedList
List vs Vector
List vs Set

Map vs Set
HashMap vs HashTable
HashMap vs ConcurrentHashMap

HastSet vs TreeSet

Stack vs Queue
Iterator vs Enumeration
Iterator vs ListIterator

comparable vs comparator

How HashMap works in java?
convert array to list?//Arrays.asList(arr);

======================================================================
insertion,deletion and retrieval of HashMap?
HashMap mp= new HashMap();
mp.put(1,"tutorials");
mp.put(2,"point");
mp.put(3,"is best");

mp.remove(2);

Set keys = map.keySet();
System.out.println("All keys are: " + keys);
// To get all key: value
for(String key: keys){
System.out.println(key + ": " + map.get(key));
}



Set set = (Set) map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry entry = mapIterator.next();
System.out.print(entry.getKey() + " : " + entry.getValue());
}


for(Map.Entry entry:map.entrySet()){
System.out.print(entry.getKey() + " : " + entry.getValue());
}

=========================================================================
Hashing Technique

h(x) = x % size

What is collision in hashing?
Collision Resolution Methods? //open hashing(chaining),closed hashing(linear probing,quadratic probing)

on high level hashmap is basically implementation of the map interface. At the same time if we see the lower level details
hashmap has a table an array of nodes and nodes are basically int hash,k key, v value and Node<k,v> next;
So basically the node itself is a linked list inside the table.
So each index in the table is basically a node which is actually a linked list of nodes.
So basically each index in the table is called a s a bucket.


Node<k,v>[] table;

put(k,v)
hash(k)
index = hash & (size-1)


How HashMap works in Java
HashMap works on the principle of hashing, we have put() and get() method for storing and retrieving object from HashMap.
When we pass both key and value to put() method to store on HashMap, it uses key object hashcode() method to
calculate hashcode and them by applying hashing on that hashcode it identifies bucket location for storing value object.
While retrieving it uses key object equals method to find out correct key value pair and return value object associated
with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list.
Also, HashMap stores both key and value tuple in every node of linked list in the form of Map.Entry object.


What will happen if two different HashMap key objects have the same hashcode?
They will be stored in the same bucket but no next node of linked list.
And keys equals () method will be used to identify correct key value pair in HashMap.



How null key is handled in HashMap?
Since equals() and hashCode() are used to store and retrieve values, how does it work in case of the null key?
The null key is handled specially in HashMap, there are two separate methods for that and those are belows
putForNullKey(V value) and getForNullKey(). Later is offloaded version of get() to look up null keys.
Null keys always map to index 0. This null case is split out into separate methods for the sake of performance
in the two most commonly used operations (get and put), but incorporated with conditionals in others.
In short, equals() and hashcode() method are not used in case of null keys in HashMap.


================================================================================================================
marker interface is interface with no fields or methods. ex serilizable , cloneable and Remote and these are used
to indicate something compiler or JVM.



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.