Solved

Java HashTable and TreeMap

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 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

22 Experts available now in Live!

Get 1:1 Help Now