Since Java 8+, you can filter an ArrayList by using the Stream API

Apart from that, you can query an ArrayList with it's own APIs such as get methods to return element by the specified index; indexOf methods to return index by the specified element; contains methods to check existing; size and isEmpty methods to check the ArrayList size

Lets walk through this tutorial to explore them in more details

Filter with Java 8+ Stream API

  • Using .stream().filter(Predicate) to return elements that match with the given predicate
@Test
public void filter() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    Integer[] arr = lst.stream().filter(e -> e >= 2).toArray(Integer[]::new);
    assertThat(arr).contains(2, 3);
}

Find element by index

  • get(int) method gets the element at the specified index in the list
@Test
public void get_thenOK() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    int ele = lst.get(0);
    assertThat(ele).isEqualTo(3);
}
  • IndexOutOfBoundsException will be thrown if the specified index is out of range
@Test(expected = IndexOutOfBoundsException.class)
public void get_thenException() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    // throws IndexOutOfBoundsException
    int ele = lst.get(4);
}

Find index by element

  • indexOf(Object) method returns the index of the first occurrence of the specified element in the list, otherwise returns -1
@Test
public void indexOf() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    int idx = lst.indexOf(3);
    assertThat(idx).isEqualTo(0);

    idx = lst.indexOf(4);
    assertThat(idx).isEqualTo(-1);
}
  • lastIndexOf(Object) method returns the index of the last occurrence of the specified element in the list, otherwise returns -1
@Test
public void lastIndexOf() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(3);

    int idx = lst.lastIndexOf(3);
    assertThat(idx).isEqualTo(2);

    idx = lst.indexOf(4);
    assertThat(idx).isEqualTo(-1);
}

Check if elements existing

  • contains(Object) method checks if an ArrayList contains the specified element
@Test
public void contains() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    boolean contained = lst.contains(1);
    assertThat(contained).isTrue();
}
  • containsAll(Collection) method checks if an ArrayList contains all of elements in the specified collection
@Test
public void containsAll() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    boolean contained = lst.containsAll(List.of(1, 3));
    assertThat(contained).isTrue();

    contained = lst.containsAll(List.of(1, 4));
    assertThat(contained).isFalse();
}


Check ArrayList size

  • size() gets the number of elements in the list
@Test
public void size() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    int size = lst.size();
    assertThat(size).isEqualTo(3);
}
  • isEmpty() checks if the list contains no elements
@Test
public void isEmpty() {  
    List<Integer> lst = new ArrayList<>();
    lst.add(3);
    lst.add(1);
    lst.add(2);

    boolean empty = lst.isEmpty();
    assertThat(empty).isFalse();
}

Conclusion

In this tutorial, we learned to filter and query an ArrayList in Java. You can find below the full source code


Share to social

Van N.

Van N. is a software engineer, creator of HelloKoding. He loves coding, blogging, and traveling. You may find him on GitHub and LinkedIn