简体   繁体   English

Java集合-如何在集合中添加多个值?

[英]Java collections - How to add multiple values in the collection?

How to add multiple values into multihashmap, since its not genric.Please see the below code. 由于它不是泛型的,如何将多个值添加到multihashmap中,请参见以下代码。

Please look it expected output : Col1,col2,col3,col4 order of display values not matching with col5 order. 请查看它的预期输出:显示值的col1,col2,col3,col4顺序与col5顺序不匹配。 Could you please advise me to handle all the values within multihashmap. 您能否建议我处理multihashmap中的所有值。

Iterator<Search> iterator = pre.iterator();
    MultiHashMap mhm = new MultiHashMap();
    StringBuilder sb = new StringBuilder();
    List list = null;
    while(iterator.hasNext())       {
        Search prer = (Search)iterator.next();
        String product = prer.getProduct();
        sb.append(prer.getreqsNbr());
        sb.append(" ");
        sb.append(prer.getOp()));
        sb.append(" ");
        dependencies.put(product, sb.toString());
        sb.setLength(0);

        ***//This is for col1,col2,col3,col4***

        prer.getProductNbr()));
        prer.getProdDescr()));
        prer.getreqsNbr()));
        prer.getreqdescr()));
        }

    ***//This loop for get values as col5***

    Set set = mhm.entrySet();  
    Iterator i = set.iterator(); 
    while(i.hasNext()) { 
        Map.Entry me = (Map.Entry)i.next(); 
        list=(List)mhm.get(me.getKey()); 
        int itemCount = list.size();
        for (int z = 0; z < itemCount; z++) {
            String values = "";
            for(int j=0;j<list.size();j++)  { 
                values += list.get(j);      }
            System.out.println(me.getKey() + ": value :" + values); 
         }

Above program output: 上面的程序输出:

c1   c2    c3  c4 c5
120  xxxx  12 xxx 14
120  xxxx  13 xxx 14
120  xxxx  14 xxx 14
130  xxxx  14 xxx 12 13 14

Expected output : 预期产量:

Col1  col2   col3  col4    col5
120   xxxx    12   xxxx    12 13 14
120   xxxx    13   xxxx    12 13 14     
120   xxxx    14   xxxx    12 13 14
130   xxxx    14   xxxx    14 

Below setof code is working as expected(this for col5). 下面的setof代码按预期工作(col5可以正常工作)。

Set set = mhm.entrySet();  
    Iterator i = set.iterator(); 
    while(i.hasNext()) { 
        Map.Entry me = (Map.Entry)i.next(); 
        list=(List)mhm.get(me.getKey()); 
        int itemCount = list.size();
        for (int z = 0; z < itemCount; z++) {
            String values = "";
            for(int j=0;j<list.size();j++)  { 
                values += list.get(j);      }
            System.out.println(me.getKey() + ": value :" + values); 
         }

This part need to push multihash map and while displaying those are asociated each other (this is for col1,col2,col3,col4. 这部分需要推送multihash映射,并在显示时将它们彼此关联(这是针对col1,col2,col3,col4的。)。

prer.getProductNbr()));
prer.getProdDescr()));
prer.getreqsNbr()));
prer.getreqdescr()));

This code will print the values many times 此代码将多次打印值

    int itemCount = list.size();
    for (int z = 0; z < itemCount; z++) {
        String values = "";
        for(int j=0;j<list.size();j++)  { 
            values += list.get(j);
        }
        System.out.println(me.getKey() + ": value :" + values); 
     }

You probably want something like this: 您可能想要这样的东西:

String values = "";
for(int j=0;j<list.size();j++)  { 
  values += list.get(j) + " ";
}
System.out.println(me.getKey() + ": value :" + values); 

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

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