Home >Java >javaTutorial >Use java's ArrayList.retainAll() function to retain specified elements in ArrayList
Use java's ArrayList.retainAll() function to retain specified elements in ArrayList
ArrayList is one of the commonly used collection classes in Java. It provides a convenient way to store and operate a set of objects. . In actual development, sometimes we need to filter out specified elements from an ArrayList while leaving other elements unchanged. This requirement can be achieved using the retainAll() function of ArrayList. This article will introduce how to use the ArrayList.retainAll() function to retain specified elements in the ArrayList, and give specific code examples.
The function of the ArrayList.retainAll() function is to retain the same elements in the ArrayList as the elements in the specified collection and delete other elements. Its method is declared as: retainAll(Collection6b3d0130bba23ae47fe2b8e8cddf0195 c), where the parameter c is another collection. When the retainAll() function is called, the ArrayList will delete all its elements that are not in c, and only retain the elements that are the same as the elements in c.
The following is a sample code using the ArrayList.retainAll() function:
import java.util.ArrayList; import java.util.Arrays; public class RetainAllExample { public static void main(String[] args) { // 创建一个ArrayList,并添加一些元素 ArrayList<String> list1 = new ArrayList<>(Arrays.asList("apple", "banana", "orange", "mango")); // 创建另一个ArrayList作为参考集合 ArrayList<String> list2 = new ArrayList<>(Arrays.asList("apple", "orange", "grape")); // 使用retainAll()函数保留list1中与list2相同的元素 list1.retainAll(list2); // 输出结果 System.out.println("保留与list2相同的元素后,list1中的元素为:" + list1); } }
Run the above code, the output result is: After retaining the same elements as list2, the elements in list1 are: [ apple, orange]
In the above example, we first created a list1 and a list2, which respectively contain some string elements. Then, we call the retainAll() function of list1 to retain the same elements in list1 as list2, and the remaining elements are deleted. Finally, by printing the contents of list1, we can see that only the same elements "apple" and "orange" as in list2 are left.
It should be noted that the ArrayList.retainAll() function will modify the original ArrayList and delete elements that do not meet the conditions. If you need to keep the original ArrayList unchanged, you can create a copy before calling the retainAll() function.
Using the ArrayList.retainAll() function can easily filter out the specified elements in the ArrayList and retain the implementation of other elements. It is very useful in scenarios that deal with some specific requirements, such as when merging two ArrayLists, only retaining the same elements, or when filtering out elements that match conditions, deleting other irrelevant elements.
To sum up, the ArrayList.retainAll() function is a practical method that can help us filter and retain specified elements in ArrayList simply and efficiently. By using this function properly, we can better process the data in ArrayList and improve the efficiency and readability of the program.
The above is the detailed content of Use java's ArrayList.retainAll() function to retain specified elements in ArrayList. For more information, please follow other related articles on the PHP Chinese website!