[Java] Delete the elements of List

Delete one element

◆ When specifying the value of an element

	public static void main(String[] args) {
				
		String arr[] = {"orange","apple","cherry","melon","grape"};
		
		List<String> list = new ArrayList<>(Arrays.asList(arr));
		list.remove("apple");
	}

◆ When specifying the INDEX number of the element

	public static void main(String[] args) {
				
		String arr[] = {"orange","apple","cherry","melon","grape"};
		
		List<String> list = new ArrayList<>(Arrays.asList(arr));
		list.remove(1);
	}

Delete multiple elements

Generate a list with the element you want to remove and put it in the argument of the list.removeAll () method

	public static void main(String[] args) {
				
		String arr[] = {"orange","apple","cherry","melon","grape"};
		
		List<String> list = new ArrayList<>(Arrays.asList(arr));
		
		List<String> remove = new ArrayList<>();
		Collections.addAll(remove,"apple","melon");
		
		list.removeAll(remove);
		
		System.out.println(list);
	}

Delete all elements

List.size will be 0 because all are deleted

	public static void main(String[] args) {
				
		String arr[] = {"orange","apple","cherry","melon","grape"};
		
		List<String> list = new ArrayList<>(Arrays.asList(arr));

		list.clear();
		
		System.out.println(list.size());
	}

Recommended Posts

[Java] Delete the elements of List
Examine the memory usage of Java elements
Compare the elements of an array (Java)
How to delete / update the list field of OneToMany
Summarize the additional elements of the Optional class in Java 9
[Note] Java Output of the sum of odd and even elements
[Java version] The story of serialization
Sort a List of Java objects
Delete all the contents of the list page [Ruby on Rails]
[Java] Try editing the elements of the Json string using the library
Official logo list of the service
List of members added in Java 9
List of types added in Java 9
[Java] Delete the specified number of characters from the end of StringBuilder
The origin of Java lambda expressions
Find out the list of fonts available in AWS Lambda + Java
Examine the list of timezone IDs available in the Java ZoneId class
Get the result of POST in Java
[Java] Get List / Map elements with Iterator
Check the contents of the Java certificate store
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Easily measure the size of Java Objects
List of download destinations for oracle java
Looking back on the basics of Java
How to sort the List of SelectItem
[Java 7] Divide the Java list and execute the process
Output of the book "Introduction to Java"
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
[Java] Contents of Collection interface and List interface
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Java memorandum (list)
Clone Java List.
[Java] Overview of Java
About List [Java]
[No.004] Corrected the order list screen of the orderer
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
Add empty data to the top of the list
About the description order of Java system properties
About the idea of anonymous classes in Java
[Delete the first letter of the character string] Ruby
The order of Java method modifiers is fixed
[Java] Access the signed URL of s3 (signed version 2)
Ominous odor list suggesting the possibility of refactoring
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
I compared the characteristics of Java and .NET
[Java] Be careful of the key type of Map
Feel the passage of time even in Java
Calculate the similarity score of strings with JAVA