I need to write a JUnittest for a function that produces
When I started to implement this class:
I said to myself: "This is nuts! Surely someone has already done this for Array, LinkedList, ArrayList and friends!
But after google searching, I could not find any such examples...
Can someone point me to a maven library that will help me compare containers containing containers and maybe point me to an example?
I've been using the Hamcrest to compare Sets of Strings. Perhaps all I need is an example of using Hamcrest to compare Sets of Lists of Strings? I'm looking at the hamcrest javadocs and I'm not seeing anything obviouse.
Please help me understand why this test fails. The first assertThat passes but the assertThat in the for each loop fails.
This is probably because assertThat is doing shallow compares (comparing references) instead of deep compares (comparing the lists element by element).
Perhaps this is because List and ArrayList do not implement Comparable? If so, is there some library available in the maven repository that does have containers (such as lists) that implement Comparable?
If not, why?
How would a Collection implement Comparable? Comparable mandates a total ordering, so can you say that ListA is greater than ListB (or less than or the same as)? If you cannot give a simple definition of that relationship, then you cannot expect the classes involved to implement Comparable.
I tried those hamcrest matchers and they seem to work for contianers (like sets) of strings.
However, I'm confused about how hamcrest compares containers of containers like Set<Collection<? extends String>>;
In other languages like C++, I can specify the lambda function or to be used for comparing elements of a Set container (for example). I assume in java, that function is Comparable::CompareTo. However, when I look at the documentation, I don't see that classes like List, or Set or ArrayList implement such a function. So how does class Set determine if you are adding a new element that is a duplicate? How does the hamcrest matcher know if a set contains a certain element (in any order)?
I tried creating descendants of ArrayList and List, and String that implement Comparable and failed (perhaps this should be a topic of a different post).
Like I suggested previously, I am surprised there is not some library in the maven repository that implements this already.
Can anyone improve on this awful code I wrote? It works, but it (my function compareIteratorCollections) does not use generics and is not as type-safe as it could be. Can someone show me how to write compareIteratorCollections using generics?