EDIT : method signature
public Comparable[][] findCommonElements(Comparable[][] collections)
wrong. It should be
public Comparable[] findCommonElements(Comparable[][] collections)
but changing it in my IDE will ruin everything. I almost feel like I went beyond my knowledge, because I do not quite understand Sets, and the 2D array annoys me badly.
I need to write an algorithm that takes two Comparable arrays , iterates through linear time efficiency, and displays common elements. I read that using HashSet will give me the fastest time efficiency, but I am at a standstill. That's why:
We were given instructions and one line of code, which is the signature of the method
public Comparable[][] findCommonElements(Comparable[][] collections)
which means that I have to return a 2d array, "collections". I emailed my professor to use HashSets, and they gave me the go-ahead, except for this problem:
"You can use HashSets inside the findCommonElements method, but you will need to count the number of comparisons completed. Although hashing is usually very effective, some comparisons will be made in case of collisions. You must have access to the source code of the HashSet you use. You also need the getComparisons () method in to your CommonElements class to return the number of comparisons. "
For two semesters of programming, I have not studied HashSets, Maps, Tables, etc. I am trying to learn this myself, and I do not quite understand the conflicts.
My code takes two arrays and returns common elements, but my return statement is disgusting since I basically wrote it to compile (2d Comparable array is a parameter).
Am I on the right track with this? Here is the code:
public class CommonElements { static Comparable[] collection1 = {"A", "B", "C", "D", "E"}; //first array static Comparable[] collection2 = {"A", "B", "C", "D", "E", "F", "G"}; //second array static Comparable[][] collections = {collection1, collection2}; //array to store common elements. static Set<Comparable> commonStuff = new HashSet<>(); //instance of Set containing common elements public static void main(String[] args) { CommonElements commonElements = new CommonElements(); //create instance of class CommonElements commonElements.findCommonElements(collections); //call the find method } public Comparable[][] findCommonElements(Comparable[][] collections) { Set<Comparable> addSet = new HashSet<>(); //instance of Set to add elements to for (Comparable x : collection1) { //adding elements from first array to my addSet addSet.add(x); } for (Comparable x : collection2) { if (addSet.contains(x)) { commonStuff.add(x); //checking for common elements, add to commonStuff Set } } System.out.println(toString(commonStuff)); //print the toString method return collections; //return statement, otherwise Java will whine at me } public String toString(Set<Comparable> commonStuff) { //this method gets rid of the brackets String elements = commonStuff.toString(); //make a String and assign it to the Set elements = elements.replaceAll("\\[", "").replaceAll("\\]", ""); //replace both brackets with empty space return "Common Elements: " + elements; //return the Set as a new String } }