このプログラムは、あなたが望む変換を行うと思います:
// The input
String[][] a = {
{"a", "b", "c", "d"},
{"c", "d"},
{"b", "c"}
};
System.out.println("Input: "+ Arrays.deepToString(a));
// Convert the input to a Set of Sets (so that we can hangle it more easily
Set<Set<String>> input = new HashSet<Set<String>>();
for (String[] s : a) {
input.add(new HashSet<String>(Arrays.asList(s)));
}
// The map is used for counting how many times each element appears
Map<String, Integer> count = new HashMap<String, Integer>();
for (Set<String> s : input) {
for (String i : s) {
if (!count.containsKey(i)) {
count.put(i, 1);
} else {
count.put(i, count.get(i) + 1);
}
}
}
//Create the output structure
Set<String> output[] = new HashSet[a.length + 1];
for (int i = 1; i < output.length; i++) {
output[i] = new HashSet<String>();
}
// Fill the output structure according the map
for (String key : count.keySet()) {
output[count.get(key)].add(key);
}
// And print the output
for (int i = output.length - 1; i > 0; i--) {
System.out.println("Set_" + i + " = " + Arrays.toString(output[i].toArray()));
}
出力:
Input: [[a, b, c, d], [c, d], [b, c]]
Set_3 = [c]
Set_2 = [d, b]
Set_1 = [a]