Solved

convert a ArrayList to a Hashmap

Posted on 2006-11-07
7
3,498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pagenation logic how it is working in my code 1 80
Which non-HTML GUI front end to use with Java? 3 64
collection output issue 9 89
DTD and JAVA versions 1 53
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …
Suggested Courses

739 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