Arrays, lists and maps
Arrays, lists and maps are three containers with three different ideas of equality. An array does not override equals at all, so it needs its own assertion; a list is ordered and compares element by element; a map compares keys and values without an order. This lesson gives each one a test and a report.
The class and three tests
Scores.java returns one value of each kind: an array of numbers, a list of names and a map of counts.
import java.util.List;
import java.util.Map;
public class Scores {
static int[] values() {
return new int[] {3, 5, 8};
}
static List<String> names() {
return List.of("Ada", "Grace");
}
static Map<String, Integer> counts() {
return Map.of("a", 1, "b", 2);
}
}
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class ScoresTest {
@Test
void arraysAreComparedElementByElement() {
assertArrayEquals(new int[] {3, 5, 8}, Scores.values());
}
@Test
void listsAreComparedByContent() {
assertEquals(java.util.List.of("Ada", "Grace"), Scores.names());
}
@Test
void mapsAreComparedByEntries() {
assertEquals(java.util.Map.of("a", 1, "b", 2), Scores.counts());
}
}
Arrays need assertArrayEquals
An array is an object, and it inherits equals from Object, which compares identity. Two arrays with the same elements are still two objects, so assertEquals on them fails. The assertion for arrays is assertArrayEquals: it compares the lengths first and then every element with the element equality of its type.
It has an overload for each primitive array and one for object arrays. For an object array it calls equals on each element, so an array of records works as well as an array of strings. A nested array needs assertArrayEquals on each row, or a loop; there is no deep version in Jupiter.
Lists compare by content
A List overrides equals: two lists are equal when they have the same size and every element at the same index is equal. The order is part of the contract, so List.of("Ada", "Grace") and List.of("Grace", "Ada") are not equal, which is what a test of an ordered result should say.
A Set also overrides equals, and it ignores order: two sets with the same elements are equal whatever order the iterator uses. That is the assertion for a result that has no meaningful order.
Maps compare by entries
A Map compares every key and its value, and the iteration order does not matter. Map.of("a", 1, "b", 2) equals the same two entries however the implementation stores them. A key that is present with a different value fails, and the failure message prints the whole map, which shows the difference at a glance.
The failure of an array
An array failure does not print the two arrays on the tree line; it names the index where they differ. For two int arrays the message is array contents differ at index [1], expected: <5> but was: <4>. The index is the useful part: it sends the reader to the element, not to the whole array. The failure list below the tree repeats the arrays in full.
The report
javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Scores.java ScoresTest.java
junit execute --class-path out --scan-classpath --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│ └─ ScoresTest ✔
│ ├─ arraysAreComparedElementByElement() ✔
│ ├─ listsAreComparedByContent() ✔
│ └─ mapsAreComparedByEntries() ✔
└─ JUnit Vintage ✔
Test run finished after 494 ms
[ 4 containers found ]
[ 0 containers skipped ]
[ 4 containers started ]
[ 0 containers aborted ]
[ 4 containers successful ]
[ 0 containers failed ]
[ 3 tests found ]
[ 0 tests skipped ]
[ 3 tests started ]
[ 0 tests aborted ]
[ 3 tests successful ]
[ 0 tests failed ]
Exercises
- Replace assertArrayEquals with assertEquals and run. What does the failure message print instead of the elements?
- Change one element of the expected array and read the index in the failure line.
- Reverse the two names in the expected list. Does the test pass? Why?
- Change one value in the expected map and run. Which entries does the report show?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which assertion compares two int arrays element by element?
Answer
assertArrayEquals
An array inherits identity equality, so only assertArrayEquals compares the elements.
-
What is part of the equality of two lists?
Answer
The elements and their order
List.equals compares size and every element at the same index, so order matters.
-
What is part of the equality of two maps?
Answer
Every key with its value, in any order
Map.equals compares the entries, and the iteration order is not part of the contract.
-
What does this program print?
public class Q1 { public static void main(String[] args) { int[] values = {3, 5, 8}; System.out.println(values.length); } }Answer
3
The array has three elements, and length returns 3.
-
What does this program print?
public class Q2 { public static void main(String[] args) { int[] values = {3, 5, 8}; System.out.println(values[1]); } }Answer
5
Index 1 is the second element, which is 5.
-
What does this program print?
public class Q3 { public static void main(String[] args) { System.out.println(java.util.List.of("a", "b").size()); } }Answer
2
The list has two elements, so size returns 2.
-
What does this program print?
public class Q4 { public static void main(String[] args) { System.out.println(java.util.Map.of("a", 1).get("a")); } }Answer
1
The key a maps to 1, so get returns 1.
-
Two arrays have the same three elements. What does
array1.equals(array2)return?Answer
false, because arrays compare identity
Arrays do not override equals, so two different array objects are never equal.
-
What does this program print?
public class Q5 { public static void main(String[] args) { String[] names = {"Ada", "Grace"}; System.out.println(names[0].length()); } }Answer
3
The first element is Ada, and the string has three characters.
-
Why does an array failure name an index?
Answer
The assertion reports where the elements first differ
assertArrayEquals compares element by element and reports the first index where they differ.
The code on this page was built with OpenJDK 21.0.13 (javac) and run with JUnit Platform Console Launcher 1.14.3 on Alpine Linux in a Docker container.
Article author: Arthur Isaev