📜  在Java中打印具有自定义类对象作为键或值的 TreeMap

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

在Java中打印具有自定义类对象作为键或值的 TreeMap

TreeMap 是一种映射实现,它根据其键的自然顺序对其条目进行排序。因此,对于整数,这将意味着升序,而对于字符串,它将是字母顺序。 TreeMap 也可以在构建时使用比较器根据用户进行排序。

在这里,我们将看到如何打印具有自定义类对象作为键或值的 TreeMap。但在此之前,让我们看看尝试正常打印时究竟会发生什么。在尝试打印对象时,直接打印的值可能格式不正确,并且在多种情况下会在输出中打印垃圾值,例如 @agc1243。

普通打印实现:

Java
// Printing TreeMap Having Custom Class Objects directly
import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
  
// make a class Student
class Student {
  
    private Integer regId;
    private String name;
  
    public Student(Integer regId, String name)
    {
        this.regId = regId;
        this.name = name;
    }
  
    public Integer getId() { return regId; }
  
    public String getName() { return name; }
}
class GFG {
    public static void main(String[] args)
    {
  
        // creating a TreeMap
        TreeMap tmap
            = new TreeMap();
  
        // Mapping student objects to int keys
        tmap.put(1, new Student(101, "Abhay"));
        tmap.put(2, new Student(102, "Sarika"));
        tmap.put(3, new Student(103, "Vanshika"));
  
        // get all entries
        Set > entries
            = tmap.entrySet();
  
        // printing keys and values using for loop
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey() + "=>"
                               + entry.getValue());
        }
    }
}


Java
// Printing TreeMap Having Custom Class
// Objects as Keys or Values in Java
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
  
// make a class Student
class Student {
  
    private Integer regId;
    private String name;
  
    public Student(Integer regId, String name)
    {
        this.regId = regId;
        this.name = name;
    }
  
    public Integer getId() { return regId; }
  
    public String getName() { return name; }
    /*
     * Override this method in your custom class
     * used as key or value in the TreeMap
     */
    public String toString()
    {
        return "[" + this.getId() + "=>" + this.getName()
            + "]";
    }
}
class GFG {
    public static void main(String[] args)
    {
  
        // creating a TreeMap
        TreeMap tmap
            = new TreeMap();
  
        // Mapping student objects to int keys
        tmap.put(1, new Student(101, "Abhay"));
        tmap.put(2, new Student(102, "Sarika"));
        tmap.put(3, new Student(103, "Vanshika"));
  
        // get all entries
        Set > entries
            = tmap.entrySet();
  
        // printing keys and values using for loop
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey() + "=>"
                               + entry.getValue());
        }
    }
}


输出
1=>Student@3941a79c
2=>Student@506e1b77
3=>Student@4fca772d

输出格式不正确的原因是什么?

原因是我们的Student类没有覆盖Object类中的toString方法。这就是为什么这里使用 Object 类的 toString 方法的原因,它在打印对象时打印“class_name@object_hashcode”。

那么,下一个问题是如何解决这个问题?

要解决此问题,请覆盖 Student 类中的toString方法。

执行:

Java

// Printing TreeMap Having Custom Class
// Objects as Keys or Values in Java
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
  
// make a class Student
class Student {
  
    private Integer regId;
    private String name;
  
    public Student(Integer regId, String name)
    {
        this.regId = regId;
        this.name = name;
    }
  
    public Integer getId() { return regId; }
  
    public String getName() { return name; }
    /*
     * Override this method in your custom class
     * used as key or value in the TreeMap
     */
    public String toString()
    {
        return "[" + this.getId() + "=>" + this.getName()
            + "]";
    }
}
class GFG {
    public static void main(String[] args)
    {
  
        // creating a TreeMap
        TreeMap tmap
            = new TreeMap();
  
        // Mapping student objects to int keys
        tmap.put(1, new Student(101, "Abhay"));
        tmap.put(2, new Student(102, "Sarika"));
        tmap.put(3, new Student(103, "Vanshika"));
  
        // get all entries
        Set > entries
            = tmap.entrySet();
  
        // printing keys and values using for loop
        for (Map.Entry entry : entries) {
            System.out.println(entry.getKey() + "=>"
                               + entry.getValue());
        }
    }
}
输出
1=>[101=>Abhay]
2=>[102=>Sarika]
3=>[103=>Vanshika]

注意:所以这里要记住的主要提示是始终覆盖自定义类中的 toString() 方法。