Tutorial 4 of Java Collection framework series: Set Interface

Source: Internet
Author: User
Tags addall

OneSetIs a set that cannot contain repeated elements. It maps the set abstraction in the mathematical sense. The Set interface only adds the restriction that element duplication is not allowed on the basis of methods inherited from the collecton interface. Set also imposes stronger limits on the behavior conventions of equals and hashcode, so that the Set instance allows meaningful comparisons, even if their specific implementations are different. Two set instances are equal (equal) If they contain the same element.

A set is a set that cannot contain repeated elements. It maps the set abstraction in the mathematical sense. The Set interface only adds the restriction that element duplication is not allowed on the basis of methods inherited from the collecton interface. Set also imposes stronger limits on the behavior conventions of equals and hashcode, so that the Set instance allows meaningful comparisons, even if their specific implementations are different. Two set instances are equal (equal) If they contain the same element.

The following is the api implementation of the set interface:

public interface Set<E> extends Collection<E> {    // Basic operations    int size();    boolean isEmpty();    boolean contains(Object element);    // optional    boolean add(E element);    // optional    boolean remove(Object element);    Iterator<E> iterator();    // Bulk operations    boolean containsAll(Collection<?> c);    // optional    boolean addAll(Collection<? extends E> c);    // optional    boolean removeAll(Collection<?> c);    // optional    boolean retainAll(Collection<?> c);    // optional    void clear();    // Array Operations    Object[] toArray();    <T> T[] toArray(T[] a);}

The Java platform provides general set implementations: hashset, treeset, and javashashset. Hashset stores elements in a hash table. It is the most efficient set implementation, but it cannot determine the iteration sequence. Treeset stores the elements in the red/black tree. The elements are sorted by values, which is a little slower than hashset. Linkedhashset is the implementation of a hashset linked list. It maintains the order of element insertion, but its access performance is inferior to that of hashset and treeset.

Here is a simple but useful set application scenario. Suppose you have a collection C, and you want to create another collection, but you must remove the repeated elements (only one ). The following line of code meets your requirements:

Collection<Type> noDups = new HashSet<Type>(c);

Here is another variant implementation to ensure the sequence of elements in the original set:

Collection<Type> noDups = new LinkedHashSet<Type>(c);

The following is a general method that encapsulates the above line of code:

public static <E> Set<E> removeDups(Collection<E> c) {   return new LinkedHashSet<E>(c);}


Basic operations on the set Interface

The size method returns the number of elements in the set. The isempty method checks whether the set is null. The add method adds a specified element to the set. If the element does not exist in the Set, a Boolean value is returned to identify whether the element is successfully added. Similarly, the remove method removes a specified element from the set. If the element exists in the Set, a Boolean value is returned to identify whether the element is successfully removed. The iterator method returns the set iterator.

The following program uses a group of words as the parameter ARGs to print out any repeated words, the number of all non-repeated words, and the list of non-repeated words.

import java.util.*;public class FindDups {    public static void main(String[] args) {        Set<String> s = new HashSet<String>();        for (String a : args)            if (!s.add(a))                System.out.println("Duplicate detected: " + a);        System.out.println(s.size() + " distinct words: " + s);    }}

Run the program with the following command:

java FindDups i came i saw i left

The program output is as follows:

Duplicate detected: iDuplicate detected: i4 distinct words: [i, left, saw, came]

Note the above CodeAlways reference the corresponding set through the interface type (SET), instead of referencing the specific implementation type (hashset). This is a strongly recommended programming practice, because it allows us to switch the specific implementation of the Set more flexibly, just need to change the constructor function. If the variables used to store the set or parameters used to pass to other methods are declared as the specific implementation type of the Set, rather than the interface type of the set, these variables or parameters must also change with the specific implementation.

In the preceding example, the specific implementation type of set is hashset, which cannot guarantee the sequence of elements in the set. If you want the program to print words in alphabetical order, you only need to change the specific implementation type of set from hashset to treeset. Modify only the previous line of code:

Set<String> s = new TreeSet<String>();

The following output is generated:

java FindDups i came i saw i leftDuplicate detected: iDuplicate detected: i4 distinct words: [came, i, left, saw]


Bulk operation)

Batch operations are especially suitable for set operations. Performing batch operations is equivalent to performing an operation in the algebraic sense of the set. Assume that both S1 and S2 are set. The following are various batch operations:

  • S1.containsall (S2)-If S2 is a subset of S1, true is returned; otherwise, false is returned.
  • S1.addall (S2)-returns the Union of S1 and S2.
  • S1.retainall (S2)-returns the intersection of S1 and S2.
  • S1.removeall (S2)-returns the difference set between S1 and S2 (s1-s2, that is, a set of all elements in S1 but not in S2)

To calculate the Union, intersection, and difference sets of the Two sets without modifying them, the caller must first copy them and then call bulk opertaion. For example:

Set<Type> union = new HashSet<Type>(s1);union.addAll(s2);Set<Type> intersection = new HashSet<Type>(s1);intersection.retainAll(s2);Set<Type> difference = new HashSet<Type>(s1);difference.removeAll(s2);

The result set type of the above Code is hashset.

Let's review the previous finddups program. Suppose you want to know which words appear only once, which words appear more than once, but do not want to print words repeatedly. This effect can be achieved using two sets. One set contains all the words in the parameter list, and the other set contains only the repeated words. The word that appears only once is the difference set of the two sets. The following is code implementation:

import java.util.*;public class FindDups2 {    public static void main(String[] args) {        Set<String> uniques = new HashSet<String>();        Set<String> dups    = new HashSet<String>();        for (String a : args)            if (!uniques.add(a))                dups.add(a);        // Destructive set-difference        uniques.removeAll(dups);        System.out.println("Unique words:    " + uniques);        System.out.println("Duplicate words: " + dups);    }}

The code execution result is as follows:

Unique words:    [left, saw, came]Duplicate words: [i]

There is also a less common set algebra operation, that is, the symmetric set difference set-composed of elements in two sets, but the elements cannot contain in the intersection of two sets. The following code implements this effect:

Set <type> metrics ricdiff = new hashset <type> (S1); Metrics ricdiff. addall (S2); // Union set <type> TMP = new hashset <type> (S1); TMP. retainall (S2); // TMP is the intersection of metrics ricdiff. removeall (TMP );


Array Operations of the Set Interface

The array operation of the Set interface is no different from the array operation of the previous collection interface.

Original article: "Translation" Java Collection framework series tutorial 4: Set Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.