Java中Collection的操作,可以透過Collections.singleton()來移除相似內容的元素:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestMain {
  public static void main(String args[]) {
    String init[] = { "One", "Two", "Three", "One", "Two", "Three" };
    
    List list1 = new ArrayList(Arrays.asList(init));
    List list2 = new ArrayList(Arrays.asList(init));
    
    // 移除list1中的第一個出現的"One"
    list1.remove("One");
    System.out.println("List1 value: "+list1);
    
    // 移除"One"
    list2.removeAll(Collections.singleton("One"));           
    System.out.println("The SingletonList is :"+list2);
 }
}

執行結果:

List1 value: [Two, Three, One, Two, Three]

The SingletonList is :[One, Two, Three, One, Two, Three]

這個網誌中的熱門文章

Bash判斷參數是否存在

Node.js package : forever