赞
踩
set不包含重复的元素,这也是使用set的一个主要原因,set有三个常用的实现,分别是HashSet、TreeSet和LinkedList。总之,对于这三个实现,何时使用是一个重要的问题,如果你需要一个快速的set,你应该使用hashSet;如果需要使用一个排序好的set,那么应该是TreeSet;如果需要一个保持插入顺序的set,那么LinkedHashSet是一个不错的选择。
- TreeSet<Integer> tree = new TreeSet<Integer>();
- tree.add(12);
- tree.add(63);
- tree.add(34);
- tree.add(45);
-
- Iterator<Integer> iterator = tree.iterator();
- System.out.print("Tree set data: ");
- while (iterator.hasNext()) {
- System.out.print(iterator.next() + " ");
- }
Tree set data: 12 34 45 63
- class Dog {
- int size;
-
- public Dog(int s) {
- size = s;
- }
-
- public String toString() {
- return size + "";
- }
- }
- import java.util.Iterator;
- import java.util.TreeSet;
-
- public class TestTreeSet {
- public static void main(String[] args) {
- TreeSet<Dog> dset = new TreeSet<Dog>();
- dset.add(new Dog(2));
- dset.add(new Dog(1));
- dset.add(new Dog(3));
-
- Iterator<Dog> iterator = dset.iterator();
-
- while (iterator.hasNext()) {
- System.out.print(iterator.next() + " ");
- }
- }
- }
- Exception in thread "main" java.lang.ClassCastException: collection.Dog cannot be cast to java.lang.Comparable
- at java.util.TreeMap.put(Unknown Source)
- at java.util.TreeSet.add(Unknown Source)
- at collection.TestTreeSet.main(TestTreeSet.java:22)
- class Dog implements Comparable<Dog>{
- int size;
-
- public Dog(int s) {
- size = s;
- }
-
- public String toString() {
- return size + "";
- }
-
- @Override
- public int compareTo(Dog o) {
- return size - o.size;
- }
- }
输出结果:
1 2 3
- HashSet<Dog> dset = new HashSet<Dog>();
- dset.add(new Dog(2));
- dset.add(new Dog(1));
- dset.add(new Dog(3));
- dset.add(new Dog(5));
- dset.add(new Dog(4));
- Iterator<Dog> iterator = dset.iterator();
- while (iterator.hasNext()) {
- System.out.print(iterator.next() + " ");
- }
5 3 2 1 4
- LinkedHashSet<Dog> dset = new LinkedHashSet<Dog>();
- dset.add(new Dog(2));
- dset.add(new Dog(1));
- dset.add(new Dog(3));
- dset.add(new Dog(5));
- dset.add(new Dog(4));
- Iterator<Dog> iterator = dset.iterator();
- while (iterator.hasNext()) {
- System.out.print(iterator.next() + " ");
- }
- public static void main(String[] args) {
-
- Random r = new Random();
-
- HashSet<Dog> hashSet = new HashSet<Dog>();
- TreeSet<Dog> treeSet = new TreeSet<Dog>();
- LinkedHashSet<Dog> linkedSet = new LinkedHashSet<Dog>();
-
- // start time
- long startTime = System.nanoTime();
-
- for (int i = 0; i < 1000; i++) {
- int x = r.nextInt(1000 - 10) + 10;
- hashSet.add(new Dog(x));
- }
- // end time
- long endTime = System.nanoTime();
- long duration = endTime - startTime;
- System.out.println("HashSet: " + duration);
-
- // start time
- startTime = System.nanoTime();
- for (int i = 0; i < 1000; i++) {
- int x = r.nextInt(1000 - 10) + 10;
- treeSet.add(new Dog(x));
- }
- // end time
- endTime = System.nanoTime();
- duration = endTime - startTime;
- System.out.println("TreeSet: " + duration);
-
- // start time
- startTime = System.nanoTime();
- for (int i = 0; i < 1000; i++) {
- int x = r.nextInt(1000 - 10) + 10;
- linkedSet.add(new Dog(x));
- }
- // end time
- endTime = System.nanoTime();
- duration = endTime - startTime;
- System.out.println("LinkedHashSet: " + duration);
-
- }
- HashSet: 2244768
- TreeSet: 3549314
- LinkedHashSet: 2263320
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。