Solved

HashSet of HashMaps ... I need an example

Posted on 2006-07-13
8
444 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
8 Comments
 

Author Comment

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

Assisted Solution

by:CEHJ
CEHJ earned 100 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 100 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
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 26

Assisted Solution

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

Expert Comment

by:ksivananth
ID: 17104089
0
 
LVL 92

Assisted Solution

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

Assisted Solution

by:firas981
firas981 earned 100 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
Android development question 2 75
Java 8 to Java 6 8 32
Netbeans and org.apache.commons.lang3 issue 3 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

685 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