用JAVA将多个集合合并成没有交集的集合
给定一个字符串的集合,格式如:{aaabbbccc},{bbbddd},{eeefff},{ggg},{dddhhh}要求将其中交集不为空的集合合并,要求合并完成后的集合...
给定一个字符串的集合,格式如:{aaa bbb ccc},{bbb ddd},{eee fff},{ggg},{ddd hhh}要求将其中交集不为空的集合合并,要求合并完成后的集合之间无交集,例如上例应输出{aaa bbb ccc ddd hhh},{eee fff},{ggg}。
public class Test{
private static int[] elements;
private static Map<String,Integer> map;
//初始化
static void make_set(String[][] a){
int count = 0;
Map<String,Integer> map = new HashMap<String,Integer>();
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
if(map.get(a[i][j])==null){
map.put(a[i][j], count++);
}
}
}
Test.map = map;
elements = new int[count];
for(int i=0;i<count;i++){
elements[i] = i;
}
}
//将相邻元素所在的集合合并
static void union(String[][] a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length-1;j++){
int n1 = elements[map.get(a[i][j])];
int n2 = elements[map.get(a[i][j+1])];
for(int k=0;k<elements.length;k++){
if(elements[k]==n2){
elements[k] = n1;
}
}
}
}
}
//根据elements中的结果将元素合并并打印
static void print(){
HashMap<Integer,List<String>> results = new HashMap<Integer,List<String>>();
//迭代map找到字符串和对应的下标
for(Iterator<Entry<String,Integer>> it = Test.map.entrySet().iterator();it.hasNext();){
Entry<String,Integer> entry = it.next();
String s = entry.getKey();
int collectionNumber = elements[entry.getValue()]; //所属的集合
if(results.get(collectionNumber)==null){
results.put(collectionNumber, new ArrayList<String>());
}
List<String> list = results.get(collectionNumber);
list.add(s);
}
for(Iterator<List<String>> it=results.values().iterator();it.hasNext();){
List<String> list = it.next();
for(String s:list){
System.out.print(s+" ");
}
System.out.println();
}
}
public static void main(String[] args){
String[][] a = {
{"aaa", "bbb", "ccc"},
{"bbb","ddd"},
{"eee", "fff"},
{"ggg"},
{"ddd","hhh"}
};
make_set(a);
union(a);
print();
}
代码如上 求解释清楚 解题的思路 重要函数的注释 。 展开
public class Test{
private static int[] elements;
private static Map<String,Integer> map;
//初始化
static void make_set(String[][] a){
int count = 0;
Map<String,Integer> map = new HashMap<String,Integer>();
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
if(map.get(a[i][j])==null){
map.put(a[i][j], count++);
}
}
}
Test.map = map;
elements = new int[count];
for(int i=0;i<count;i++){
elements[i] = i;
}
}
//将相邻元素所在的集合合并
static void union(String[][] a){
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length-1;j++){
int n1 = elements[map.get(a[i][j])];
int n2 = elements[map.get(a[i][j+1])];
for(int k=0;k<elements.length;k++){
if(elements[k]==n2){
elements[k] = n1;
}
}
}
}
}
//根据elements中的结果将元素合并并打印
static void print(){
HashMap<Integer,List<String>> results = new HashMap<Integer,List<String>>();
//迭代map找到字符串和对应的下标
for(Iterator<Entry<String,Integer>> it = Test.map.entrySet().iterator();it.hasNext();){
Entry<String,Integer> entry = it.next();
String s = entry.getKey();
int collectionNumber = elements[entry.getValue()]; //所属的集合
if(results.get(collectionNumber)==null){
results.put(collectionNumber, new ArrayList<String>());
}
List<String> list = results.get(collectionNumber);
list.add(s);
}
for(Iterator<List<String>> it=results.values().iterator();it.hasNext();){
List<String> list = it.next();
for(String s:list){
System.out.print(s+" ");
}
System.out.println();
}
}
public static void main(String[] args){
String[][] a = {
{"aaa", "bbb", "ccc"},
{"bbb","ddd"},
{"eee", "fff"},
{"ggg"},
{"ddd","hhh"}
};
make_set(a);
union(a);
print();
}
代码如上 求解释清楚 解题的思路 重要函数的注释 。 展开
1个回答
展开全部
import java.util.HashSet;
import java.util.Set;
public class Sets {
public static <T> Set<T> union(Set<T> a, Set<T> b){
Set<T> set = new HashSet<T>(a);
set.addAll(b);
return set;
}
public static <T> Set<T> intersection(Set<T> a, Set<T> b){
Set<T> set = new HashSet<>();
set.retainAll(b);
return set;
}
public static <T> Set<T> difference(Set<T> a, Set<T> b){
Set<T> set = new HashSet<>(a);
set.removeAll(b);
return set;
}
public static <T> Set<T> complement(Set<T> a, Set<T> b){
Set<T> set = difference(union(a, b), intersection(a, b));
return set;
}
public static void main(String[] args) {
String[] strs1 = {"aaa","bbb","ccc"};
String[] strs2 = {"bbb","ddd"};
Set<String> set1 = new HashSet<>();
Set<String> set2 = new HashSet<>();
for (String string : strs1) {
set1.add(string);
}
for (String string : strs2) {
set2.add(string);
}
Set<String> set = Sets.complement(set1, set2);
System.out.println(set);
}
}
采纳吧
追问
能把思路讲出来么 我看不懂
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询