跳转至

Java 集合Set

1. 简介

  Java Set是java.util包的一部分,并扩展了java.util.Collection接口。它不允许使用重复元素,并且在max时只能容纳一个null元素。Java Set界面的几个重要特性如下:

  • set接口是无序的对象集合,其中不能存储重复的值。
  • Java Set不提供对元素插入或删除位置的控制。
  • 基本上,Set由HashSet,LinkedHashSet或TreeSet(排序表示)实现。
  • 设置有各种方法添加,删除清除,大小等,以增强此接口的使用

setdemo.class

package ArrayListDemo;

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

/**
 * @author summer
 * @create 2020-02-06 17:08
 */
public class setdemo {
    public static void main(String[] args) {
        Set<String> hset = new HashSet<String>();
        hset.add("www");
        hset.add("www");
        hset.add(".");
        hset.add("jd");
        hset.add(".");
        hset.add("com");
        System.out.println("Set output without the duplicates");
        System.out.println(hset);

        // Set demonstration using TreeSet
        System.out.print("Sorted Set after passing into TreeSet");
        Set<String> tree_Set = new TreeSet<String>(hset);
        System.out.println(tree_Set);
    }
}

输出:

Set output without the duplicates
[com, www, jd, .]
Sorted Set after passing into TreeSet[., com, jd, www]

注意
(请注意,我们输入了一个重复的实体,但它没有显示在输出中。
 另外,我们可以通过将无序的Set in作为TreeSet的参数传递来直接对条目进行排序)。

我们可以看到在最终输出中忽略了重复条目“www”,Set接口不允许重复条目。
(请注意,我们输入了一个重复的实体,但它没有显示在输出中。
 另外,我们可以通过将无序的Set in作为TreeSet的参数传递来直接对条目进行排序)。

2. 例子

现在让我们看一下Set的一些基本操作,即Union,Intersection和Difference。我们来看两个整数集的例子:

[1,3,2,4,8,9,0]
[1,3,7,5,4,0,7,5]
这允许用户向另一个添加一个步骤。由于Set本身不允许任何重复的条目,我们不需要处理常见的值。 预期输出:
Union : [0, 1, 2, 3, 4, 5, 7, 8, 9]

Set的交集:

此操作用于保留两个集合的公共值。

Intersection : [0, 1, 3, 4]

Set的差异:

此操作用于从另一个集合中删除一个集合的所有值。

Difference : [2, 8, 9]

setdemo2.class

package ArrayListDemo;

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

/**
 * @author summer
 * @create 2020-02-06 17:14
 */
public class setdemo2 {
    public static void main(String[] args) {
        Set<Integer> a = new HashSet<Integer>();
        a.addAll(Arrays.asList(new Integer[] {1, 3, 2, 4, 8, 9, 0}));
        Set<Integer> b = new HashSet<Integer>();
        b.addAll(Arrays.asList(new Integer[] {1, 3, 7, 5, 4, 0, 7, 5}));
        System.out.println("开始 a:" +a);
        System.out.println("开始 b:" +b);

        // To find union
        Set<Integer> union = new HashSet<Integer>(a);
        union.addAll(b);
        System.out.print("Union of the two Set");
        System.out.println(union);
        System.out.println("中间1 a:" +a);
        System.out.println("中间1 b:" +b);

        // To find intersection
        Set<Integer> intersection = new HashSet<Integer>(a);
        intersection.retainAll(b);
        System.out.print("Intersection of the two Set");
        System.out.println(intersection);
        System.out.println("中间2 a:" +a);
        System.out.println("中间2 b:" +b);

        // To find the symmetric difference
        Set<Integer> difference = new HashSet<Integer>(a);
        difference.removeAll(b);
        System.out.print("Difference of the two Set");
        System.out.println(difference);
        System.out.println("中间3 a:" +a);
        System.out.println("中间3 b:" +b);

    }
}
输出
开始 a:[0, 1, 2, 3, 4, 8, 9]
开始 b:[0, 1, 3, 4, 5, 7]
Union of the two Set[0, 1, 2, 3, 4, 5, 7, 8, 9]
中间1 a:[0, 1, 2, 3, 4, 8, 9]
中间1 b:[0, 1, 3, 4, 5, 7]
Intersection of the two Set[0, 1, 3, 4]
中间2 a:[0, 1, 2, 3, 4, 8, 9]
中间2 b:[0, 1, 3, 4, 5, 7]
Difference of the two Set[2, 8, 9]
中间3 a:[0, 1, 2, 3, 4, 8, 9]
中间3 b:[0, 1, 3, 4, 5, 7]

设置接口中的方法:

序号 方法名 作用
1 add() 此方法用于一次向集合添加一个对象
2 clear() 此方法用于从集合中删除所有元素
3 contains 此方法用于验证集合中是否存在指定元素
4 isEmpty 此方法用于检查集合是否为空。
5 iterator 用于返回Iterator对象,该对象可用于从集合中检索对象。
6 remove 用于从集合中删除指定的对象。
7 size 用于了解集合中存在的元素的大小或数量。

下面的程序说明了几种Set Interface方法的操作

// Java code to illustrate clear()
import java.io.*;
import java.util.HashSet;

public class HashSetDemo{
    public static void main(String args[])
    {
        // Creating an empty HashSet
        HashSet<String> set = new HashSet<String>();

        // Use add() method to add elements into the Set
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("4");
        set.add("Geeks");

        // Displaying the HashSet
        System.out.println("HashSet: " + set);

        // Using isEmpty() to verify for the emptiness
        System.out.println("The set is empty? "+
                                       set.isEmpty());

        // Does the set contain "Geeks"
        System.out.println("Does the set contain 'Geeks'?" 
                                 + set.contains("Geeks"));

        // Getting the size of the set
        System.out.println("The size of the set is "+
                                             set.size());

        // Removing "To" from the set
        set.remove("To");

        // Displaying the HashSet
        System.out.println("HashSet: " + set);        

        // Clearing the HashSet using clear() method
        set.clear();

        // Displaying the final Set after clearing;
        System.out.println("The final set: " + set);
    }
}
输出
HashSet: [4, Geeks, Welcome, To]
The set is empty? false
Does the set contain 'Geeks'?true
The size of the set is 4
HashSet: [4, Geeks, Welcome]
The final set: []