Solved

Java HashTable and TreeMap

Posted on 2011-02-11
4
1,182 Views
Last Modified: 2012-05-11
Hello,

I want to maintain table like below in JAVA.

S1                X1      Y1      FLAG1      Date
                    X2      Y2      FLAG2      Date
                     X1      Y3      FLAG1      Date
S2      X1      Y1      FLAG1      Date
                     X2      Y2      FLAG2      Date
                     X1      Y3      FLAG3      Date

I having data in (s, x, y, flag) format.Before inserting data I want to perform certain validation like
1) If the same entry is already present, then create new entry
2) If same (s, x) is found in table but flag is different then check all the entry for y less then equal to "new y" & remove those entries.


I want to perform insertion operation on regular interval.

I came to know HashTable and Treemap are  useful in this type of operation.
But i am not sure how to use them.My keys will be (s,x) & values(y,flag,date)

Also I tried to use below approach.

Hashtable<String,Object[]> tm = new Hashtable<String,Object[]>();
tm.put("S1",new Object[]{20,30,"Yes"});

But I am not able access object?

Can anyone help me to solve this issue?

Thanks










       





0
Comment
Question by:orazen12
[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
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:Dushyant Sharma
Dushyant Sharma earned 100 total points
ID: 34877257
you can get it using the array access

Hashtable<String, Object[]> tm = new Hashtable<String, Object[]>();
        tm.put("S1", new Object[]{20, 30, "Yes"});
        System.out.println(tm.get("S1")[0]);

but it is advised that you create a proper bean and store and get values from them. it is good practice to use beans.
0
 
LVL 4

Accepted Solution

by:
msk_apk earned 400 total points
ID: 34879150
1. Create each row as an Object say for example Row object and assign fields for that object.

public class Row
{
        private String key,
        private String x,
        private int y,
        private Date date,

       //getter and setter for all fields
}

2. Now create index object for all the keys you want to search. This index object will have reference to that Row object.

for example  Index object can be designed as below

public class Index implements Comparable
{
         private String key;
         //here you can have either rowId or rowId array or arraylist based on whether you will have multiple rowId with same keys or not
        //if multiple rows are allowed with same key, then it should be ArrayList, other wise it should be just an int
         private int rowId;

         private Index(String key, int rowId)
         {
                   this.key = key;
                   this.rowId = rowId;
         }

        public int compareTo(Object o)
        {
                  Index toBeCompared = (Index)o;
                  return this.compareTo(toBeCompared.key);
         }
}

Now have two indices one with key as S and other with key as (s +x), because you would like to search based on key S and other key (s +x ).

Have two TreeMaps one for S and another for s + x

pseudo code will be

TreeSet sSet = new TreeSet();
TreeSet sxMap = new TreeSet();
ArrayList rowList = new ArrayList();

Row row = new Row();
//assign all fields

Index sIndex = new Index(s,rowList.length()-1);
Index sxIndex = new Index(s+x, rowList.length()-1)

if(sIndex.contains(s))
{
    //your logic
    //add this rowId to the sIndex
}

if(sxIndex.contains(s+x))
{
   //your logic
   //add this rowId to the sxIndex
}

rowList.add(row);
 

0
 
LVL 4

Expert Comment

by:msk_apk
ID: 34879165
there are two mistakes in the above code

1.   Have two TreeMaps one for S and another for s + x

its not TreeMap its TreeSet actually.

2. missed key in the return statement

public int compareTo(Object o)
        {
                  Index toBeCompared = (Index)o;
                  return this.key.compareTo(toBeCompared.key);
         }
0
 

Author Comment

by:orazen12
ID: 34893853
ddsh79:

Thanks for your reply.I am able to access array elements now.

msk_apk:
Thanks for the reply.I will try this logic.
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

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 46
ejb on wildfly 5 65
Print Rhino Java Array in Javascript 1 59
IBM TS2900 (3572) Tape Autoloader Java? 12 109
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 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…
Suggested Courses

752 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