473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Array List to HashMap

karthickkuchanur
156 100+
Hi,

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
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6.  
  7.   public class Test
  8.   {
  9.     public static void main(String[] args)
  10.     {
  11.       List<String> list = new ArrayList<String>();
  12.       list.add("A");
  13. list.add("A");
  14. list.add("B");
  15.  
  16.       list.add("C");
  17.       list.add("C");
  18.       ArrayList<String> tempList = new ArrayList<String>();
  19.       HashMap<Integer,ArrayList<String>> hashMap = new HashMap<Integer,ArrayList<String>>();
  20.       int count = 1;
  21.       for (Iterator iterator = list.iterator(); iterator.hasNext();)
  22.       {        
  23.         String string = (String) iterator.next();
  24.  
  25.         for (int i = 0; i <list.size(); i++)
  26.         {
  27.           if (!hashMap.isEmpty() )
  28.           {
  29.             tempList = hashMap.get(i);
  30.             if (tempList !=null && tempList.contains(string)) 
  31.             {
  32.               tempList.add(string);
  33.               hashMap.put((Integer)count, tempList);
  34.             }
  35.           } 
  36.           else {
  37.             tempList.add(string);
  38.             hashMap.put((Integer)count, tempList);
  39.           }
  40.         }
  41.         count++;
  42.       }
  43.       System.out.println(hashMap);
  44.     }
  45.  
  46.   }
  47.  
  48.  

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
Aug 1 '12 #1
0 1598

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jonny | last post by:
Hey I need to search an array list to remove duplicate Data The list is populated by a search function that enters in a name if the it is told too This causes duplicate names to be entered...
1
by: Phenix Smith | last post by:
I am looking at coverting an application over to C#. Currently I have a global list varible that needs to be converted. I have tested different ideas to accomplish the same idea. Ths best way I...
13
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
18
by: Doug Bell | last post by:
Hi, I have a function that loads Rows from a field in a DataTable into an ArrayList. See below: It also adds a Row (eg "All Areas"). I wanted this new row to be the first record so I added a...
3
by: Hrvoje Voda | last post by:
DataRow drow = db.dataSetLogo.Logo.Select("LogoName = '" + cPicList.SelectedItem.ToString() + "'"); if (drow.Length > 0) { try {
3
by: Ant | last post by:
Hi, I'm using an array list as the collection for my indexer (_alPeople). When i try to set _alpeople to a value at a certain index, I get an error. I can't simply add the value as it needs to be...
2
by: hannahs | last post by:
I'll try to explain this the best I can. I have a class called book which creates books that each hold information such as author, title, ISBN code, etc. Then I have another class called...
22
by: Energizer100 | last post by:
Hey. I'm having some difficulty in Array lists. I have to find the average of all the numbers in an array list. That's my first task. My second one is to find the mode of all the numbers, meaning the...
3
dlite922
by: dlite922 | last post by:
I have a list of objects, the object has a String and an Int in it. I need to check to see if an item is in the list, if so increment the integer and save it back to the list. I believe I've done...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.