Java 简明教程

Java - Set Interface

Set Collection 是一个不能包含重复元素的集合。它模拟了数学的集合抽象。

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

Set 接口仅包含从集合继承的方法,并增加了不允许重复元素的限制。

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Set 还对 equals 和 hashCode 操作的行为添加了一个更强的契约,即使 Set 实例的实现类型不同,也能对它们进行有意义的比较。

Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

Set Interface Methods

Set 声明的方法在以下表中总结 −

The methods declared by Set are summarized in the following table −

Sr.No.

Method & Description

1

add( ) Adds an object to the collection.

2

clear( ) Removes all objects from the collection.

3

contains( ) Returns true if a specified object is an element within the collection.

4

isEmpty( ) Returns true if the collection has no elements.

5

iterator( ) Returns an Iterator object for the collection, which may be used to retrieve an object.

6

remove( ) Removes a specified object from the collection.

7

* size( )* Returns the number of elements in the collection.

Set Interface Examples

Set 已在其各个类中实现,如 HashSetTreeSetLinkedHashSet。以下是 Java 中 Set 接口的一些实现。

Set has its implementation in various classes like HashSet, TreeSet, LinkedHashSet. Below are some of the implementations of the Set interface in Java.

Example to Implement Set Using HashSet

以下是一个使用 HashSet 来解释 Set 功能的示例 −

Following is an example to explain Set functionality using HashSet −

import java.util.HashSet;
import java.util.Set;

public class SetDemo {

  public static void main(String args[]) {
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new HashSet<>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);
      }
      catch(Exception e) {}
   }
}
[34, 22, 10, 60, 30]

Example to Implement Set Using TreeSet

以下是一个使用 TreeSet 来解释 Set 功能的示例 −

Following is an example to explain Set functionality using TreeSet −

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class SetDemo {

  public static void main(String args[]) {
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new HashSet<>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);

         TreeSet<Integer> sortedSet = new TreeSet<>(set);
         System.out.println("The sorted list is:");
         System.out.println(sortedSet);

         System.out.println("The First element of the set is: "+ (Integer)sortedSet.first());
         System.out.println("The last element of the set is: "+ (Integer)sortedSet.last());
      }
      catch(Exception e) {}
   }
}
[34, 22, 10, 60, 30]
The sorted list is:
[10, 22, 30, 34, 60]
The First element of the set is: 10
The last element of the set is: 60

Example to Implement Set Using LinkedHashSet

以下是一个使用 LinkedHashSet 操作来解释 Set 功能的示例 −

Following is an example to explain Set functionality using LinkedHashSet opearations −

import java.util.LinkedHashSet;
import java.util.Set;

public class SetDemo {

  public static void main(String args[]) {
      int count[] = {34, 22,10,60,30,22};
      Set<Integer> set = new LinkedHashSet<>();
      try {
         for(int i = 0; i < 5; i++) {
            set.add(count[i]);
         }
         System.out.println(set);
      }
      catch(Exception e) {}
   }
}
[34, 22, 10, 60, 30]