原文地址:
http://examples.javacodegeeks.com/core-java/util/set/java-set-example/
In this example we will demonstrate the use of the interface
, which is part of the Java Collections Framework. It extends the interface Collection so that all elements contained have no dublicates and only one null element may appear.There are several classes implementing the interface, such as
, , , , and . The classes and are the most commonly used.implements the interface using a hash table. It offers high performance for the basic methods such as add, remove, contains and size, however the ordering of the elements cannot be tracked and possibly it could change at anytime during execution.
uses a to store the elements, which keeps them sorted by their natural order or by the comparator that we prefer to use.
1. How to create a Set:
HashSet()构造一个新的空 set,其底层 HashMap 实例的默认初始容量是 16,加载因子是 0.75。
HashSet(Collection<? extends E> c)
构造一个包含指定 collection 中的元素的新 set。
HashSet(int initialCapacity)
构造一个新的空 set,其底层 HashMap 实例具有指定的初始容量和默认的加载因子(0.75)。
HashSet(int initialCapacity, float loadFactor)
构造一个新的空 set,其底层 HashMap 实例具有指定的初始容量和指定的加载因子
2. Common Methods:
- : Adds a new element, if it does not exist already.
- : Adds all the elements of the given collection, if the do not exist already. If the given collection is also a set, then the execution of the method results in the union of the two sets.
- : Returns true if the elements given exists in the set.
- : Returns true if all the elements in the given collection exist in the set. In case the given collection is a set, the method return true if it is a subset of this set.
- : Returns true if the given object that is compared with this set is also a set, both of them contain the same number of elements and every element of the given set is contained in this set.
- : Returns the number of the elements in the set.
- : Removes the specified elements from the set.
- : Removes from the set all the elements that the collection contains.
- : Removes all the elements from the set, resulting in an empty set.
- : Returns true if the set has no elements.
- : Returns the hash code value of this set. The hash code of a set is the sum of the hash codes of the elements contained in the set.
- : Return an array containing all the elements of this set.
3. Examples of using Set in Java:
package lvzheming;
import java.util.*;
public class ExamplesofusingSetinJava{
public static void main(String args[]) {
// We create a new, empty set
Set mySet1 = new HashSet();
// We add a few elements
mySet1.add("A");
mySet1.add("B");
mySet1.add("C");
mySet1.add("D");
// Print the elements of the Set
System.out.println("mySet1: " + mySet1);
// Create a list and add some elements
List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
// Now create the set using the appropriate constructor
Set mySet2 = new HashSet(list);
// Print the elements of the list an the the set
System.out.println("list: " + list);
System.out.println("mySet2: " + mySet2);
// Compare the two sets
System.out.println("MySet1 matches mySet2: " + mySet1.equals(mySet2));
// Now we will remove one element from mySet2 and compare again
mySet2.remove("A");
System.out.println("mySet2: " + mySet2);
System.out.println("MySet1 matches mySet2: " + mySet1.equals(mySet2));
mySet2.add("A");
// Lets check if our sets contain all the elements of the list
System.out.println("MySet1 contains all the elements: " + mySet1.containsAll(list));
System.out.println("MySet2 contains all the elements: " + mySet2.containsAll(list));
// Use of Iterator in Set
//迭代器能够自动识别集合元素的类型,只要使用next就可以自动输出所有集合的内容
Iterator iterator = mySet1.iterator();
//Returns true if the iteration has more elements.
while (iterator.hasNext()) {
//Returns the next element in the iteration.
System.out.println("Iterator loop: " + iterator.next());
}
// Use of for-each in Set
for (Object str : mySet1) {
System.out.println("for-each loop: " + str);
}
// Clearing all the elements
mySet1.clear();
System.out.println("mySet1 is Empty: " + mySet1.isEmpty());
// Checking the number of elements
System.out.println("mySet1 has: " + mySet1.size() + " Elements");
System.out.println("mySet2 has: " + mySet2.size() + " Elements");
// Creating an Array with the contents of the set
String[] array = (String[]) mySet2.toArray(new String[mySet2.size()]);
System.out.println("The array:" + Arrays.toString(array));
}
}