How to Print LinkedHashMap Containing Custom Class Objects in Java?
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements can be accessed in their insertion order.
There are two ways to print LinkedHashMap containing custom class objects in Java:
Method 1(Using System.out.println() method)
The simplest way to print LinkedHashMap is by using the System.out.println method directly which uses the toString method of the LinkedHashMap class. The toString method, which is inherited from the AbstractMap class, returns a string representation of all mappings of the map enclosed by { and } and each key-value pair separated by a comma. But if the LinkedHashMap contains objects of a custom class as keys or values, the toString method of the Object class prints the class name, followed by @, followed by hash code of the object which is not human-readable. Below is the implementation:
Example 1:
Java
import java.util.*;
class Triangle {
private String type;
public Triangle(String type) { this .type = type; }
}
public class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Triangle> map
= new LinkedHashMap<Integer, Triangle>();
map.put( 1 , new Triangle( "Equilateral triangle" ));
map.put( 2 , new Triangle( "Isosceles triangle" ));
map.put( 3 , new Triangle( "Right angled triangle" ));
System.out.println(map);
}
}
|
Output
{1=Triangle@214c265e, 2=Triangle@448139f0, 3=Triangle@7cca494b}
But we can print it in human-readable form by overriding the toString () method in the custom class. Below is the implementation:
Example 2:
Java
import java.util.*;
class Triangle {
private String type;
public Triangle(String type) { this .type = type; }
public String toString() { return this .type; }
}
public class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Triangle> map
= new LinkedHashMap<Integer, Triangle>();
map.put( 1 , new Triangle( "Equilateral triangle" ));
map.put( 2 , new Triangle( "Isosceles triangle" ));
map.put( 3 , new Triangle( "Right angled triangle" ));
System.out.println(map);
}
}
|
Output
{1=Equilateral triangle, 2=Isosceles triangle, 3=Right angled triangle}
Method 2(Using Map.Entry)
We can print LinkedHashMap containing custom class objects in Java using Entry. Below is the implementation:
Example:
Java
import java.util.*;
class Triangle {
private String type;
public Triangle(String type) { this .type = type; }
public String toString() { return this .type; }
}
public class GFG {
public static void main(String[] args)
{
LinkedHashMap<Integer, Triangle> map
= new LinkedHashMap<Integer, Triangle>();
map.put( 1 , new Triangle( "Equilateral triangle" ));
map.put( 2 , new Triangle( "Isosceles triangle" ));
map.put( 3 , new Triangle( "Right angled triangle" ));
for (Map.Entry<Integer, Triangle> entry :
map.entrySet()) {
System.out.println( "Key: " + entry.getKey()
+ ", Value: "
+ entry.getValue());
}
}
}
|
Output
Key: 1, Value: Equilateral triangle
Key: 2, Value: Isosceles triangle
Key: 3, Value: Right angled triangle
Last Updated :
31 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...