This example shows how to print LinkedHashMap elements in Java. This example also shows how to print all keys, all values, and all mappings of LinkedHashMap using various ways.
How to print LinkedHashMap in Java?
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.
The order of the entries is determined by the iterator of the entry set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.LinkedHashMap; public class JavaPrintLinkedHashMapExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(1, "One"); lhm.put(2, "Two"); lhm.put(3, "Three"); /* * The toString method returns all mappings of * map where keys and values are separated by a =, * each mapping is separated by a comma and all * mappings are enclosed in { and }. */ System.out.println( lhm ); } } |
Output
1 |
{1=One, 2=Two, 3=Three} |
How to print all keys of the LinkedHashMap?
To print all keys of the LinkedHashMap, use the keySet
method to get all the keys of the map.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(1, "One"); lhm.put(2, "Two"); lhm.put(3, "Three"); //get all keys using the keySet method Set<Integer> allKeys = lhm.keySet(); //print keys System.out.println( allKeys ); |
Output
1 |
[1, 2, 3] |
If you want to change the output format, you can also iterate the keys and print them one by one.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(1, "One"); lhm.put(2, "Two"); lhm.put(3, "Three"); //get all keys using the keySet method Set<Integer> allKeys = lhm.keySet(); for(Integer key : allKeys){ System.out.println( key ); } |
Output
1 2 3 |
1 2 3 |
How to print all keys of the LinkedHashMap?
To print all values of the LinkedHashMap object, use the values
method which returns a Collection containing all the values contained in the LinkedHashMap object.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(1, "One"); lhm.put(2, "Two"); lhm.put(3, "Three"); //get all values using the values method Collection<String> values = lhm.values(); //print System.out.println( values ); |
Output
1 |
[One, Two, Three] |
Again, if you want to change the output format, you can iterate through the values of the LinkedHashMap and print them one by one.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(1, "One"); lhm.put(2, "Two"); lhm.put(3, "Three"); //get all values using the values method Collection<String> values = lhm.values(); for(String value : values){ System.out.println( value ); } |
Output
1 2 3 |
One Two Three |
How to print all mappings of the LinkedHashMap?
To print all mappings of the LinkedHashMap, use the entrySet
method which returns all the entries contained in the LinkedHashMap object.
1 2 3 4 5 6 7 8 9 |
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(1, "One"); lhm.put(2, "Two"); lhm.put(3, "Three"); for( Map.Entry<Integer, String> entry : lhm.entrySet() ){ System.out.println( entry.getKey() + "=>" + entry.getValue() ); } |
Output
1 2 3 |
1=>One 2=>Two 3=>Three |
How to print LinkedHashMap containing custom class objects?
If the LinkedHashMap contains objects of a custom class as keys or values, then the custom class should implement the toString
method for the output to be in human-readable format.
If the custom class does not override the toString
method, the toString
method of the Object class is used which prints the class name, followed by @, followed by hash code of the object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Box{ private String type; public Box(String type){ this.type = type; } } public class JavaPrintLinkedHashMapExample { public static void main(String[] args) { LinkedHashMap<Integer, Box> lhm = new LinkedHashMap<Integer, Box>(); lhm.put(1, new Box("Wooden Box")); lhm.put(2, new Box("Metal Box")); System.out.println( lhm ); } } |
Output
1 |
{1=com.javacodeexamples.collections.linkedhashmap.Box@15db9742, 2=com.javacodeexamples.collections.linkedhashmap.Box@6d06d69c} |
As you can see from the output, the printed information is not very useful. Now, let’s override the toString
method and try again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Box{ private String type; public Box(String type){ this.type = type; } /* * Override this method to print * useful information about the object */ public String toString(){ return this.type; } } public class JavaPrintLinkedHashMapExample { public static void main(String[] args) { LinkedHashMap<Integer, Box> lhm = new LinkedHashMap<Integer, Box>(); lhm.put(1, new Box("Wooden Box")); lhm.put(2, new Box("Metal Box")); System.out.println( lhm ); } } |
Output
1 |
{1=Wooden Box, 2=Metal Box} |
Tip: Always override the toString
method in your custom classes.
This example is a part of the Java LinkedHashMap tutorial with examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap