0% found this document useful (0 votes)
5 views

Hashmap Java

Uploaded by

Priya Solapure
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Hashmap Java

Uploaded by

Priya Solapure
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Hashmap :

=====================================================

-Hashmap is class which extends the AbstractMap and abstractmap implemennts the
map.
-Hashmap implements the map.
-Hashmap stores the data in the form of key and value pair.
where your key is unique.
-So here one object is used as key and another object used as value.
-If you try to insert duplicate key , it will replace the present element by
current one.
-It will not maintain insertion order.

example ->
Map<String, Integer> empMap = new HashMap<>();
empMap.put("EP5050", 21000);
empMap.put("EP5053", 45000);
empMap.put("EP5059", 1000);
empMap.put("EP5051", 89000);
empMap.put("EP5050", 32000);
empMap.put("EP5059", 8000);

for (Map.Entry aa : empMap.entrySet()) {


System.out.println(aa.getKey() + " " + aa.getValue());
}

empMap.remove("EP5050");
System.out.println("==============After remove operation============");

for (Map.Entry aa : empMap.entrySet()) {


System.out.println(aa.getKey() + " " + aa.getValue());
}

System.out.println("=====================================");

System.out.println(empMap.get("EP5059"));

System.out.println("=====================================");
System.out.println(empMap.containsKey("EP5059"));
System.out.println(empMap.containsValue(56000));
}

-default size is 16 -(bucket of hashmap)

syntax:-
HashMap<K, V> hmap = new HashMap<>();

=========================================================
Internal Working of Hashmap.....
--------------------------------------------------------

- A hashmap usese a hashtable , so internally implementes using two data structure


namely an arry and linkedlist.
- When you declaring hashmap internallty, it will create an array of bucket(default
size --> 16).
-The buckets are refered to as a nodes or we can say linkedkist.(or BST (binary
search tree)).
A node contains :-
1)Hashcode
2)key
3)Value
4)Address of next node

- now when you called put() method of hashmap class, hashcod will be generated by
put method.
- after calling of hashcode() method, hashcode of inserted key is generated and
after that index is calculated to put the
inseted record in that array index
index = hashcode(key) & (length-1)
ex -->

hashcode() method
equals() method

hashcode() method-
- it is used to get hashcode of an object.
-it return the memory reference of an object in the form of integer.
- it calculate the bucket i.e. it calcualte the index.

equals() method -

- this method used to check weather 2 objects are equal or not.


-it comapres the key whether they are equal or not. If equals() method return true
then they are
equal othewise not.

=======================================

Hash collision --

hash collision is a situation where two or more distinct records can create same
hashcode that is same has bucket position in hahtable.

Que) What is contract in between hashcode() and equals() method ?

Ans ->
A contract is:
If two objects are equal then they should have the same hashcode
and if two objects are not equal then they may or may not have same hash code.

==============================================================================
For interview questions

https://www.interviewbit.com/hashmap-interview-questions/

================================================================

public class Book {

private int id;


private String bookName;
private int price;
private String type;

public Book() {

public Book(int id, String bookName, int price, String type) {


super();
this.id = id;
this.bookName = bookName;
this.price = price;
this.type = type;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getBookName() {


return bookName;
}

public void setBookName(String bookName) {


this.bookName = bookName;
}

public int getPrice() {


return price;
}

public void setPrice(int price) {


this.price = price;
}

public String getType() {


return type;
}

public void setType(String type) {


this.type = type;
}

@Override
public int hashCode() {
return Objects.hash(bookName, id);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
return Objects.equals(bookName, other.bookName) && id == other.id;
}

}
---------------------------------------------------

public static void main(String[] args) {

HashMap<Book, Integer> bookMap = new HashMap<>();


Book b1 = new Book(1, "ABC", 150, "Learning");
bookMap.put(b1, b1.getPrice());
Book b2 = new Book(2, "XYZ", 180, "Historical");
bookMap.put(b2, b2.getPrice());
Book b3 = new Book(3, "PQR", 210, "Myth");
bookMap.put(b3, b3.getPrice());
Book b4 = new Book(5, "AB", 150, "Learning");
bookMap.put(b4, b4.getPrice());

for (Map.Entry bk : bookMap.entrySet()) {


Book b =(Book) bk.getKey();
System.out.println(b.getId()+" "+b.getBookName()+" "+b.getPrice()
+" "+b.getType()+"-----"+bk.getValue());

}
}

You might also like