[Java] List method that determines whether a specific object is included

list.contains() Determine if there is a specific element and return it as a boolean

	public static void main(String[] args) {
				
		String arr[] = {"orange","apple","cherry","melon","grape"};
		
		List<String> list = new ArrayList<>(Arrays.asList(arr));
		
	if(list.contains("banana")) {
			System.out.println("Exists");
		}else {
			System.out.println("not exist");
		}
	}

list.indexOf() Returns the INDEX number of a particular element as an int, -1 if it does not exist

	public static void main(String[] args) {
				
		String arr[] = {"orange","apple","cherry","melon","grape"};
		
		List<String> list = new ArrayList<>(Arrays.asList(arr));
		
		int x = list.indexOf("orange");
		System.out.println(x); 
		
		if(list.indexOf("orange") != -1) {
			System.out.println("Exists");
		}else {
			System.out.println("not exist");
		}

	}

Recommended Posts

[Java] List method that determines whether a specific object is included
A program that determines whether the entered integer is close to an integer
Docker × Java Building a development environment that is too simple
Finally, create a method for whether there is any character
Create a java method [Memo] [java11]
[Java] Wrapper that executes private method of object from outside class
[Java] You might be happy if the return value of a method that returns null is Optional <>
[Java] JUnit that NG if a method with a large number of lines is detected using black magic
Java method list (memorial) (under construction)
How slow is a Java Scanner?
Sort a List of Java objects
Java Calendar is not a singleton.
What is a lambda expression (Java)
How to test a private method in Java and partially mock that method
Declare a method that has a Java return value with the return value data type
[Java small story] Monitor when a value is added to the List