GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — java-basics/core-java/strings/ListUsingRemoveAll.java at master · learning-zone/java-basics Expand file tree/ Copy pathListUsingRemoveAll.javaMore file actions 55 lines (41 loc) · 1.39 KB / Copy pathListUsingRemoveAll.javaFile metadata and controls55 lines (41 loc) · 1.39 KB 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 52 53 54 55 package strings; import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.function.Predicate; public class ListUsingRemoveAll { // Generic function to remove elements using Predicate public static <T> List<T> removeElements(List<T> l, Predicate<T> p) { // Create collection using Predicate Collection<T> collection = new ArrayList<>(); for (T t : l) { if (p.test(t)) { collection.add(t); } } // Print the list System.out.println("Collection to be removed: " + collection); // Removing 10 using List.removeAll() // passing a collection l.removeAll(collection); // Return the list return l; } public static void main(String[] args) { // Create a list with null values List<String> l = new ArrayList<>( Arrays.asList("1", "10", "15", "10", "12", "5", "10", "20")); // Print the list System.out.println("Original List: " + l); // Creating a Predicate condition checking for 10 Predicate<String> is10 = i -> (i == "10"); // Removing using Predicate l = removeElements(l, is10); // Print the list System.out.println("Updated List: " + l); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |