Java HashSet应用一例

/*
*
本例演示Java HashSet的基本应用
用HashSet分析命令行输入参数和现有集合的关系。
参数通过String[] args传入,现有集合源自args参数,即每一轮把一个arg参数添加进encountered集合
*
需要理解如下内容:

  • 基本逻辑:本例从命令行中传入若干字符串参数,参数将保存在args数组中,每次取出一个,并与encountered数组的副本commonSubset做交集比较

  • 判断二者关系是子集,父集,公共集,无关联等;

  • encountered初始状态为空,每一轮最后把当前取出的arg添加到encountered中

  • 用到的方法

  • a.retainAll(Collection c)

  • a.addAll()

  • a.add()

  • a.size()

  • a.containAll(b)

  • retainAll取交集
    *Boolean a.retainAll(b)

  • 返回值是布尔值

  • true—集合a发生变化

  • false–集合a未发生变化

  • 结果:返回集合的交集,直接写入调用集合a

  • containsAll判断a集合是否包含所有b集合元素

  • a.containsAll(b)

  • 传入的命令行参数:she said she is signorina arsdeghino arsdeghinob

  • */

import java.util.*;

public class SetExample {
    public static void main(String[] args) {
        Set<Character> encountered = new HashSet<>();//定义一个空Set,记录args中出现过的字符集合
//        HashSet<Character> hs = new HashSet<>();
        for (String arg : args) {//遍历args数组
            Set<Character> chars = new HashSet<>();//setup a Char set named chars storing for each arg
            int size = arg.length();//取元素个数,对String而言是length()方法,对Array而言是length属性
            for (int j = 0; j < size; j++) {
                chars.add(arg.charAt(j));//把arg逐个字符添加到chars集合
            }
            Set<Character> commonSubset = new HashSet<>(encountered);//由encountered集合生成一个副本commonSubset集合
            commonSubset.retainAll(chars);//和当前arg做交集
            boolean areDistinct = commonSubset.size() == 0;//判断是否有交集,size=0意味着没有交集,boolean为true
            if (areDistinct) {
                System.out.println(chars + " and " + encountered + " are separated.");
            } else {
                boolean isSubnet = encountered.containsAll(chars);
                boolean isSuperset = chars.containsAll(encountered);
                if (isSubnet && isSuperset) {
                    System.out.println(chars + " is same as " + encountered);
                } else if (isSubnet) {
                    System.out.println(chars + "'s father collection is " + encountered);
                    continue;
                } else if (isSuperset) {
                    System.out.println(chars + "'s child collection is " + encountered);
                } else {
                    System.out.println(chars + " and " + encountered + " commonSubset is " + commonSubset);
                }
            }
            encountered.addAll(chars);
        }
    }
}

执行结果:
[s, e, h] and [] are separated.
[a, s, d, i] and [s, e, h] commonSubset is [s]
[s, e, h]'s father collection is [a, s, d, e, h, i]
[s, i]'s father collection is [a, s, d, e, h, i]
[a, r, s, g, i, n, o] and [a, s, d, e, h, i] commonSubset is [a, s, i]
[a, r, s, d, e, g, h, i, n, o] is same as [a, r, s, d, e, g, h, i, n, o]
[a, r, b, s, d, e, g, h, i, n, o]'s child collection is [a, r, s, d, e, g, h, i, n, o]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值