Current Events > java help? real issue now known, fix elusive.

Topic List
Page List: 1
Medussa
06/29/17 1:30:46 AM
#1:


how do i convince it that ArrayList [a,a,b,b] is not a subset of ArrayList [a,a,a,b]?

I'm currently using containsAll() with an override on equals() to compare all the fields of my class.
but I finally figured out the problem is how containsAll() compares two ArrayLists. it checks each member of the first ArrayList separately against all the members of the second ArrayList. so, in my case, it checks if the second list has a. then it checks if the second list has a. then b, then b. well, shit. all of those things are true. but the number of times a and b are in the list are important, and needs to be preserved.

I'm going to have to write my own method for this, huh?
---
Boom! That's right, this is all happening! You cannot change the channel now!
Act now! Venchmen are standing by for your orders!
... Copied to Clipboard!
Milkman5
06/29/17 1:37:50 AM
#2:


maybe

boolean isSubset = bigList.containsAll(smallList);

Found that on stackoverflow
... Copied to Clipboard!
Milkman5
06/29/17 1:38:40 AM
#3:


You're saying you want to find out if something is a subset that counts duplicates as separate elements essentially, correct?
... Copied to Clipboard!
Annihilated
06/29/17 1:41:16 AM
#4:


I don't see how doing an "equals" on the arrays won't accomplish the same thing, unless the array is full of complex objects. I'm guessing the array type is what you've overridden equals for (don't forget to also override hashCode, that's a rule). You might also consider the Arrays.deepEquals() method. Or I could have just misunderstood whatever it is you're trying to do.

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
... Copied to Clipboard!
Medussa
06/29/17 1:43:20 AM
#5:


Milkman5 posted...
You're saying you want to find out if something is a subset that counts duplicates as separate elements essentially, correct?


yes. if the large set has [A A A B], [A A B B] is not a subset. but the way containsAll() compares the two lists makes it evaluate as one.

why isn't isSubset in the documentation for ArrayList? here's hoping that's the winner.

edit: I guess it's bedtime. completely misread that line of code. no, containsAll() isn't the answer. but see below, I need the solution to work with objects.
---
Boom! That's right, this is all happening! You cannot change the channel now!
Act now! Venchmen are standing by for your orders!
... Copied to Clipboard!
Medussa
06/29/17 1:45:07 AM
#6:


Annihilated posted...
I don't see how doing an "equals" on the arrays won't accomplish the same thing, unless the array is full of complex objects. I'm guessing the array type is what you've overridden equals for (don't forget to also override hashCode, that's a rule). You might also consider the Arrays.deepEquals() method. Or I could have just misunderstood whatever it is you're trying to do.

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html


oh, shit, I guess I didn't include that info this time. yes, A and B are objects, not primitives. also, I'm new to hashCode, but i think I understood the how behind it, if not the why.

edit: also, ArrayList, not Array.
---
Boom! That's right, this is all happening! You cannot change the channel now!
Act now! Venchmen are standing by for your orders!
... Copied to Clipboard!
Milkman5
06/29/17 1:46:13 AM
#7:


Annihilated posted...
I don't see how doing an "equals" on the arrays won't accomplish the same thing, unless the array is full of complex objects. I'm guessing the array type is what you've overridden equals for (don't forget to also override hashCode, that's a rule). You might also consider the Arrays.deepEquals() method. Or I could have just misunderstood whatever it is you're trying to do.

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html


equals =/= is subset

sets can contain lots of subsets and only one subset is actually equal
... Copied to Clipboard!
Annihilated
06/29/17 2:07:05 AM
#8:


Medussa posted...
Annihilated posted...
I don't see how doing an "equals" on the arrays won't accomplish the same thing, unless the array is full of complex objects. I'm guessing the array type is what you've overridden equals for (don't forget to also override hashCode, that's a rule). You might also consider the Arrays.deepEquals() method. Or I could have just misunderstood whatever it is you're trying to do.

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html


oh, shit, I guess I didn't include that info this time. yes, A and B are objects, not primitives. also, I'm new to hashCode, but i think I understood the how behind it, if not the why.

edit: also, ArrayList, not Array.


Yeah sorry, I actually meant to say ArrayList instead of Array, but you can always append a toArray() if you need to. HashCode basically optimizes the object's retrieval from collections such as maps and lists. It does not need to be unique, but it does need to be sufficiently different from other non equal object instances such that they are placed in different "buckets" in the collection, which is basically an internal array. Improper overriding of equals/hashCode in some cases can result in the object not being found in the collection at all.

Regarding your actual problem, it looks like the boolean might be the obvious answer. The reason isSubset isn't part of the documentation is because it's an implicit return value of the containsAll method. If it contains all of the items in the list, then surely it is a subset. Anyway, I also recommend against coding after 2 am, as my ability to think is also affected.
... Copied to Clipboard!
Medussa
06/29/17 2:12:41 AM
#9:


Annihilated posted...
Anyway, I also recommend against coding after 2 am, as my ability to think is also affected.


ain't that the truth =p

and I already knew containsAll() doesn't work and why. unfortunately =/ this topic is a longwinded way of asking for a workaround without having to code it all manually.

thank you both for the effort, though =)
---
Boom! That's right, this is all happening! You cannot change the channel now!
Act now! Venchmen are standing by for your orders!
... Copied to Clipboard!
scar the 1
06/29/17 2:52:38 AM
#10:


A set, by definition, does not contain duplicates. A subset is a set.

If this were Python you could just compare the counts of each element in the two lists. That does sound like a slow operation, though, for large amounts of data. Probably something like O(n²).
---
Everything has an end, except for the sausage. It has two.
... Copied to Clipboard!
Medussa
06/29/17 1:51:37 PM
#11:


scar the 1 posted...
A set, by definition, does not contain duplicates. A subset is a set.


ah. that's the rub.

ok, I can't code it until after work, but what do you think of this:

create a stackedUnit class, that adds a counter to each unit. so a team of [AAAB] becomes a stacked team of [AB], with A having a counter of 3, and B having a counter of 1. a subset would contain only members of the team, and each must have a counter less than or equal to the respective counter in team.

I think I got this =) but if anyone sees a flaw, I'd like to think on another solution at work.
---
Boom! That's right, this is all happening! You cannot change the channel now!
Act now! Venchmen are standing by for your orders!
... Copied to Clipboard!
scar the 1
06/29/17 3:39:37 PM
#12:


Well you could use a dictionary to map each char to an int. Increment every time you find the same one. One dictionary per list, and you could compare them much easier. Or if it's actually chars in your lists, make strings out of them and use StringUtils.countMatches.
---
Everything has an end, except for the sausage. It has two.
... Copied to Clipboard!
Medussa
06/29/17 4:10:50 PM
#13:


they're not characters, i'm using letters here to represent objects.
---
Boom! That's right, this is all happening! You cannot change the channel now!
Act now! Venchmen are standing by for your orders!
... Copied to Clipboard!
scar the 1
06/30/17 2:46:08 AM
#14:


Medussa posted...
they're not characters, i'm using letters here to represent objects.

As long as the objects are hashable you can use dictionaries to keep count.
---
Everything has an end, except for the sausage. It has two.
... Copied to Clipboard!
Topic List
Page List: 1