99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Java Set集合

2018-02-20 03:02 更新

Java集合教程 - Java Set集合


Set 表示唯一對象的集合。集合中元素的排序是不相關的。

集合框架提供三種類型的集合:

  • 數(shù)學集
  • 排序集
  • 導航集

數(shù)學集

Set 接口對數(shù)學中的一組進行建模。集合是唯一元素的集合。

Java最多允許一個Set中的一個空元素。 Set 中元素的排序并不重要。

Java不保證 Set 中元素的排序。

當循環(huán)遍歷 Set 的所有元素時,你得到 Set 中的每個元素一次。

集合框架提供 HashSet 類作為實現(xiàn)為設置接口。

以下代碼顯示了如何創(chuàng)建一個Set并向其添加元素。 當向集合添加重復元素時,它們將被忽略。

如果比較它們,則在集合中的兩個元素被認為是相等的使用 equals()方法返回true。

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

public class Main {
  public static void main(String[] args) {

    Set<String> s1 = new HashSet<>();

    // Add a few elements
    s1.add("HTML");
    s1.add("CSS");
    s1.add("XML");
    s1.add("XML"); // Duplicate

    // Create another set by copying s1
    Set<String> s2 = new HashSet<>(s1);
    // Add a few more elements 
    s2.add("Java"); 
    s2.add("SQL");
    s2.add(null); // one null is fine
    s2.add(null); // Duplicate

    System.out.println("s1: " + s1);
    System.out.println("s1.size(): " + s1.size());

    System.out.println("s2: " + s2);
    System.out.println("s2.size(): " + s2.size());
  }
}

上面的代碼生成以下結果。


LinkedHashSet

集合框架提供 LinkedHashSet 類作為 Set 接口的另一個實現(xiàn)類。

HashSet 不保證順序元素。 LinkedHashSet 在插入元素時保持元素順序。

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

public class Main {
  public static void main(String[] args) {

    Set<String> s1 = new LinkedHashSet<>();
    s1.add("A");
    s1.add("B");
    s1.add("C");
    s1.add("D");
    System.out.println("LinkedHashSet: " + s1);


  }
}

上面的代碼生成以下結果。

集合操作

我們可以對集合執(zhí)行并集,交集和差分運算。

// Union  of  s1  and  s2  will be  stored in s1 
s1.add(s2);

// Intersection of  s1  and  s2  will be  stored in s1 
s1.retainAll(s2);

// Difference of  s1  and  s2  will be  stored in s1 
s1.removeAll(s2);

在集合操作期間,修改s1。要保持原始設置不變,請在操作之前復制:

Set  s1Unions2  = new HashSet(s1); // Make a  copy  of  s1
s1Unions2.addAll(s2);

要測試集合s1是否是另一個集合s2的子集,請使用s2.containsAll(s1)方法。

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

public class Main {
  public static void main(String[] args) {
    Set<String> s1 = new HashSet<>();
    s1.add("HTML");
    s1.add("CSS");
    s1.add("XML");

    Set<String> s2 = new HashSet<>();
    s2.add("Java");
    s2.add("Javascript");
    s2.add("CSS");

    System.out.println("s1: " + s1);
    System.out.println("s2: " + s2);

    performUnion(s1, s2);
    performIntersection(s1, s2);
    performDifference(s1, s2);
    testForSubset(s1, s2);
  }

  public static void performUnion(Set<String> s1, Set<String> s2) {
    Set<String> s1Unions2 = new HashSet<>(s1);
    s1Unions2.addAll(s2);
    System.out.println("s1 union  s2: " + s1Unions2);
  }

  public static void performIntersection(Set<String> s1, Set<String> s2) {
    Set<String> s1Intersections2 = new HashSet<>(s1);
    s1Intersections2.retainAll(s2);
    System.out.println("s1 intersection  s2: " + s1Intersections2);
  }

  public static void performDifference(Set<String> s1, Set<String> s2) {
    Set<String> s1Differences2 = new HashSet<>(s1);
    s1Differences2.removeAll(s2);

    Set<String> s2Differences1 = new HashSet<>(s2);
    s2Differences1.removeAll(s1);

    System.out.println("s1 difference s2: " + s1Differences2);
    System.out.println("s2 difference s1: " + s2Differences1);
  }

  public static void testForSubset(Set<String> s1, Set<String> s2) {
    System.out.println("s2 is  subset s1: " + s1.containsAll(s2));
    System.out.println("s1 is  subset s2: " + s2.containsAll(s1));
  }

}

上面的代碼生成以下結果。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號