Skip to content

Instantly share code, notes, and snippets.

@SnuktheGreat
Created February 3, 2016 14:48
Show Gist options
  • Save SnuktheGreat/ca111b608fb03987180d to your computer and use it in GitHub Desktop.
Save SnuktheGreat/ca111b608fb03987180d to your computer and use it in GitHub Desktop.
Simple test to proof equals works for similar collection types.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class CollectionsTest {
@Test
public void testListEquals() throws Exception {
List<Integer> arrayList = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toCollection(ArrayList::new));
List<Integer> linkedList = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toCollection(LinkedList::new));
assertThat(arrayList, is(equalTo(arrayList)));
assertThat(linkedList, is(equalTo(linkedList)));
assertThat(arrayList, is(equalTo(linkedList)));
assertThat(linkedList, is(equalTo(arrayList)));
assertThat(arrayList, is(equalTo(ImmutableList.of(1, 2, 3, 4, 5))));
}
@Test
public void testSetEquals() throws Exception {
Set<Integer> hashSet = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toCollection(HashSet::new));
Set<Integer> treeSet = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toCollection(TreeSet::new));
assertThat(hashSet, is(equalTo(hashSet)));
assertThat(treeSet, is(equalTo(treeSet)));
assertThat(hashSet, is(equalTo(treeSet)));
assertThat(treeSet, is(equalTo(hashSet)));
assertThat(treeSet, is(equalTo(ImmutableSet.of(1, 2, 3, 4, 5))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment