Solved

HashSet of HashMaps ... I need an example

Posted on 2006-07-13
8
440 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
Comment Utility
btw, Im using java 1.4.2
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
Comment Utility
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
Comment Utility
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 100 total points
Comment Utility
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
Comment Utility
0
 

Assisted Solution

by:firas981
firas981 earned 100 total points
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 59
how to use external config file with Spring MVC 4 59
json format text only 4 65
Problem to start Neon 20 49
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now