📜  如何在Java中打印包含自定义类对象的 LinkedHashMap?

📅  最后修改于: 2022-05-13 01:54:47.526000             🧑  作者: Mango

如何在Java中打印包含自定义类对象的 LinkedHashMap?

LinkedHashMap 就像 HashMap 一样,具有维护插入其中的元素顺序的附加功能。 HashMap 提供了快速插入、搜索和删除的优点,但它从未维护 LinkedHashMap 提供的插入轨迹和插入顺序,其中元素可以按插入顺序访问。

有两种方法可以在Java中打印包含自定义类对象的 LinkedHashMap:

  • 使用 System.out.println
  • 使用条目

方法一(使用 System.out.println() 方法)

打印 LinkedHashMap 的最简单方法是直接使用 System.out.println 方法,该方法使用 LinkedHashMap 类的 toString 方法。 toString 方法继承自 AbstractMap 类,返回由 { 和 } 括起来的映射的所有映射的字符串表示形式,以及用逗号分隔的每个键值对。但是,如果 LinkedHashMap 包含自定义类的对象作为键或值,则 Object 类的 toString 方法打印类名,后跟 @,后跟非人类可读的对象的哈希码。下面是实现:

示例 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 map
            = new LinkedHashMap();
 
        // 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);
    }
}


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 map
            = new LinkedHashMap();
 
        // 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);
    }
}


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 map
            = new LinkedHashMap();
 
        // 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 entry :
             map.entrySet()) {
            System.out.println("Key: " + entry.getKey()
                               + ", Value: "
                               + entry.getValue());
        }
    }
}


输出
{1=Triangle@214c265e, 2=Triangle@448139f0, 3=Triangle@7cca494b}

但是我们可以通过覆盖自定义类中的 toString() 方法以人类可读的形式打印它。下面是实现:

示例 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 map
            = new LinkedHashMap();
 
        // 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}

方法二(使用Map.Entry)

我们可以使用 Entry 在Java中打印包含自定义类对象的 LinkedHashMap。下面是实现:

例子:

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 map
            = new LinkedHashMap();
 
        // 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 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