java容器之Set常用方法

Set不包含相同的元素,有HashSet(散列,查找速度最快),TreeSet(按升序排列),LinkedHashSet(按输入顺序)

下面是常用的方法:

import java.util.*;
public class SetOperations {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Set set1=new HashSet();
		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 set2=new HashSet(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);

	}

}


你可能感兴趣的:(java)