Comparing different members in a hashset

Hello all,

I have a question:

I need to compare the following information:

[a,b], [a] and and retain the superset i.e. discard the [a] and.

I have this information in a hashset. Now how do I compare them?

Please try to suggest some solution as soon as possible.

Regards,

Shef

[340 byte] By [shefa] at [2007-11-26 20:15:06]
# 1
You can use the Set.containsAll method to check whether one set contains all elements of the other one.
ProggerFromKupfera at 2007-7-9 23:21:36 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi, Thanks for such a quick reply.But can you help me by showing some code?I understand the method containsAll but how to use it?Regards,Shef
shefa at 2007-7-9 23:21:36 > top of Java-index,Java Essentials,Java Programming...
# 3

> I need to compare the following information:

> [a,b], [a ] and [b ] and retain the superset i.e.

> discard the [a ] and [b ].

Okay, I fixed the formatting above (your [b ] was turning everything into bold). Can you restate your question to explain exactly what you want? Are those bracketed things three Sets, or what?

doremifasollatidoa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks.Actually the problem is like this:I have a hashset hs which has 3 elements, [a,b] [a] Each of these elements are Arraylist. So, I have to compare them and retain the superset. Plese suggest me how can I do this.Regards,Shef
shefa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 5

I am currently trying to do this, but get error at the bold line. How to access the 2nd and 3rd element in a hashset?

Iterator ieRHS=hsRHS.iterator();//hsRHS is our hashset

while(ieRHS.hasNext()){

ArrayList t2 = (ArrayList) ieRHS.next(); //1st element in hashset

ArrayList t3 = (ArrayList) ieRHS.next();//2nd element

ArrayList t4 = (ArrayList) ieRHS.next();//3rd element

if (t2.toString().contains(t3.toString()))

System.out.println("t2 contains t3 and they are:-"+t2 + ","+t3);

}

Regards,

Shef

shefa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 6

[b ] without a space keeps turning your posts into bold. Please make sure to add the space. (Similarly, [i ] without spaces turns posts into italic, and [u ] without spaces turns posts into underlined.) Also, when posting code, use the "code" button above the posting box.

Anyway, I don't understand your code. What error do you get at the bold line?

Try this:

System.out.println(hsRHS.size());

System.out.println(hsRHS);

What does it print?

If you have a Set that has elements, [a, b], [a], and [c], what do you want to retain? Your example/wording isn't clear.

doremifasollatidoa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 7

If you have (always) exactly3 elements in your set, you could extract the elements into a vector and then check for inclusion. Pseudocode:

s: HashSet containing the 3 lists

x: array containing the 3 lists

if x[0].containsAll(x[1]) then s.remove(x[1])

if x[0].containsAll(x[2]) then s.remove(x[2])

if x[1].containsAll(x[0]) then s.remove(x[0])

if x[1].containsAll(x[2]) then s.remove(x[2])

if x[2].containsAll(x[0]) then s.remove(x[0])

if x[2].containsAll(x[1]) then s.remove(x[1])

If you know that there are exactly two one-element-lists and exactly one two-element-list containing the same elements, you can improve this code. And if you want to be flexible in the number of the lists, you can rewrite the code to use two nested for-loops.

ProggerFromKupfera at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 8

hsRHS.size is 3 and it contains 3 arraylists [a,b], [a] and [b ].

Now I have to compare them with each other and would like to keep only [a,b] as it contains the other two elements also.

Actually I am working on rules like:

p=>[a,b]

q=>[a]

r=>[b ]

And would like to display only the first rule. I have these RHS of these rules in a hashset. Now how to achieve this? I hope I am clear this time.

The error I got in the previous code that I showed is: "no such element". This may be because I am using .next multiple times. Is it allowed?

Regards,

Shef.

shefa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 9

No, It is not necessary that I always have 3 elements and we are also not sure how many elements they may contain.

The idea is to retain only the superset which includes all other elements.I hope when you read my post where I mention about the rules that I am working with, you will understand my point.

Regards,

Shef

shefa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 10

> The error I got in the previous code that I showed is: "no such element". This may be because I am using .next multiple times. Is it allowed?

Of course it is allowed, but you have to make sure that there is a next element (if your code fails at the second call to next(), it seems as if your HashSet contains only one element!

ProggerFromKupfera at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 11

You should be able to use ".next()" multiple times, but should call "hasNext()" before each one. If you have three elements, you should be able to call next() three times.

Easiest way to get all elements is probably this:

Iterator ieRHS=hsRHS.iterator(); //hsRHS is our hashset

Set superHashSet = new HashSet();

while(ieRHS.hasNext()){

superHashSet.addAll((List)ieRHS.next());

}

System.out.println(superHashSet);

That adds all elements that appear at least once in any List to superHashSet. If you need that in an ArrayList, you can then do:

List superList = new ArrayList(superHashSet);

System.out.println(superList);

doremifasollatidoa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 12

> > The error I got in the previous code that I showed is: "no such element". This may be because I am using .next multiple times. Is it allowed?

> Of course it is allowed, but you have to make sure that there is a next element (if your code fails at the second call to next(), it seems as if your HashSet contains only one element!

The best way to avoid this exception is to check if there is a next element (with hasNext()) every time before calling next().

ProggerFromKupfera at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 13

Thanks to both of you.

I have to check my code to solve why hsRHS is shown to contain only 1 value when infact when I print it just before the return statement in my program I get 3 values. May be I am messing up with some loops.

I will have a look and try to solve the problem. Thanks once again.

Regards,

Shef

shefa at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 14

List l = new ArrayList(hsRHS);

for(int i = 0; i<l.size(); ++i){

List l1 = (List)l.get(i);

for(int j = 0; j><l.size();){

List l2 = (List)l.get(j);

if(l1 != l2 && l1.containsAll(l2)){

l.remove(j);

if(j><i) --i;

}

else{

++j;

}

}

}

Afterwards, l will contain no lists whose elements are completely contained in other lists.>

ProggerFromKupfera at 2007-7-9 23:21:37 > top of Java-index,Java Essentials,Java Programming...
# 15
Thanks a lot.Regards,Shef
shefa at 2007-7-21 17:53:42 > top of Java-index,Java Essentials,Java Programming...