Java List remove item by omitting removed element from copying

Description

Java List remove item by omitting removed element from copying

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> arrayList1 = new ArrayList<String>();
    arrayList1.add("CSS");
    arrayList1.add("CSS");
    arrayList1.add("HTML");
    arrayList1.add("HTML");
    arrayList1.add("HTML");
    arrayList1.add("Java");
    /* ww w  . ja va  2 s. co  m*/
    System.out.println(arrayList1);
    arrayList1 = deleteAll(arrayList1, "HTML");
    
    System.out.println(arrayList1);
  }

  private static <T> List<T> deleteAll(List<T> list, T valueToDelete) {
    List<T> result = new ArrayList<T>(list.size());
    for (T value : list) {
      if (!value.equals(valueToDelete)) {
        result.add(value);
      }
    }
    return result;
  }
}



PreviousNext

Related