Solved

Java HashTable and TreeMap

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

632 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