Solved

Java HashTable and TreeMap

Posted on 2011-02-11
4
1,178 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap bin 2 34
Windows 10 IE Certificate Issue 10 45
expectj telnet failing 5 35
hibernate example issues from command prompt 10 40
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…
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…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 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