1.I am having the arraylist which contains string like A,A,B,C,C
2.I need to separe the unique value in to map
3.The output must be like {1=[A, A]},{2=[b]},{3=[C, C]}
4.I tried something hardly but i can't
Expand|Select|Wrap|Line Numbers
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- public class Test
- {
- public static void main(String[] args)
- {
- List<String> list = new ArrayList<String>();
- list.add("A");
- list.add("A");
- list.add("B");
- list.add("C");
- list.add("C");
- ArrayList<String> tempList = new ArrayList<String>();
- HashMap<Integer,ArrayList<String>> hashMap = new HashMap<Integer,ArrayList<String>>();
- int count = 1;
- for (Iterator iterator = list.iterator(); iterator.hasNext();)
- {
- String string = (String) iterator.next();
- for (int i = 0; i <list.size(); i++)
- {
- if (!hashMap.isEmpty() )
- {
- tempList = hashMap.get(i);
- if (tempList !=null && tempList.contains(string))
- {
- tempList.add(string);
- hashMap.put((Integer)count, tempList);
- }
- }
- else {
- tempList.add(string);
- hashMap.put((Integer)count, tempList);
- }
- }
- count++;
- }
- System.out.println(hashMap);
- }
- }
Finally the 5 values int the ArrayList need to group by 3 ,i will take the value from the key of hash map .Please help me