简体   繁体   English

如何在java中打印String数组的ArrayList,这是Map的值

[英]How to print a ArrayList of String arrays in java which is a value of Map

I want to print the content of this attribute: 我想打印此属性的内容:

private HashMap<RegionVersObj,  ArrayList<String[]>> region;

I have done this with the below code. 我已经用下面的代码做到了。 Inside the toString( ) method I am iterating with the Map Entry. toString( )方法中,我正在迭代Map Entry。 I have created a StringBuffer object and appending the content in it. 我创建了一个StringBuffer对象,并将内容附加在其中。

 public String toString() {     
        StringBuffer regionToPrint = new StringBuffer();
          for (Map.Entry<RegionVersObj,  ArrayList<String[]>> entry : region.entrySet())
            {
              regionToPrint.append(entry.getKey().toString());
              regionToPrint.append("=[");
              for(String[] s:entry.getValue()){
                  regionToPrint.append("[");
                  for(String s1:s){
                      regionToPrint.append(s1);
                      regionToPrint.append(",");
                  }
                  regionToPrint.append("],");
              }
        }

        return "region=" + regionToPrint.toString();
    }

This is the way I am trying to solving this. 这就是我试图解决此问题的方法。 But I want to know is there any better way in which I can solve this? 但是我想知道有什么更好的方法可以解决这个问题吗?

Kindly use the JAVA 1.8. 请使用JAVA 1.8。 Since it has the more in build functions. 由于它具有更多内置功能。 Example we can make the String[] array into comma separated values using String.join() 示例我们可以使用String.join()String[]数组转换为逗号分隔的值

 region.forEach((k,v)->{
              regionToPrint.append(k.toString());
              regionToPrint.append("=[");
              for(String[] s:v){
                  regionToPrint.append("[");
                  regionToPrint.append(String.join(",", s));
                  regionToPrint.append("],");
              }
              regionToPrint.append("],");
        }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM