1个回答
展开全部
public static void main(String[] args) {
Set<Character> result = new HashSet<Character>();
Set<Character> set1 = new HashSet<Character>() {
{
add('a');
add('b');
add('c');
add('d');
add('e');
}
};
Set<Character> set2 = new HashSet<Character>() {
{
add('a');
add('b');
add('c');
}
};
result.clear();
result.addAll(set1);
result.retainAll(set2);
System.out.println("交集:" + result);
result.clear();
result.addAll(set1);
result.removeAll(set2);
System.out.println("差集:" + result);
result.clear();
result.addAll(set1);
result.addAll(set2);
System.out.println("并集:" + result);
result.clear();
result.addAll(set1);
if(result.containsAll(set2)){
result.removeAll(set2);
System.out.println("补集:"+result);
}else{
System.out.println("无补集");
}
}
Set<Character> result = new HashSet<Character>();
Set<Character> set1 = new HashSet<Character>() {
{
add('a');
add('b');
add('c');
add('d');
add('e');
}
};
Set<Character> set2 = new HashSet<Character>() {
{
add('a');
add('b');
add('c');
}
};
result.clear();
result.addAll(set1);
result.retainAll(set2);
System.out.println("交集:" + result);
result.clear();
result.addAll(set1);
result.removeAll(set2);
System.out.println("差集:" + result);
result.clear();
result.addAll(set1);
result.addAll(set2);
System.out.println("并集:" + result);
result.clear();
result.addAll(set1);
if(result.containsAll(set2)){
result.removeAll(set2);
System.out.println("补集:"+result);
}else{
System.out.println("无补集");
}
}
更多追问追答
追问
元素必须为小写字母😞
追答
是小写字母
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询