java HashSet去重示例
阿新 • • 發佈:2019-01-28
import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class HashSetDemo { public static void main(String[] args){ List tableList = new ArrayList(); tableList.add("hello"); tableList.add("hell0"); tableList.add("world"); tableList.add("world"); tableList.add(2); tableList.add(2); tableList.add(true); tableList.add(true); HashSet hs = new HashSet(tableList); //System.out.println(hs.toString()); //System.out.println(tableList); Iterator i = hs.iterator(); while(i.hasNext()){ Object temp = i.next(); System.out.println(temp.toString()); } } }
輸出:
2
true
hell0
hello
world