Solved

Java HashTable and TreeMap

Posted on 2011-02-11
4
1,176 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:ddsh79
ddsh79 earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

762 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

6 Experts available now in Live!

Get 1:1 Help Now