gpt4 book ai didi

java - 映射到 Java 中的字符串

转载 作者:IT老高 更新时间:2023-10-28 11:53:29 25 4
gpt4 key购买 nike

当我在 Java 中执行 System.out.println(map) 时,我在标准输出中得到了很好的输出。如何在不干预标准输出的情况下在变量中获得 Map 的相同字符串表示形式?类似 String mapAsString = Collections.toString(map)?

最佳答案

使用 Object#toString() .

String string = map.toString();

毕竟这也是 System.out.println(object) 所做的under the hoods . map 的格式在 AbstractMap#toString() 中描述。 .

Returns a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).

关于java - 映射到 Java 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2828252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com