Solved

convert a ArrayList to a Hashmap

Posted on 2006-11-07
7
3,433 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
> 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
Comment Utility
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
Comment Utility
> 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
Comment Utility
thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now