Skip to content

Commit 5d1164c

Browse files
committed
Add example for LinkedList contains() method
1 parent 5f14b90 commit 5d1164c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.gaurav.ExProject.LinkedList;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* A Java program to check if the element is present
7+
* in the linkedList using the contains(Object o) method.
8+
*
9+
* @author coderolls.com
10+
*
11+
*/
12+
public class LinkedListContains {
13+
14+
public static void main(String[] args) {
15+
16+
LinkedList<String> linkedList = new LinkedList<String>();
17+
18+
linkedList.add("Uber");
19+
linkedList.add("Ola");
20+
linkedList.add("Didi");
21+
linkedList.add("Lyft");
22+
linkedList.add("Yandex");
23+
24+
System.out.println("LinkedList is as follows: \n"+ linkedList);
25+
26+
27+
// checks if the linkedList contains "Ola"
28+
// returns true if present
29+
boolean isOlaPresnet = linkedList.contains("Ola"); // true
30+
31+
System.out.println("\nCheck if linkedList contains Ola: "+ isOlaPresnet);
32+
33+
// checks if the linkedList contains "Grab"
34+
// returns true if present
35+
boolean isGrabPresnet = linkedList.contains("Grab"); // false
36+
37+
System.out.println("\nCheck if linkedList contains Grab: "+ isGrabPresnet);
38+
39+
}
40+
}

0 commit comments

Comments
 (0)