Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert a ArrayList to a Hashmap

Posted on 2006-11-07
7
Medium Priority
?
3,547 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 1600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

670 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