Set 存储的值不重复.
package ds.collections.sets; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.TreeSet; /** * set测试 * @author Hust * @Time 2011-11-9 */ public class SetTest { public static void main(String[] args) { //testHashSet(); //testHashSetObj(); testTreeSet(); } //treeSet public static void testTreeSet(){ Set<String> treeSetTest = Collections.synchronizedSortedSet(new TreeSet<String>()); treeSetTest.add("123"); treeSetTest.add("123"); Iterator<String> it =treeSetTest.iterator(); while(it.hasNext()){ System.out.println(it.next()); }// //123 TreeSet<String> treeSetTest2 = new TreeSet<String>(); treeSetTest2.add("def"); treeSetTest2.add("abc"); Iterator<String> it2 =treeSetTest2.iterator(); while(it2.hasNext()){ System.out.println(it2.next()); } /** result is abc def */ //排序接口 TreeSet<Po2> treeSetComp = new TreeSet<Po2>(new Compara()); treeSetComp.add(new Po2(345,"1234565")); treeSetComp.add(new Po2(1,"a")); treeSetComp.add(new Po2(1,"ab")); treeSetComp.add(new Po2(1,"1")); treeSetComp.add(new Po2(1,"123")); treeSetComp.add(new Po2(1,"41255")); treeSetComp.add(new Po2(1,"132")); treeSetComp.add(new Po2(1,"一")); treeSetComp.add(new Po2(1,"二")); Iterator<Po2> itc =treeSetComp.iterator(); while(itc.hasNext()){ System.out.println(((Po2)itc.next()).toString()); } /** 1,1 1,a 1,ab 1,一 1,二 1,123 1,132 1,41255 345,1234565 */ } //HashSet public static void testHashSet() { //方法不同步: Set set = Collections.synchronizedSet(new HashSet());来实现同步 Set<String> set = new HashSet<String>(); System.out.println(set.isEmpty()); set.add("123"); set.add(new String("123")); //String 的hashCode使用比的是值*31 ... set.add("456"); set.add("789"); set.add("123"); // 重复的abc,set会自动将其去掉 //set还是利用equals()方法进行比较 System.out.println(set.contains("123")); System.out.println(set.remove("456")); System.out.println(set.contains("456")); System.out.println("size=" + set.size()); List<String> list = new ArrayList<String>(); list.add("zxc"); list.add("asd"); list.add("456"); set.addAll(list); // 将list中的值加入set,并去掉重复的abc System.out.println("size=" + set.size()); for (Iterator<String> it = set.iterator(); it.hasNext();) { System.out.println("value=" + it.next().toString()); } } //测试存放对象 public static void testHashSetObj(){ Set<Po> poSet = Collections.synchronizedSet(new HashSet<Po>()); Po p1 = new Po(1,"123"); Po p2 = new Po(1,"123"); poSet.add(p1); poSet.add(p2); System.out.println(poSet.size()); for (Iterator<Po> iterator = poSet.iterator(); iterator.hasNext();) { Po po = iterator.next(); System.out.println(po.toString()); } /** 1,123 1,123 */ poSet.clear(); System.out.println(poSet.size()); Set<Po2> po2Set = Collections.synchronizedSet(new HashSet<Po2>()); Po2 p21 = new Po2(1,"123"); Po2 p22 = new Po2(1,"123"); po2Set.add(p21); po2Set.add(p22); for (Iterator<Po2> iterator = po2Set.iterator(); iterator.hasNext();) { Po2 po2 = iterator.next(); System.out.println(po2.hashCode() + po2.toString()); } /** 不重写hashCode 与 equals时 15580811,123 15580811,123 重写后 15580811,123 */ } } /** * 测试类 * @author Hust * @Time 2011-11-9 */ class Po { private int id; private String name; public Po(int id,String name){ this.id = id; this.name = name; } public String toString(){ return id+","+name; } } /** * 测试类 * @author Hust * @Time 2011-11-9 */ class Po2 { private int id; private String name; public Po2(int id,String name){ this.id = id; this.name = name; } //重写hashCode public int hashCode() { int hash = 0; hash += (this.name!=null)?this.name.hashCode():0; hash += 31*hash + this.id; return hash; } //重写equals public boolean equals(Object o ){ if(o == null) return false; if(o.getClass() != this.getClass()) return false; Po2 po = (Po2)o; if(this.id != po.id || !this.name.equals(po.name)) return false; return true; } public String toString(){ return id+","+name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Compara implements Comparator<Po2>{ @Override public int compare(Po2 o1, Po2 o2) { if(o1.equals(o2)) return 0; else if(o1.getId() >o2.getId()) return 1; else if(o1.getName().hashCode() > o2.getName().hashCode()) return 1; return -1; } }?
?