1. Introduction
This article is part of Complete Guide to ArrayList series. In this article, we will look at the remaining methods of List interface. Previously we discussed:
- How to create ArrayList class’s objects?
- How to add/set/get elements from ArrayList?
- How to remove elements from ArrayList?
- How to check if an element exists in ArrayList?
2. Content
Below are the methods that we will discuss.
int size() boolean isEmpty() void clear() boolean equals(Object o) int hashCode() void trimToSize()
3. int size()
size() method returns total number of elements in ArrayList.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); int size = names.size(); Assert.assertEquals(3, size);
4. boolean isEmpty()
isEmpty() method returns true if the ArrayList has no elements. No elements in ArrayList also mean that size of ArrayList is equal to 0.
List<String> names = new ArrayList<>(); Assert.assertTrue(names.isEmpty()); names.add("John"); names.add("Jane"); names.add("Jack"); Assert.assertFalse(names.isEmpty());
5. void clear()
clear() method is used to nullify all the elements in the ArrayList. If the ArrayList’s size > 0, after this call the size of ArrayList will be equal to 0.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("John"); names.clear(); Assert.assertTrue(names.isEmpty());
6. boolean equals(Object o)
equals(Object o) method is used to check for equality between two ArrayLists. It will return true if and only if the size of both Lists are the same, and all pairs of both Lists are the same.
Example 1:
List<String> names1 = new ArrayList<>(); names1.add("John"); names1.add("Jane"); names1.add("Jack"); List<String> names2 = new ArrayList<>(); names2.add("John"); names2.add("Jane"); names2.add("Jack"); Assert.assertTrue(names1.equals(names2)); Assert.assertTrue(names2.equals(names1));
Example 2 :
List<String> names1 = new ArrayList<>(); names1.add("John"); names1.add("Jane"); names1.add("Jack"); List<String> names2 = new ArrayList<>(); names2.add("John"); names2.add("Jack"); names2.add("Jane"); Assert.assertFalse(names1.equals(names2));
Example 3 :
List<String> names1 = new ArrayList<>(); names1.add("John"); names1.add("Jane"); names1.add("Jack"); List<String> names2 = new ArrayList<>(); names2.add("Sansa"); names2.add("Arya"); Assert.assertFalse(names1.equals(names2));
7. int hashCode()
hashCode() method returns hash code for this ArrayList.
ArrayList calculates hash code using below code :
int hashCode = 1; for (E e : list) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
Get hash code of the List.
List<String> names = new ArrayList<>(); names.add("John"); names.add("Jack"); System.out.println(names.hashCode()); Output : 74052597
8. void trimToSize()
ArrayList usually grows the underlying array by 1.5 times once it reaches the capacity. We should keep in mind that the underlying array will always be larger than the total number of elements in it. This method can minimize the storage of ArrayList instances.
This method is not part of List interface, so you need to declare an instance of ArrayList.
Example :
ArrayList<String> names = new ArrayList<>(); names.add("John"); names.add("Jane"); names.add("Jack"); names.trimToSize();
Below is how ArrayList would look like before making the call to trimToSize().
It trims ArrayList to the number of elements.
9. Conclusion
In this article, we discussed 6 different methods.
- int size(): returns number of elements in ArrayList.
- boolean isEmpty(): returns true if size of ArrayList > 0 else returns false.
- void clear(): null outs all elements of ArrayList.
- boolean equals(Object o): Check if two ArrayList are equal.
- int hashCode(): Returns the hash code of the ArrayList.
- void trimToSize(): Trims the size of ArrayList to the number of elements.