java容器之Set常用方法

u010660276發表於2014-01-03

Set不包含相同的元素,有HashSet(雜湊,查詢速度最快),TreeSet(按升序排列),LinkedHashSet(按輸入順序)

下面是常用的方法:

import java.util.*;
public class SetOperations {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Set<String> set1=new HashSet<String>();
		Collections.addAll(set1,"a b c d e f g h i k l j".split(" "));
		set1.add("m");
		System.out.println(set1.contains("m"));
		Set<String> set2=new HashSet<String>(Arrays.asList("a b c d".split(" ")));
		System.out.println(set1.containsAll(set2));
		set1.remove("h");
		System.out.println(set1);
		set1.removeAll(set2);
		System.out.println(set1);

	}

}


相關文章