Solved

HashSet of HashMaps ... I need an example

Posted on 2006-07-13
8
449 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

617 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