我找到了很多与这个问题相似的问题,但并不完全符合我的需求。对于许多专家来说,这个问题可能非常简单:我有一个Hashset
,其中包含我的自定义bean作为其内容:
Set<TableColumnBean> mySet = new HashSet<TableColumnBean>();
//loop and add values to Hashset
for(String str : tableColumnBeanMap.keySet()){
mySet.add(new TableColumnBean(tableColumnBeanMap.get(str).getTable, `tableColumnBeanMap.get(str).getColumn))`
}
这里的tableColumnBeanMap是一个Map,其中包含TableColumnBean
类型的bean列表。 TableColumnBean
是一个简单的bean,它有两个属性table和column。在循环结束时,我想要漂亮地打印出set的内容,这对于保留作为调试日志选项非常有用。我考虑使用java for循环遍历set并打印内容。但在其他地方,我使用了Jackson
库,例如:
new ObjectMapper().writeWithDefaultPrettyPrinter().writeValueAsString(myMap)
有没有类似于在地图上很好用的东西,也可以在集合上使用呢?(最好是像Jackson的pretty print那样的一行代码-因为它能够产生易于阅读的输出格式,而且不会使你的代码变得杂乱无章)。
new ObjectMapper().writeWithDefaultPrettyPrinter().writeValueAsString(mySet)
有什么问题? - Wilson