Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array of sub Sets from parent Set ?

 
kri shan
Ranch Hand
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you inserting data into the set? Is it a tuple.. I mean as some class objects?
 
kri shan
Ranch Hand
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, POJO class like User which inserts data into the set
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kri shan
Ranch Hand
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, sbSet = getSubSet(allTypes, qualification); here getSubSet() is taking two arguments allTypes and qualification. Is it separate function ?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic