java merge string_How do I merge two string sets in Java but only if they have common elements?

2017-11-25 20:55:26

0

I'm new to Java and I'm trying (and - apparently- failing) to combine two string sets if they have common elements:

CommonElements = Set1;

CommonElements.retainAll(Set2);

System.out.println("common elements of"+Set1+"and"+Set2+":"+CommonElements);

if (CommonElements.size()!=0) {

Set1.addAll(Set2);

System.out.println("both sets"+Set1);

}

Now, I am applying this same thing though I now have an ArrayList of sets:

for (i=0; i

for (int j = 1; j

Set Set1 = myList.get(i);

System.out.println("Set1"+Set1);

Set Set2 = myList.get(i+j);

System.out.println("Set2"+Set2);

Set commonElements = myList.get(i);

commonElements.retainAll(Set2);

System.out.println("Common elements of"+i+"and"+(i+j)+":"+Set3);

if (commonElements.size()!=0) {

Set1.addAll(Set2);

myList.set(i, Set1);

System.out.println("both sets"+Set1);

}

System.out.println(myList.get(i));

But System.out.println(myList.get(i)) results in:

if the two sets don't have anything in common:

Set1 becomes empty, myList.get(i) returns [].

if the two sets do have something in common:

Set1 becomes Set2, so List entry number i takes the value of List entry number i+j.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值