找到你要的答案

Q:Java - printing textual representation of objects in sets

Q:java打印的文本表示的对象的集合

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

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.

我有一个地图作为字符串和值作为一组对象的键。

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

我有这样的东西到目前为止:

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

这只是用对象标识打印出密钥。

answer1: 回答1:

You have to override the toString method from your class.

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:

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:

  • Eclipse: Source > Generate toString()...
  • IntelliJ IDEA: Code > Generate... > toString()

你必须重写toString方法从您的类。

例如.:

类名

class 类名 {

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

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

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

}

和包含主方法的类:

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<类名>> aMap = new HashMap<>();
        aMap.put("firstKey", new HashSet<类名>() {
            {
                add(new 类名(1, "a"));
                add(new 类名(2, "b"));
            }
        });
        aMap.put("secondKey", new HashSet<类名>() {
            {
                add(new 类名(3, "c"));
            }
        });
        for (String eachKey : aMap.keySet()) {
            System.out.println(eachKey + " :" + aMap.get(eachKey));
        }
    }

}

输出将:

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

注意,通常你的IDE生成toString方法为你的能力:

  • Eclipse: Source > Generate toString()...
  • IntelliJ IDEA: Code > Generate... > toString()
answer2: 回答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.

覆盖在对象类的tostring()方法。系统的println()称tostring()的对象,对象的类将只打印类名”如果你没有在你的类定义tostring()方法。

java