Back
Close

How to test if a List contains a specific value in Java

[CG]BOUGA
3,267 views
// {...}
// Method 1: basic structures
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
int integerToFind = 6;
boolean integerExists = integers.contains(integerToFind);
System.out.println("6 is the list: " + integerExists);
// Method 2: complex structures
List<User> users = getUsers();
String userToFind = "Bobby";
boolean userExists = users.stream().anyMatch(user -> userToFind.equals(user.name));
System.out.println("Bobby is in the list: " + userExists);
// Method 3: find the element that matches the condition
User bobby = users.stream()
.filter(user -> userToFind.equals(user.name))
.findFirst()
.orElse(null);
System.out.println("Bobby: " + bobby);
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants