Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// {
import java.util.Arrays;
import java.util.List;
class User {
public int id;
public String name;
User(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "{ id: " + id + ", name: " + name + " }";
}
}
public class Main {
public static List<User> getUsers() {
return Arrays.asList(
new User(1, "Ali"),
new User(2, "Bobby"),
new User(3, "Charlie"),
new User(4, "Dora")
);
}
public static void main(String[] args) throws Exception {
// }
// 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);
Press desired key combination and then press ENTER.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content