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

x
?
Solved

HashSet of HashMaps ... I need an example

Posted on 2006-07-13
8
Medium Priority
?
455 Views
Last Modified: 2012-05-05
I need an good, complete, working example of how to implement a set of HashMaps as a HashSet ... either a link to a tutorial with source code or some clear source code would be very helpful.

I'm trying to implement a set of tables in the form of ...

int   double
1    2.3
2    3.3

I will have many of these tables.
0
Comment
Question by:polkadot
[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
8 Comments
 

Author Comment

by:polkadot
ID: 17103101
btw, Im using java 1.4.2
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 17103306
In < 1.5 you need to use wrapper classes for primitives

Set maps = new HashSet();
Map m1 = new HashMap();
m1.put(new Integer(1), new Double(2.3));
maps.add(m1);

Map m2 = new HashMap();
m2.put(new Integer(2), new Double(3.3));
maps.add(m2);
0
 
LVL 20

Accepted Solution

by:
dopyiii earned 400 total points
ID: 17103313
Thinking in Java (http://www.mindview.net) is always good.

Here's a simple (yet complete) working example of how to do a HashSet of HashMaps:

import java.util.*;

public class sethash
{
   public static void main(String args[])
   {
      // setup the hashmap and hashset
       Map<Integer, Float> foo = new HashMap<Integer, Float>();
       Set<Map> bar = new HashSet<Map>();

      // stuff some stuff into the hashmap
      foo.put(new Integer(1), new Float(2.3));
      foo.put(new Integer(2), new Float(3.3));

     // stuff that into a hashmap
      bar.add(foo);

     // show me that it's all in there
     System.out.println(bar.toString());
   }
}

You can then operate on the hashset like any other, grabbing the hashmaps and extracting their data as necessary using iterators.

This is the 1.5.x way of doing things, but the only real difference is that in 1.5 their more type safe (thus requiring the explicit type identifiers).  You can do something ilke this in 1.4 and you won't get the errors that you will in 1.5 (but explicit type safety is always a good thing in my mind):

HasMap foo = new HashMap();
HashSet bar = new HashSet();
bar.put(1, 2.3);
bar.put(2, 3.3);
foo.add(bar);
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 26

Assisted Solution

by:ksivananth
ksivananth earned 400 total points
ID: 17104077
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17104089
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 17104134
0
 

Assisted Solution

by:firas981
firas981 earned 400 total points
ID: 17120532
Does this usecase satisfy you (regardless of the implementation for now)?

            //create the set of tables
                SetOfTables s = new SetOfTables();
            s.addTable("table1", new int[]{1,2},new double[]{1.1,2.2});
            s.addTable("table2", new int[]{3,4},new double[]{3.1,4.1});
            
            //in another place, use the set of tables
            for (Iterator iter = s.iterator(); iter.hasNext();) {
                  Integer key =  (Integer)iter.next();//keys from one table got successive
                  Double value = s.getValue();
                  String tableName = s.getTable();
                  //use them someway
                  System.out.println(tableName+" "+key+" "+value);
            }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17168634
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

688 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