HTML Java

Java Map


Map

  • Map is an interface it’s a part of collection but not inherits the collection interface.
  • Because map is the one and only collection which is different than any other collections.
  • It has features of storing value in key value pair and every collection store in form of objects or in a single value.

Implementation of Map


HashMap()

  • Linked HashMap() they are the implementing classes of the map interface.
  • To store key internally set is used and to store values internally list is used.
  • Child interface of map is sorted map and implementing class of Sorted Map is tree map.

And we call entry of 1 pair of key and value.

1 ---coder

Key is 1 and value is coder and pair of this called entry and to maintain this entry nested interface of map Interface entry interface is used why this interface is used to store pair of key values if u want to fetch all data of map so the type of pair of key value is entry type and entry is nested interface of Map.

Interface Map{
Interface Entry{
}
}

If you want to use nested interface then you write first name of parent interface then child interface you can’t use entry directly (Map.Entry) inside map type of pair of key value is entry type.

If you want to fetch data from map and you want only key then you run map method of keyset and hold it in the variable of Set and if u want values then you can run values method and hold into the list variable but if you want all entry then pair of key value and this will be an entry and to fetch that entry in Map interface 1 method is entry and when this method return data of map then we typecast into (Map.entry).

Anonymous Inner class: in this we create an interface but we are not implementing this interface in our main class we are creating Anonymous Inner class and for A parent Interface must be there like in my case I interface is there so when we compile it then 3 class file will be created 1 for interface I and another for

AnonymousClassUsingLambdaExpression and 1 more class file is AnonymousClassUsingLambdaExpression$1 so in this case 3 file is generated we can solve this memory wastage by using lambda expression.

Example

interface I{
void show();
}
public class AnonymousClassUsingLambdaExpression {
public static void main(String[] args) {
I i = new I() {
public void show() {
System.out.println("Exmaple Of Anonymous Inner Class");
}
};
i.show();

}

}
Try it »