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).
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.
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.
Joined: Apr 08, 2004
Hi Rob, sbSet = getSubSet(allTypes, qualification); here getSubSet() is taking two arguments allTypes and qualification. Is it separate function ?
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).