Solved

convert a ArrayList to a Hashmap

Posted on 2006-11-07
7
3,477 Views
Last Modified: 2008-02-01
hi experts

i have a requirment where i have to convert a ArrayList to a Hashmap

want to know if thats possible

here is a code snippet
ArrayList judgeObjects = (ArrayList)request.getAttribute("judges"); //judges is of type list
Iterator judgeIterator=  judgeObjects.iterator();
while (judgeIterator.hasNext ()) {
    HashMap judegenames = judgeIterator.next(); //want to save ArrayList objects in Hashmap.// Any ideas ?....

....
}
0
Comment
Question by:jaggernat
  • 4
  • 3
7 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 400 total points
ID: 17892257
try this:

Map map = new HashMap();
ArrayList judgeObjects = (ArrayList)request.getAttribute("judges");Iterator judgeIterator=  judgeObjects.iterator();
while (judgeIterator.hasNext ()) {
    HashMap judegenames = judgeIterator.next(); //want to save ArrayList objects in Hashmap.// Any ideas ?....
    map.putAll(judegenames);
....
}
0
 
LVL 10

Author Comment

by:jaggernat
ID: 17892353
works ..thanks.

one general question.

is ther any specific use of Hashmap over arraylist or vice-versa.
Can i just use Arraylist instead of using Hashmap in while loop
 (  HashMap judegenames = judgeIterator.next();
    map.putAll(judegenames);
 )
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17892616
> is ther any specific use of Hashmap over arraylist or vice-versa.
HashMap (or Map in general) is used to store key - value pairs whereas List stores list of objects
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17892636
> Can i just use Arraylist instead of using Hashmap in while loop
Well depends on the usage, if you want only either key or values of the judegenames, then you can use ArrayList
otherwise Map should be used
0
 
LVL 10

Author Comment

by:jaggernat
ID: 17892664
while (judgeIterator.hasNext ()) {
    HashMap judegenames = judgeIterator.next(); //want to save ArrayList objects in Hashmap.// Any ideas ?....
    map.putAll(judegenames);
....
}



for the above code , can i just use arraylist instead of hashmap. which one is a better choice

thanks
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17892695
> for the above code , can i just use arraylist instead of hashmap. which
> one is a better choice
see above
0
 
LVL 10

Author Comment

by:jaggernat
ID: 17892701
thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factorial example 4 48
MySqlDump not dumping triggers 1 43
Which non-HTML GUI front end to use with Java? 3 24
Is Applet the way to go for my drag and drop system? 8 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question