2012-05-12 54 views
0

我在Java中的Hashmap中有一个关键值对,我以下面的方式遍历Map。在散列图中摆脱逗号

ArrayList<String> mysection = new ArrayList<String>(); 
    ArrayList<String> temp = new ArrayList<String>(); 

    Iterator it = Map.entrySet().iterator(); 

    while (it.hasNext()){ 
     Map.Entry pairs = (Map.Entry)it.next();     
     System.out.println(" = " + pairs.getValue());     

     mysection.add(pairs.getKey().toString()); 
     temp.add(pairs.getValue().toString()); 

     it.remove(); // avoids a ConcurrentModificationException   

    } 

这看起来不错,但循环运行一次并创建一个与密钥关联的长串值。我不想那样。我希望每个值都存储在ArrayList中,但不会以这种方式发生。所有值都存储在第一个索引本身中。

其次,即使我尝试分叉,我也找不到合适的方法。首先,我想通过“,”运算符分割字符串,但字符串本身包含多个“,”请建议我应该专门为第一个问题做些什么,我不希望所有值都是单个字符串。

感谢

登录:

        05-12 12:14:01.387: I/System.out(433): EAMCET: caution on punctuality, 
      05-12 12:14:01.387: I/System.out(433): Delay by even a minute can cost one year for the aspirants, warn the officials making it clear that they need to report at least 45 minutes in advance for the EAMCET to be held on May 12. Th... 
      05-12 12:14:01.387: I/System.out(433): , 
      05-12 12:14:01.387: I/System.out(433): Shankar Rao takes on Kiran , 
      05-12 12:14:01.387: I/System.out(433): Former Minister P. Shankar Rao on Friday opened a new front in his fight against Chief Minister N. Kiran Kumar Reddy by submitting a notice of breach of privilege against the latter for preventing... 
      05-12 12:14:01.387: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Police fear more Maoist attacks , 
      05-12 12:14:01.458: I/System.out(433): Uneasy calm prevails in villages tucked away along the shores of the Godavari adjoining neighbouring Chhattisgarh after the Maoists conducted a â??Praja Courtâ? in the interior Mukunur village of Maha... 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): 'Science in danger of regressing' , 
      05-12 12:14:01.458: I/System.out(433): Askok Ganguly delivers 'A.V. Rama Rao Tech Award Lecture' 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Global firms pick up ISB students , 
      05-12 12:14:01.458: I/System.out(433): Average annual salary offered is Rs.18.83 lakh, 8 p.c. more than last year 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Telugu varsity to make its courses job-oriented, 
      05-12 12:14:01.458: I/System.out(433): Potti Sreeramulu Telugu University is planning to attract more students to pursue higher education by offering employment oriented courses.The university was exploring chances to embed info... 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): Kiran sharpens attack on Jagan, 
      05-12 12:14:01.458: I/System.out(433): Ruling Congress has launched a three-cornered attack on its two arch rivals -- YSRCP president Y.S. Jaganmohan Reddy and TDP chief N. Chandrababu Naidu at Tirupati on Friday in its run up to the b... 
      05-12 12:14:01.458: I/System.out(433): , 
      05-12 12:14:01.458: I/System.out(433): RINL to get Central nod soon for mining in Rajasthan, 
      05-12 12:14:01.458: I/System.out(433): The Centre will give its nod soon for granting lease for iron ore mining to Rashtriya Ispat Nigam Limited (RINL), the corporate entity of Visakhapatnam Steel Plant. 
      05-12 12:14:01.458: I/System.out(433): â??After the Rajasthan g... 

下面是输出的图像: enter image description here

+2

您应该添加代码以明确您的代码中的temp和mysection是什么 – Tom

+1

我们无法通过'mysection'或'temp'告诉您正在做什么。请给出一个简短的*完整的*程序(只是普通的Java)来说明问题。 –

+1

你应该解释你是如何得出这个结论的。 –

回答

0

好的,我已经完成了...实际上有很多方法可以遍历地图,但这并不是最适合我想要的。所以改变了现在其工作流畅的另一种方法的迭代实现。

1

我试图理解的代码。 我怀疑问题出在地图对象上,以及如何为它填充数据。从你的陈述中,也许你的地图只有一个长字符串的条目。

因此......至少,您可以发布地图数据填充代码。希望解决这个问题不会那么困难。