Java - 打印集合中对象的文本表示

时间:2022-10-25 00:11:38

I have a Map with keys as String and values as a set of objects.

我有一个Map,其键为String,值为一组对象。

How would I go about printing out the object associated with each individual key and its attributes?

我如何打印出与每个单独的键及其属性相关联的对象?

I have something like this so far:

到目前为止我有这样的事情:

for (String eachKey : aMap.keySet()) {
    System.out.println(eachKey + " :" + aMap.get(eachKey));
}

This just prints out the key with the object identity.

这只是打印出具有对象标识的密钥。

2 个解决方案

#1


You have to override the toString method from your class.

您必须从类中重写toString方法。

E.g.:

MyClass

class MyClass {

    int i = 1;
    String s = "test";

    MyClass(int i, String s) {
        this.i = i;
        this.s = s;
    }

    @Override
    public String toString() {
        return "MyObject [i=" + i + ", s=" + s + "]";
    }

}

and the class containing the main method:

以及包含main方法的类:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class A {

    @SuppressWarnings("serial")
    public static void main(String[] args) {
        Map<String, Set<MyClass>> aMap = new HashMap<>();
        aMap.put("firstKey", new HashSet<MyClass>() {
            {
                add(new MyClass(1, "a"));
                add(new MyClass(2, "b"));
            }
        });
        aMap.put("secondKey", new HashSet<MyClass>() {
            {
                add(new MyClass(3, "c"));
            }
        });
        for (String eachKey : aMap.keySet()) {
            System.out.println(eachKey + " :" + aMap.get(eachKey));
        }
    }

}

The output will be:

输出将是:

firstKey :[MyObject [i=2, s=b], MyObject [i=1, s=a]]
secondKey :[MyObject [i=3, s=c]]

Notice that usually your IDE has the ability to generate the toString method for you:

请注意,通常您的IDE可以为您生成toString方法:

  • Eclipse: Source > Generate toString()...
  • Eclipse:Source> Generate toString()...

  • IntelliJ IDEA: Code > Generate... > toString()
  • IntelliJ IDEA:代码>生成...> toString()

#2


Override the toString() method in Object class. System.out.println() will call toString() of the object, the Object class will only print classname@ if you have not defined toString() method in your class.

覆盖Object类中的toString()方法。 System.out.println()将调用对象的toString(),如果您没有在类中定义toString()方法,则Object类将只打印classname @。

#1


You have to override the toString method from your class.

您必须从类中重写toString方法。

E.g.:

MyClass

class MyClass {

    int i = 1;
    String s = "test";

    MyClass(int i, String s) {
        this.i = i;
        this.s = s;
    }

    @Override
    public String toString() {
        return "MyObject [i=" + i + ", s=" + s + "]";
    }

}

and the class containing the main method:

以及包含main方法的类:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class A {

    @SuppressWarnings("serial")
    public static void main(String[] args) {
        Map<String, Set<MyClass>> aMap = new HashMap<>();
        aMap.put("firstKey", new HashSet<MyClass>() {
            {
                add(new MyClass(1, "a"));
                add(new MyClass(2, "b"));
            }
        });
        aMap.put("secondKey", new HashSet<MyClass>() {
            {
                add(new MyClass(3, "c"));
            }
        });
        for (String eachKey : aMap.keySet()) {
            System.out.println(eachKey + " :" + aMap.get(eachKey));
        }
    }

}

The output will be:

输出将是:

firstKey :[MyObject [i=2, s=b], MyObject [i=1, s=a]]
secondKey :[MyObject [i=3, s=c]]

Notice that usually your IDE has the ability to generate the toString method for you:

请注意,通常您的IDE可以为您生成toString方法:

  • Eclipse: Source > Generate toString()...
  • Eclipse:Source> Generate toString()...

  • IntelliJ IDEA: Code > Generate... > toString()
  • IntelliJ IDEA:代码>生成...> toString()

#2


Override the toString() method in Object class. System.out.println() will call toString() of the object, the Object class will only print classname@ if you have not defined toString() method in your class.

覆盖Object类中的toString()方法。 System.out.println()将调用对象的toString(),如果您没有在类中定义toString()方法,则Object类将只打印classname @。