| Author |
array of sub Sets from parent Set ?
|
kri shan
Ranch Hand
Joined: Apr 08, 2004
Posts: 1300
|
|
name, age and qualification are the columns in the database. I accessed whole data from the database and put in the Set(java.util.Set). Now i want to form array of Sets based on qualification value(qualification is the key for forming array of Sets).
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
kri shan wrote:name, age and qualification are the columns in the database. I accessed whole data from the database and put in the Set( java.util.Set).
But how can you insert three values into a set?? Do you have a POJO class like User which you insert into the set?? I also didn't get the actual question that you asked...
|
 |
amitabh mehra
Ranch Hand
Joined: Dec 05, 2006
Posts: 98
|
|
|
How are you inserting data into the set? Is it a tuple.. I mean as some class objects?
|
 |
kri shan
Ranch Hand
Joined: Apr 08, 2004
Posts: 1300
|
|
|
Yes, POJO class like User which inserts data into the set
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
There are two different types of sub sets.
The first is the snapshot (pseudo code):
The second is much harder, and is a view on the original set. If the original set changes, the view changes as well. This is what SortedSet.subSet, SortedSet.headSet and SortedSet.tailSet are required to return. You can check TreeSet (and effectively TreeMap) of how this can be implemented.
AbstractList.subList also does something similar.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
kri shan
Ranch Hand
Joined: Apr 08, 2004
Posts: 1300
|
|
Rob,
based on your first soultion:
I want to create array of subSets based on Qualification values. Because i do not know how many different qulification type values are in the database.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
I would go for a Map<Qualification,Set<MyType>>, combined with a method that returns a Set<MyType> given a Set<MyType> and Qualification. (This method can be private, or an instance method on Qualification, or whatever you choose.)
You can store the entire set with a null key, or of course as a separate field. For all qualifications, you can retrieve them from the map as follows:
This is a lazy initialization implementation; if you never need a sub set for a qualification, you simply never initialize it. If you do, you only initialize it once and cache it afterwards.
Note that I made an abstraction of your qualification; perhaps in your code it will be a simple String, or an int / long (in which case you use Integer / Long as key type). The important thing is, that this code should work regardless of your representation choice.
|
 |
kri shan
Ranch Hand
Joined: Apr 08, 2004
Posts: 1300
|
|
|
Hi Rob, sbSet = getSubSet(allTypes, qualification); here getSubSet() is taking two arguments allTypes and qualification. Is it separate function ?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Yes it is. It is the method that actually does the filtering (e.g. like my previous post). But like I said, it could be a method of class Qualification, in which case the call would be qualification.getSubSet(allTypes).
|
 |
 |
|
|
subject: array of sub Sets from parent Set ?
|
|
|