How can I use Binary Search with an ArrayList?
Here are elements of the ArrayList:
public class DictionaryElements implements
Comparable<DictionaryElements>, Comparator<DictionaryElements>{
private String word;
private String translation;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
public DictionaryElements() {
}
public DictionaryElements(String word, String translation) {
this.word = word;
this.translation = translation;
}
@Override
public String toString() {
return word + " - " + translation;
}
@Override
public int compareTo(DictionaryElements dictionary) {
return this.word.compareTo(dictionary.word);
}
@Override
public int compare(DictionaryElements wordOne, DictionaryElements wordTwo) {
return wordOne.getWord().compareTo(wordTwo.getWord());
}
}
Than I sorted a list here:
public class DictionarySorter {
ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
public DictionarySorter(ArrayList<DictionaryElements> dictionaryList) {
this.dictionaryList = dictionaryList;
}
public ArrayList<DictionaryElements> getSortedByWord() {
Collections.sort(dictionaryList);
return dictionaryList;
}
}
And here I tried to imply Binary Search:
public class Main {
public static void main(String[] args) {
DictionaryElements dictionaryElements = new DictionaryElements();
ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
DictionarySorter dictionarySorter = new
DictionarySorter(dictionaryList);
boolean found = true;
dictionaryList();
Scanner scanner = new Scanner(System.in);
System.out.println("Write one word in English:");
String wordInEnglish = scanner.nextLine();
int index = Collections.binarySearch(dictionaryList, wordInEnglish);
if (found) {
//code here.
}
else {
System.out.println("Sorry, i didn't find " + wordInEnglish + " ;(");
}
scanner.close();
}
public static void dictionaryList() {
ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
dictionaryList.add(new DictionaryElements("Apple", "Apfel"));
dictionaryList.add(new DictionaryElements("Pear", "Birne"));
dictionaryList.add(new DictionaryElements("Orange", "Orange"));
DictionarySorter dictionarySorter = new DictionarySorter(dictionaryList);
ArrayList<DictionaryElements> sortedDictionaryList = dictionarySorter.getSortedByWord();
for (DictionaryElements dictionary : sortedDictionaryList) {
System.out.println(dictionary);
}
}
}
Error says: The method binarySearch(List<? extends Comparable<? super T>>, T) in the type Collections is not applicable for the arguments (ArrayList, String)
What did I missed and how can I fix this?
If you have a look at the method declaration for Collections.binarySearch the comparator implemented must be of the same type as the key being passed in.
Your DictionaryElements extends Comparable of type DictionaryElements type but the key you are passing in is of type String.
You'll need pass a DictionaryElement as the key instead:
DictionaryElements key = new DictionaryElements(wordInEnglish, translation);
int index = Collections.binarySearch(dictionaryList, key);