ljacqu / JavaCollectionBehavior

Test cases to document different Java collections and their differences
0 stars 0 forks source link

Document other Collection gotchas #4

Open ljacqu opened 1 year ago

ljacqu commented 1 year ago

The tests focus on differences among implementations of the same interface, but it would be interesting to highlight some of the less obvious behavior that all implementations [should] share. Such as:

ljacqu commented 4 months ago

reversed doesn't behave the same for ArrayList vs LinkedHashSet

    public static void main(String[] args) {
        {
            LinkedHashSet<String> seqColl = new LinkedHashSet<>(Arrays.asList("b"));
            SequencedCollection<String> reversedCollection = seqColl.reversed();
            reversedCollection.add("f");
            System.out.println(reversedCollection); // Output: [f, b]
        }

        {
            ArrayList<String> seqColl = new ArrayList<>(Arrays.asList("b"));
            SequencedCollection<String> reversedCollection = seqColl.reversed();
            reversedCollection.add("f");
            System.out.println(reversedCollection); // Output: [b, f]
        }
    }