用Java的Set實現交并差等集合運算

放碼過來

package com.lun.util;import java.util.HashSet;
import java.util.Set;public class SetUtils {public static <T> Set<T> union(Set<T> setA, Set<T> setB) {Set<T> tmp = new HashSet<T>(setA);tmp.addAll(setB);return tmp;}public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {Set<T> tmp = new HashSet<T>();for (T x : setA)if (setB.contains(x))tmp.add(x);return tmp;}public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {Set<T> tmp = new HashSet<T>(setA);tmp.removeAll(setB);return tmp;}public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) {Set<T> tmpA;Set<T> tmpB;tmpA = union(setA, setB);tmpB = intersection(setA, setB);return difference(tmpA, tmpB);}public static <T> boolean isSubset(Set<T> setA, Set<T> setB) {return setB.containsAll(setA);}public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) {return setA.containsAll(setB);}public static void main(String args[]) {HashSet<Character> set1 = new HashSet<Character>();HashSet<Character> set2 = new HashSet<Character>();set1.add('A');set1.add('B');set1.add('C');set1.add('D');set2.add('C');set2.add('D');set2.add('E');set2.add('F');System.out.println("set1: " + set1);System.out.println("set2: " + set2);System.out.println("Union: " + union(set1, set2));System.out.println("Intersection: " + intersection(set1, set2));System.out.println("Difference (set1 - set2): " + difference(set1, set2));System.out.println("Difference (set2 - set1): " + difference(set2, set1));System.out.println("Symmetric Difference: " + symDifference(set1, set2));HashSet<Character> set3 = new HashSet<Character>(set1);set3.remove('D');System.out.println("set3: " + set3);System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3));System.out.println("Is set1 a superset of set2? " + isSuperset(set1, set3));System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1));System.out.println("Is set3 a superset of set1? " + isSuperset(set3, set1));}}/* result:set1: [A, B, C, D]
set2: [C, D, E, F]
Union: [A, B, C, D, E, F]
Intersection: [C, D]
Difference (set1 - set2): [A, B]
Difference (set2 - set1): [E, F]
Symmetric Difference: [A, B, E, F]
set3: [A, B, C]
Is set1 a subset of set2? false
Is set1 a superset of set2? true
Is set3 a subset of set1? true
Is set3 a superset of set1? false*/

參考資料

Set operations: union, intersection, difference, symmetric difference, is subset, is superset : Set ? Collections Data Structure ? Java

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445970.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445970.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445970.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

post方法就反回了一個string字符串前臺怎么接_Golang Web入門(2):如何實現一個RESTful風格的路由...

摘要在上一篇文章中&#xff0c;我們聊了聊在Golang中怎么實現一個Http服務器。但是在最后我們可以發現&#xff0c;固然DefaultServeMux可以做路由分發的功能&#xff0c;但是他的功能同樣是不完善的。由DefaultServeMux做路由分發&#xff0c;是不能實現RESTful風格的API的&a…

FFmpeg源代碼簡單分析-通用-avcodec_open2()

參考鏈接 FFmpeg源代碼簡單分析&#xff1a;avcodec_open2()_雷霄驊的博客-CSDN博客 avcodec_open2() 該函數用于初始化一個音視頻編解碼器的AVCodecContextavcodec_open2()的聲明位于libavcodec\avcodec.h&#xff0c;如下所示。 /*** Initialize the AVCodecContext to use…

統計MySQL中某數據庫硬盤占用量大小

放碼過來 select TABLE_NAME, concat(truncate(data_length/1024/1024,2), MB) as data_size, concat(truncate(index_length/1024/1024,2), MB) as index_size from information_schema.tables where TABLE_SCHEMA your_db_name order by data_length desc;運行結果 參考…

halcon 相似度_Halcon分類函數,shape模型

《zw版Halcon-delphi系列原創教程》 Halcon分類函數013,shape模型為方便閱讀&#xff0c;在不影響說明的前提下&#xff0c;筆者對函數進行了簡化&#xff1a;:: 用符號“**”&#xff0c;替換&#xff1a;“procedure”:: 用大寫字母“X”&#xff0c;替換&#xff1a;“IHUnt…

用Python將文件夾打包成Zip并備份至U盤

需求概要 將maven工程打包并備份至U盤。為了簡單起見&#xff0c;只需備份工程中的src文件夾和pom.xml文件即可。 放碼過來 import os import zipfile import datetime import shutilnowTimeStr datetime.datetime.now().strftime("%Y%m%d%H%M") newZipFileName …

FFmpeg源代碼簡單分析-通用-avcodec_close()

參考鏈接 FFmpeg源代碼簡單分析&#xff1a;avcodec_close()_雷霄驊的博客-CSDN博客_avcodec_close avcodec_close() 該函數用于關閉編碼器avcodec_close()函數的聲明位于libavcodec\avcodec.h&#xff0c;如下所示。 ?該函數只有一個參數&#xff0c;就是需要關閉的編碼器的…

redis 緩存過期默認時間_redis緩存過期機制

筆者在線上使用redis緩存的時候發現即使某些查詢已經設置了無過期時間的緩存,但是查詢仍然非常耗時。經過排查,發現緩存確實已經不存在,導致了緩存擊穿,查詢直接訪問了mysql數據庫。因為我們用的是公司公共的redis緩存服務器,在和運維人員交流后發現可能是redis的內存淘汰…

FFmpeg源代碼簡單分析-解碼-打開媒體的函數avformat_open_input

參考鏈接 圖解FFMPEG打開媒體的函數avformat_open_input_雷霄驊的博客-CSDN博客_avformat_open_input 使用FFmpeg源代碼簡單分析&#xff1a;avformat_open_input()_雷霄驊的博客-CSDN博客_avformat_open_input() avformat_open_input FFmpeg打開媒體的的過程開始于avformat_…

redis session java獲取attribute_面試題:給我說說你能想到幾種分布式session實現?...

作者&#xff1a;yanglbme 來源&#xff1a;https://github.com/doocs/advanced-java/blob/master/docs/distributed-system/distributed-session.md# 面試官心理分析面試官問了你一堆 dubbo 是怎么玩兒的&#xff0c;你會玩兒 dubbo 就可以把單塊系統弄成分布式系統&#xff0…

Projection投影

解釋一 Projection means choosing which columns (or expressions) the query shall return. Selection means which rows are to be returned. if the query is select a, b, c from foobar where x3;then “a, b, c” is the projection part, “where x3” the selecti…

FFmpeg源代碼簡單分析-解碼-avformat_find_stream_info()

參考鏈接 FFmpeg源代碼簡單分析&#xff1a;avformat_find_stream_info()_雷霄驊的博客-CSDN博客_avformat_find_stream_info avformat_find_stream_info() ?該函數可以讀取一部分視音頻數據并且獲得一些相關的信息avformat_find_stream_info()的聲明位于libavformat\avform…

Tail Recursion尾遞歸

什么是尾遞歸 Tail Recursion /te?l r??k??r?n/ In traditional recursion, the typical model is that you perform your recursive calls first, and then you take the return value of the recursive call and calculate the result. In this manner, you don’t g…

python遞歸算法案例教案_python教案

第五單元進階程序設計(總10課時)第一節選擇編程語言(1課時)一、教學目標1、了解程序設計語言和兩種翻譯方式&#xff1b;2、了解Python背景、功能、安裝&#xff0c;熟悉Python編程環境&#xff1b;3、編程初體驗。體驗一個小程序從建立、輸入、調試、運行、保存的全過程。掌握…

FFmpeg源代碼簡單分析-解碼-av_read_frame()

參考鏈接 ffmpeg 源代碼簡單分析 &#xff1a; av_read_frame()_雷霄驊的博客-CSDN博客_ffmpeg frame av_read_frame() ffmpeg中的av_read_frame()的作用是讀取碼流中的音頻若干幀或者視頻一幀。例如&#xff0c;解碼視頻的時候&#xff0c;每解碼一個視頻幀&#xff0c;需要…

數據庫 流量切分_私域流量之社群運營技巧,社群運營技巧解析

一、明白社群運營的目的1、社群的目的確立任何一個社群(組織)成立的時分&#xff0c;都是承載著一定的目的的&#xff0c;這個目的就像是北極星一樣&#xff0c;指引著我們的方向。確立運營目的的過程&#xff0c;也是在尋覓北極星的過程。社群運營屬于觸達用戶的一種方式&…

用Python在Tomcat成功啟動后自動打開瀏覽器訪問Web應用

前提條件 WindowsPython 2.7需設置CATALINA_HOME環境變量 放碼過來 # -*- coding: utf-8 -* import os import time import subprocesstomcatStartFilePath C:\\tomcat\\apache-tomcat-7.0.90-windows-x64\\apache-tomcat-7.0.90\\bin\\startup.bat browserPath C:\\Users…

FFmpeg源代碼簡單分析-解碼-avcodec_send_packet 和 avcodec_receive_frame 替代 avcodec_decode_video2

參考鏈接 ffmpeg 源代碼簡單分析 &#xff1a; avcodec_decode_video2()_雷霄驊的博客-CSDN博客_avcodec_decode_video2 avcodec_decode_video2 ffmpeg中的avcodec_decode_video2()的作用是解碼一幀視頻數據。輸入一個壓縮編碼的結構體AVPacket&#xff0c;輸出一個解碼后的結…

FFmpeg源代碼簡單分析-解碼-avformat_close_input()

參考鏈接 FFmpeg源代碼簡單分析&#xff1a;avformat_close_input()_雷霄驊的博客-CSDN博客_avformat_close_input avformat_close_input() 本文簡單分析FFmpeg的avformat_close_input()函數。該函數用于關閉一個AVFormatContext&#xff0c;一般情況下是和avformat_open_inp…

android 使用shell模擬觸屏_[Android]通過adb shell input上報命令模擬屏幕點擊事件【轉】...

常用的 input上報命令&#xff1a;input text 1234 實際向界面注入1234文字&#xff0c;有輸入框&#xff0c;能明顯看到效果input keyevent 4 鍵盤事件&#xff0c;4 為返回input tap 100 300 單擊觸屏事件 &#xff0c;模擬點擊x100 y 300 位置input swipe 100 300 500 300 …

用Python連接MySQL并進行CRUD

Tag: MySQL, PyMySQL, Python 準備條件 Python 2.7MySQL 5.5安裝 PyMySQL pip install PyMySQL 放碼過來 創建一數據表 CREATE TABLE users (id int(11) NOT NULL AUTO_INCREMENT,email varchar(255) COLLATE utf8_bin NOT NULL,password varchar(255) COLLATE utf8_bin N…