Solved

convert a ArrayList to a Hashmap

Posted on 2006-11-07
7
3,469 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 62
windows explorer path to command prompt 5 45
runtime exception 2 29
Crystal Reports Licensing Questions 4 13
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

823 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