Solved

Setting a HashMap to a Collection and vice versa

Posted on 2004-09-13
10
419 Views
Last Modified: 2008-01-09
Hi,

I have a HashMap called userSelection that I want to convert to a java Collection and then after it is passed into a new class, I want to change the collection back to a HashMap.  I work with collections and hashmaps all the time and have all the imports setup correctly, all I need is a few lines of code as an example to show how to convert these things back and forth.

Thanks in advance!! :)
0
Comment
Question by:gdlp2004
[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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12049835
HashMap is a Collection already.
If you want the keys then use userSelection.keySet()
Or to get the values use userSelection.values()
0
 
LVL 92

Expert Comment

by:objects
ID: 12049908
but as a HashMap is a subclass of Collection you can just say:

HashMap hm = new HasMap();
Collection c = hm;

Depends though exactly what it is you need to do.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 12049950
Yap thats correct just wanna add another comment what objects said with HasMap and Collections are true so the answers
that you need will already be on what objects said.

This tutorial can also add help :
http://java.sun.com/docs/books/tutorial/collections/index.html
http://java.sun.com/docs/books/tutorial/collections/interfaces/index.html

Hope that helps . . .
Javatm
0
Industry Leaders: 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 1

Expert Comment

by:Dave_Toland
ID: 12049985
don't forget you will need to cast back to a hashmap

HashMap hm = (HashMap)c;
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 12051148
HashMap is NOT a collection.  It is a MAP.  
A hasmap CONTAINS 3 related Sets (implements collection):
  -KeySet() - Set of keys
  -Values() - Set of values
  -entrySet() - Set of keys AND values (Map.Entry instances)

The only way to switch back and forth between collection and map is to use the entrySet() method:

Collection entries=m.entrySet();
...
HashMap map=new HashMap(entries.size())
for(Iterator it=entries.Iterator();it.hasNext();) {
  Map.Entry each=(Map.Entry) it.next();
  map.put(each.getKey(), each.getValue);
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12051301
> HashMap is NOT a collection.  It is a MAP.  

I stand corrected :)
0
 

Author Comment

by:gdlp2004
ID: 12054884
Before I test this out, I need to know how to set a HashMap to a collection...is it as easy as everyone originally posted, or is it more complex like MogalManic said?

0
 

Author Comment

by:gdlp2004
ID: 12054952
Oh nevermind, I see he did show the two separate ways...I will test them both out right now...
0
 

Author Comment

by:gdlp2004
ID: 12172396
Mogal Manic was right on the money...many thanks!
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12174167
no problem
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …

740 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