• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Setting a HashMap to a Collection and vice versa

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
gdlp2004
Asked:
gdlp2004
  • 3
  • 3
  • 2
  • +2
1 Solution
 
objectsCommented:
HashMap is a Collection already.
If you want the keys then use userSelection.keySet()
Or to get the values use userSelection.values()
0
 
objectsCommented:
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
 
JavatmCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Dave_TolandCommented:
don't forget you will need to cast back to a hashmap

HashMap hm = (HashMap)c;
0
 
MogalManicCommented:
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
 
objectsCommented:
> HashMap is NOT a collection.  It is a MAP.  

I stand corrected :)
0
 
gdlp2004Author Commented:
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
 
gdlp2004Author Commented:
Oh nevermind, I see he did show the two separate ways...I will test them both out right now...
0
 
gdlp2004Author Commented:
Mogal Manic was right on the money...many thanks!
0
 
MogalManicCommented:
no problem
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now