How to Print LinkedHashMap Containing Custom Class Objects in Java?
Last Updated :
31 Aug, 2021
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
// 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);
}
}
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
// 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);
}
}
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
// 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());
}
}
}
OutputKey: 1, Value: Equilateral triangle
Key: 2, Value: Isosceles triangle
Key: 3, Value: Right angled triangle
Similar Reads
How to Print LinkedHashSet Elements in Java? LinkedHashSet is a child class of HashSet in which duplicates are not allowed but the insertion order is preserved. The elements are printed in the same order in which they were inserted. There are several ways to print LinkedHashSet elements: By simply printing the elementsBy using enhanced for loo
4 min read
How to Convert LinkedHashMap to List in Java? LinkedHashMap is predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, in LinkedHashMap insertion order is preserved. We to convert LinkedHashMap to ArrayList. A Map store data in pair of Key and Value while converting a LinkedHashMAp to ArrayLis
2 min read
How to Remove Elements from a LinkedHashMap in Java? A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove
2 min read
How to Print all Keys of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains a key and its respective value. Unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to print all the Keys present in our LinkedHashMap in java. We have to iterate through each Key in our LinkedHas
2 min read
How to Find User Defined Objects From LinkedHashSet in Java? LinkedHashSet is used to store elements in which order they were inserted. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were i
3 min read
How to Check if LinkedHashMap Contains a value in Java? LinkedHashMap is a predefined class in Java which is similar to HashMap, contains key and its respective value unlike HashMap, In LinkedHashMap insertion order is preserved. The task is to check if LinkedHashMap contains any value in java. to check we have to iterate through our LinkedHashMap and if
3 min read
How to Convert ArrayList to LinkedHashSet in Java? ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th
8 min read
How to Convert all LinkedHashMap Values to a List in Java? The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List
2 min read
How to Get All the Values of the LinkedHashMap in Java? LinkedHashMap is a predefined class in Java that is similar to HashMap, contains key and its respective value, unlike HashMap. In LinkedHashMap insertion order is preserved. The task is to get all the values present in our LinkedHashMap that is linked with their respective key. Use Iteration or pred
4 min read
How to Eliminate Duplicate User Defined Objects as a Key from Java LinkedHashMap? Duplicate user-defined objects as a key from Java LinkedHashMap can be removed and achieved by implementing equals and hashcode methods at the user-defined objects. Example: Input : LinkedHashMap = [{[Apple, 40], Kashmir}, {[Grapes, 80], Nashik}] Duplicate key = {[Grapes, 80], Delhi} Output: LinkedH
2 min read