Reputation: 99
I am trying to combine three arrays into one array. Only keeping the common elements. This is not a duplicate question. I am aware there are other examples online, but that is using int [] and I don't know how to do it with Comparable.
What I need help with:
How to add the single combined/updated array to the 2d array.
How to count the iterations of each time an element gets compared.
If I wanted how could I change the arrays I have now to a List? - I was thinking maybe that would be easier to add.
I am a newbie to programming and I would appreciate the help. I am trying to learn java just by reading books and searching online.
This is what I have so far.
public class Common{
Comparable [] col_1 = {1, 1, 2};
Comparable [] col_2 = {1, 1, 2,3};
Comparable [] col_3= {1, 1, 2,3,4,};
Comparable [][] collections = {col_1, col_2, col_3};
int comparisonCount = 0
public Comparable[] findCommon(Comparable [][] collections){
int i, j, k, x, y;
for(i = 0; i< col_1.length; i++){
for(j = 0; j < col_2.length; j++){
for(k = 0; k < col_3.length; k++){
comparisonCount++;
// This should be counting but is not...
if(col_1[i].compareTo(col_2[j]) == 0 && col_1[i].compareTo(col_3[k]) ==0){
//keep searching until last element & allow duplicates & add to collections or a temp[]
}
}
}
}
// Here I'm not sure how to add the elements to the collection
for (x = 0; x < collections.length; x++){
for(y = 0; y< collections[x].length; y++){
collections [x][y] = ?????? // not sure how to add results here
}
}
}
public void setComparisons(int count){
count = comparisonCount;
}
public int getComparisons(){
return comparisonCount;
}
public class Sorting {
public static void main(String[] args) {
Common m = new Common();
//I want to test it from here but I don't know how to initialize each array.
for(int x=0; x < m.collections.length; x++){
for(int y= 0; y< m.collections[x].length; y++){
System.out.println(m.collections[x][y]);
}
// what I should be getting is only (1, 1, 2) - the order is not important really. I just want to learn.
}
System.out.println(m.getComparisons());
}
}
Upvotes: 0
Views: 208
Reputation: 10575
To keep only common elements in the comparable sets, you can use a TreeSet, that compares elements using the passed comparator.
Also, using a custom comparator you can count how many times elements are compared to each other:
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
public static Comparable[] findCommon(Comparable[][] collections, Comparator comparator) {
TreeSet<Comparable> set = new TreeSet<Comparable>(comparator);
Collections.addAll(set, collections[0]);
for (int i = 1; i < collections.length; i++)
set.retainAll(Arrays.asList(collections[i]));
return set.toArray(new Comparable[set.size()]);
}
public static void main(String[] args) {
Comparable[] col_1 = {1, 1, 2};
Comparable[] col_2 = {1, 1, 2, 3};
Comparable[] col_3 = {1, 1, 2, 3, 4};
Comparable[][] collections = {col_1, col_2, col_3};
final int comparisonCount = 0;
CountingComparator comparator = new CountingComparator();
System.out.println(Arrays.toString(findCommon(collections, comparator)));
System.out.println(comparator.getComparisonCount());
}
private static class CountingComparator implements Comparator<Comparable> {
private int comparisonCount;
public int getComparisonCount() {
return comparisonCount;
}
@Override
public int compare(Comparable o1, Comparable o2) {
comparisonCount++;
return o1.compareTo(o2);
}
}
}
Upvotes: 1
Reputation: 1371
The answers (in order of simplicity):
col_1.length * col_2.length * col_3.length
).List<Comparable[]> ls
isn't unreasonable). All you'll have to do with the HashSet (assuming you make HashSet<Comparable> hs = new HashSet<>();
) is ls.add(hs.toArray())
. Frankly, I don't know why you'd do this since your method is supposed to return hs.toArray()
instead (in fact, does your code compile, seeing as you're not returning according to the code you've posted?).Upvotes: 0