I have a arrayList with values {a,b,a,c,d,b,a}
I want to make a comparison of each element in the list and insert the pair of common indexes into a List of array or something using java
example output: [[0,2,6], [1,4]]
explanation: a is at indexes 0,2,6 and b is at indexes 1,4
So far I have this:
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
List<String> name = new ArrayList<String>();
letter.add("a");
letter.add("b");
letter.add("c");
letter.add("b");
letter.add("a");
for (int i = 0; i < letter.size(); i++) {
for (int j = 1; j < letter.size(); j++) {
if (letters.get(i).equals(letters.get(j)) && i != j) {
hashMap.put(i, j);
}
}
}
System.out.println(hashMap); //o/p: {0=4, 1=3, 3=1}
List<int[]> myList = new ArrayList<int[]>();
Iterator entries = hashMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
myList.add(new int[] {key,hashMap.get(key)});
}
System.out.println(myList.toString());
//O/P: [[I@380fb434, [I@668bc3d5, [I@3cda1055]
UPDATE:
the idea was to get [[0,4],[1,3],[3,1]] as elements in myList but I am not able to get that. Any help is much appreciated! Thanks!
Based on the above array of indexes, I want to compare the elements in a different List B and C at those indexes - meaning compare elements at indexes 0,2,6 in List B and C and check if all three elements are equal. Same for elements at index 1,4