ArrayList Methods
Introduction
We have learned the basic CRUD (Create, Read, Update, Delete) operations and few operations like concatenation, slicing, and so on on ArrayLists.
In this unit, we will learn some more operations such as finding the index of an element, sorting the ArrayLists, etc.
Finding Index of an Element
The method indexOf() will return the index of the first occurrence of the specified element in the ArrayList.
In case the element is not contained in the ArrayList, it will return -1.
Since ArrayLists present elements in the form of objects, the search parameter(the element to be searched) must be passed as an object.
Syntax
arrList.indexOf(obj);
The indexOf() method compares the value of the given object with the value of every object in the ArrayList.
Example 1:
Code
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>();
arrList.add(3);
arrList.add(6);
arrList.add(2);
arrList.add(1);
System.out.println(arrList);
int index = arrList.indexOf((Integer)6); // finding index of 6.
System.out.println(index);
}
}
Output:
[3, 6, 2, 1]
1
In the above code, we have used wrapper class Integer to typecast 6 into an object.
Example 2:
The indexOf() method checks for value equality, and not reference equality.
Code
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<String> arrList = new ArrayList<>();
String str1 = "A";
String str2 = "B";
String str3 = new String("A");
arrList.add(str1);
arrList.add(str2);
arrList.add(str3);
System.out.println(arrList);
int index = arrList.indexOf(str3); // finding index of "A".
System.out.println(index);
}
}
Output:
[A, B, A]
0
In the above code, although we passed a new reference to string object A, str3, into the indexOf() method, it compared the values instead of references and returned 0.
Example 3:
If the element does not exist in the ArrayList, it will return -1.
Code
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>();
arrList.add(3);
arrList.add(6);
arrList.add(2);
arrList.add(1);
System.out.println(arrList);
int index = arrList.indexOf((Integer)5);
// finding index of 5, which is not present in arrList
System.out.println(index); // Returns -1 , as 5 is not present in arrList.
}
}
Output:
[3, 6, 2, 1]
-1
2. Finding an Element's Frequency
In this case, the Collections.frequency() method determines the number of times an element appears in a specified ArrayList.
The Collections class is located in the java.util package.
Syntax
Collections.frequency(arrList, obj);
Here,
arrList is an ArrayList
obj is the object/element whose frequency is to be found.
Code
import java.util.*;
class Main{
public static void main(String[] args){
ArrayList<Integer> arrList=new ArrayList<>();
arrList.add(3);
arrList.add(6);
arrList.add(2);
arrList.add(1);
arrList.add(2);
System.out.println(arrList);
// Find the frequency of element 2
int frequency=Collections.frequency(arrList,(Integer)2);
System.out.println(frequency);
}
}
Output:
[3, 6, 2, 1, 2]
2
In the preceding code, we typecasted the number 2 into an instance of the wrapper class Integer.
3. Sorting an ArrayList
The Collections.sort() method may be used to sort the given ArrayList in two ways:
In ascending order.
In descending order.
This method alters the input ArrayList.
Ascending Order
We look at sorting an ArrayList in ascending order.
Syntax
Collections.sort(arrList);//sorts in ascending order
Example 1:
Code
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>(4);
arrList.add(3);
arrList.add(6);
arrList.add(2);
arrList.add(1);
System.out.println(arrList);
// Sorting the arraylist in ascending order
Collections.sort(arrList);
System.out.println(arrList);
}
}
Output:
[3, 6, 2, 1]
[1, 2, 3, 6]
Example 2:
Code
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<Character> arrList = new ArrayList<>(4);
arrList.add('E');
arrList.add('K');
arrList.add('B');
arrList.add('O');
System.out.println(arrList) ;
// Sorting the arraylist in ascending order
Collections.sort(arrList) ;
System.out.println(arrList) ;
}
}
Output:
[E, K, B, O]
[B, E, K, O]
Descending Order
To sort the ArrayList in descending order, pass the argument Collection.reverseOrder() to the Collections.sort() method.
Syntax
Collections.sort(arrList, Collections.reverseOrder());
Here, the arrList must be the first argument and the Collection.reverseOrder() must be the second argument.
Example 1:
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>();
arrList.add(3);
arrList.add(6);
arrList.add(2);
arrList.add(1);
System.out.println(arrList);
// Sorting the ArrayList in descending order
Collections.sort(arrList, Collections.reverseOrder());
System.out.println(arrList);
}
}
Output
[3, 6, 2, 1]
[6, 3, 2, 1]
Example 2:
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrList = new ArrayList<>();
arrList.add("Bryant");
arrList.add("Wade");
arrList.add("Garnett");
arrList.add("Rose");
System.out.println(arrList);
// Sorting the ArrayList in descending order
Collections.sort(arrList, Collections.reverseOrder());
System.out.println(arrList);
}
}
Output :
[Bryant, Wade, Garnett, Rose]
[Wade, Rose, Garnett, Bryant]
Summary
The indexOf() method returns the index of the first occurrence of the specified element in the ArrayList. It will return -1 if the ArrayList does not contain the specified element. The Collections.frequency() method will return the frequency of occurrence of the element in the specified ArrayList.
Sorting:
Collections.sort(): This sorts the ArrayList in ascending order.
Collections.sort(arrList, Collections.reverseOrder()): This sorts it in descending order.