Solved

HashSet of HashMaps ... I need an example

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 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