题目
给定一个字符串的集合,格式如:{aaa bbb ccc}, {bbb ddd},
{eee fff},{ggg},{ddd hhh},将其中交集不为空的集合合并,要
求合并完成后的集合之间无交集。例如上例应输出{aaa bbb ccc
ddd hhh}, {eee fff}, {ggg}
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class Demo17 { public Demo17 (){} public static void main(String[] args) { Map<Integer,Integer> map = new HashMap<Integer,Integer>();//用于统计重复的 set在list中的 角标 List<HashSet<String>> al = new ArrayList<HashSet<String>>();//用于存放set List<HashSet<String>> al2 = new ArrayList<HashSet<String>>();//用于存放set中含有交集的 set List<HashSet<String>> al3 = new ArrayList<HashSet<String>>();//用于存放将有交集的合并后 存放在此set中 HashSet<String> list_last = new HashSet<String>();//将有交集的 存放在此set中 Set<String> s1 = new HashSet<String>(); s1.add("aaa"); s1.add("bbb"); s1.add("ccc"); Set<String> s2 = new HashSet<String>(); s2.add("bbb"); s2.add("ddd"); Set<String> s3 = new HashSet<String>(); s3.add("eee"); s3.add("fff"); Set<String> s4= new HashSet<String>(); s4.add("ggg"); Set<String> s5 = new HashSet<String>(); s5.add("ddd"); s5.add("hhh"); al.add((HashSet<String>) s1); al.add((HashSet<String>) s2); al.add((HashSet<String>) s3); al.add((HashSet<String>) s4); al.add((HashSet<String>) s5); map = getResult(al); merge(map, al, al2, al3, list_last); al.removeAll(al2); al.addAll(0,al3); printList(al); } private static List<HashSet<String>> merge(Map<Integer,Integer> map,List<HashSet<String>> al,List<HashSet<String>> al2,List<HashSet<String>> al3,HashSet<String> list_last) { Set<Integer> keyset = map.keySet(); int key1 = -1; int key2 = 0; Iterator<Integer> it = keyset.iterator(); while(it.hasNext()) { key1=key2; key2 = it.next(); int value = map.get(key2); if(!list_last.isEmpty()) { list_last = new HashSet<String>(); } if(key1!=-1&&key2==map.get(key1)) { list_last.addAll(al.get(key2)); list_last.addAll(al.get(value)); al2.add(al.get(key2)); al2.add(al.get(value)); if(al3.size()-1>=0) { al3.get(al3.size()-1).addAll(list_last); } else { al3.add(list_last); } }else { list_last.addAll(al.get(key2)); list_last.addAll(al.get(value)); al2.add(al.get(key2)); al2.add(al.get(value)); al3.add(list_last); } } return al; } private static Map<Integer,Integer> getResult( List<HashSet<String>> al) { Map<Integer,Integer> map = new HashMap<Integer,Integer>(); String str=null; // HashSet<String>[] s = (HashSet<String> [])al.toArray(); System.out.println(al.size()); HashSet<String>[] s = new HashSet[al.size()] ; al.toArray(s); for (int i=0;i<s.length-1;i++) { for(int j=i+1;j<s.length;j++) { Iterator<String > it2 = s[j].iterator(); while(it2.hasNext()) { str= it2.next(); if(s[i].contains(str)) { map.put(i,j); System.out.println(map); break; } } } } return map; } private static void printList(List<HashSet<String>> al) { Iterator <HashSet<String>> it = al.iterator(); while(it.hasNext()) { HashSet<String> s = it.next(); String [] sarr =new String [s.size()]; s.toArray(sarr); System.out.print("{"); for (int i = 0; i < sarr.length; i++) { if(i!=sarr.length-1) System.out.print(sarr[i]+","); else System.out.print(sarr[i]); } System.out.print("}"); } } }