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:
- Using System.out.println
- Using Entry
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
// Java program to demonstrate how to print LinkedHashMap of // custom class objects import java.util.*; // Custom class class Triangle { private String type; // Constructor public Triangle(String type) { this .type = type; } } public class GFG { public static void main(String[] args) { // New LinkedHashMap of custom class Triangle LinkedHashMap<Integer, Triangle> map = new LinkedHashMap<Integer, Triangle>(); // Add elements to LinkedHashMap map.put( 1 , new Triangle( "Equilateral triangle" )); map.put( 2 , new Triangle( "Isosceles triangle" )); map.put( 3 , new Triangle( "Right angled triangle" )); // Print map System.out.println(map); } } |
{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
// Java program to demonstrate how to print LinkedHashMap of // custom class objects import java.util.*; // Custom class class Triangle { private String type; // Constructor public Triangle(String type) { this .type = type; } // Override toString method to print human readable // information about the object public String toString() { return this .type; } } public class GFG { public static void main(String[] args) { // New LinkedHashMap of custom class Triangle LinkedHashMap<Integer, Triangle> map = new LinkedHashMap<Integer, Triangle>(); // Add elements to LinkedHashMap map.put( 1 , new Triangle( "Equilateral triangle" )); map.put( 2 , new Triangle( "Isosceles triangle" )); map.put( 3 , new Triangle( "Right angled triangle" )); // Print map System.out.println(map); } } |
{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
// Java program to demonstrate how to print LinkedHashMap of // custom class objects import java.util.*; // Custom class class Triangle { private String type; // Constructor public Triangle(String type) { this .type = type; } // Override toString method to print human readable // information about the object public String toString() { return this .type; } } public class GFG { public static void main(String[] args) { // New LinkedHashMap of custom class Triangle LinkedHashMap<Integer, Triangle> map = new LinkedHashMap<Integer, Triangle>(); // Add elements to LinkedHashMap map.put( 1 , new Triangle( "Equilateral triangle" )); map.put( 2 , new Triangle( "Isosceles triangle" )); map.put( 3 , new Triangle( "Right angled triangle" )); // Print map using Entry for (Map.Entry<Integer, Triangle> entry : map.entrySet()) { System.out.println( "Key: " + entry.getKey() + ", Value: " + entry.getValue()); } } } |
Key: 1, Value: Equilateral triangle Key: 2, Value: Isosceles triangle Key: 3, Value: Right angled triangle