当前位置:天才代写 > tutorial > JAVA 教程 > 利用Sets

利用Sets

2017-11-13 08:00 星期一 所属: JAVA 教程 浏览:401

Set拥有与Collection完全沟通的接口,所以和两种差异的List差异,它没有什么特另外成果。相反,Set完全就是一个Collection,只是具有差异的行为(这是实例和多形性最抱负的应用:用于表达差异的行为)。在这里,一个Set只答允每个工具存在一个实例(正如各人今后会看到的那样,一个工具的“值”的组成是相当巨大的)。

Set(接口) 添加到Set的每个元素都必需是唯一无二的;不然Set就不会添加反复的元素。添加到Set里的工具必需界说equals(),从而成立工具的独一性。Set拥有与Collection完全沟通的接口。一个Set不能担保本身可按任何特定的顺序维持本身的元素
HashSet* 用于除很是小的以外的所有Set。工具也必需界说hashCode()
ArraySet 由一个数组后推获得的Set。面向很是小的Set设计,出格是那些需要频繁建设和删除的。对付小Set,与HashSet对比,ArraySet建设和重复所需支付的价钱都要小得多。但跟着Set的增大,它的机能也会大打折扣。不需要HashCode()
TreeSet 由一个“红黑树”后推获得的顺序Set(注释⑦)。这样一来,我们就可以从一个Set里提到一个顺序荟萃

⑦:直至本书写作的时候,TreeSet仍然只是公布,尚未正式实现。所以这里没有提供利用TreeSet的例子。

Set (interface)
 

Each element that you add to the Set must be unique; otherwise the Set doesn’t add the duplicate element. Objects added to a Set must define equals() to establish object uniqueness. Set has exactly the same interface as Collection. The Set interface does not guarantee it will maintain its elements in any particular order.
 

HashSet*
 

For Sets where fast lookup time is important. Objects must also define hashCode().
 

TreeSet
 

An ordered Set backed by a red-black tree. This way, you can extract an ordered sequence from a Set.
 

下面这个例子并没有列出用一个Set可以或许做的全部工作,因为接口与Collection是沟通的,前例已经操练过了。相反,我们要例示的重点在于使一个Set唯一无二的行为:
 

//: Set1.java
// Things you can do with Sets
package c08.newcollections;
import java.util.*;

public class Set1 {
  public static void testVisual(Set a) {
    Collection1.fill(a);
    Collection1.fill(a);
    Collection1.fill(a);
    Collection1.print(a); // No duplicates!
    // Add another set to this one:
    a.addAll(a);
    a.add("one"); 
    a.add("one"); 
    a.add("one");
    Collection1.print(a);
    // Look something up:
    System.out.println("a.contains(\"one\"): " +
      a.contains("one"));
  }
  public static void main(String[] args) {
    testVisual(new HashSet());
    testVisual(new TreeSet());
  }
} ///:~

反复的值被添加到Set,但在打印的时候,我们会发明Set只接管每个值的一个实例。
运行这个措施时,会留意到由HashSet维持的顺序与ArraySet是差异的。这是由于它们回收了差异的要领来生存元素,以便它们今后的定位。ArraySet保持着它们的顺序状态,而HashSet利用一个散列函数,这是出格为快速检索设计的)。建设本身的范例时,必然要留意Set需要通过一种方法来维持一种存储顺序,就象本章早些时候展示的“groundhog”(土拔鼠)例子那样。下面是一个例子:
 

//: Set2.java
// Putting your own type in a Set
package c08.newcollections;
import java.util.*;

class MyType implements Comparable {
  private int i;
  public MyType(int n) { i = n; }
  public boolean equals(Object o) {
    return 
      (o instanceof MyType) 
      && (i == ((MyType)o).i);
  }
  public int hashCode() { return i; }
  public String toString() { return i + " "; }
  public int compareTo(Object o) {
    int i2 = ((MyType) o).i;
    return (i2 < i  -1 : (i2 == i  0 : 1));
  }
}

public class Set2 {
  public static Set fill(Set a, int size) {
    for(int i = 0; i < size; i++)
      a.add(new MyType(i));
    return a;
  }
  public static Set fill(Set a) {
    return fill(a, 10);
  }
  public static void test(Set a) {
    fill(a);
    fill(a); // Try to add duplicates
    fill(a);
    a.addAll(fill(new TreeSet()));
    System.out.println(a);
  }
  public static void main(String[] args) {
    test(new HashSet());
    test(new TreeSet());
  }
} ///:~

#p#分页标题#e#

对equals()及hashCode()的界说遵照“groundhog”例子已经给出的形式。在两种环境下都必需界说一个equals()。但只有要把类置入一个HashSet的前提下,才有须要利用hashCode()——这种环境是完全有大概的,因为凡是应先选择作为一个Set实现。

 

    关键字:

天才代写-代写联系方式