How to use `Collections.binarySearch()` to do a binary search though an ArrayList of objects?
up vote
1
down vote
favorite
I have tried all the answers from the related questions, like following:
Implement binary search using the `Collections.binarySearch` signature
Can't use binary search with Object Arraylist?
But none of them have worked for me.
The thing is that I want to do binarySearch()
to find an object with a particular attribute in an ArrayList
.
I am using the following code for this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SearchingThread extends Thread {
private String search;
private ArrayList<Vehicle> vehicles;
public SearchingThread(String search, ArrayList<Vehicle> vehicles) {
this.search = search;
this.vehicles = vehicles;
}
public void run() {
Comparator<Vehicle> comp = new Comparator<Vehicle>() {
@Override
public int compare(Vehicle o1, Vehicle o2) {
return o1.getModel().compareTo(o2.getModel());
}
};
int index = Collections.binarySearch(vehicles, search, comp);
}
}
Here search
is the variable with the model
that I want to search in the ArrayList vehicles
.
I am getting the following error for this:
The method binarySearch(List, T, Comparator) in the type Collections is not applicable for the arguments (ArrayList, String, Comparator)
I am not able to use it, can anyone help me understand the cause and solution to the error.
Edit:
Sorry for not posting this before, sorting is not an issue. I have sorted the array list accordingly beforehand.
java comparator binary-search
add a comment |
up vote
1
down vote
favorite
I have tried all the answers from the related questions, like following:
Implement binary search using the `Collections.binarySearch` signature
Can't use binary search with Object Arraylist?
But none of them have worked for me.
The thing is that I want to do binarySearch()
to find an object with a particular attribute in an ArrayList
.
I am using the following code for this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SearchingThread extends Thread {
private String search;
private ArrayList<Vehicle> vehicles;
public SearchingThread(String search, ArrayList<Vehicle> vehicles) {
this.search = search;
this.vehicles = vehicles;
}
public void run() {
Comparator<Vehicle> comp = new Comparator<Vehicle>() {
@Override
public int compare(Vehicle o1, Vehicle o2) {
return o1.getModel().compareTo(o2.getModel());
}
};
int index = Collections.binarySearch(vehicles, search, comp);
}
}
Here search
is the variable with the model
that I want to search in the ArrayList vehicles
.
I am getting the following error for this:
The method binarySearch(List, T, Comparator) in the type Collections is not applicable for the arguments (ArrayList, String, Comparator)
I am not able to use it, can anyone help me understand the cause and solution to the error.
Edit:
Sorry for not posting this before, sorting is not an issue. I have sorted the array list accordingly beforehand.
java comparator binary-search
Just for the record: you understand that a prerequisite for binary search is that your array/list is already sorted? Is that the case here?
– GhostCat
Nov 10 at 10:40
1
"I was too lazy" you are supremely lazy if you can't be bothered to do s/eihcle/ehicle/g.
– Andy Turner
Nov 10 at 10:43
@AndyTurner yes :)
– Manhar
Nov 10 at 10:50
@GhostCat apologies, see the edited question.
– Manhar
Nov 10 at 10:50
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have tried all the answers from the related questions, like following:
Implement binary search using the `Collections.binarySearch` signature
Can't use binary search with Object Arraylist?
But none of them have worked for me.
The thing is that I want to do binarySearch()
to find an object with a particular attribute in an ArrayList
.
I am using the following code for this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SearchingThread extends Thread {
private String search;
private ArrayList<Vehicle> vehicles;
public SearchingThread(String search, ArrayList<Vehicle> vehicles) {
this.search = search;
this.vehicles = vehicles;
}
public void run() {
Comparator<Vehicle> comp = new Comparator<Vehicle>() {
@Override
public int compare(Vehicle o1, Vehicle o2) {
return o1.getModel().compareTo(o2.getModel());
}
};
int index = Collections.binarySearch(vehicles, search, comp);
}
}
Here search
is the variable with the model
that I want to search in the ArrayList vehicles
.
I am getting the following error for this:
The method binarySearch(List, T, Comparator) in the type Collections is not applicable for the arguments (ArrayList, String, Comparator)
I am not able to use it, can anyone help me understand the cause and solution to the error.
Edit:
Sorry for not posting this before, sorting is not an issue. I have sorted the array list accordingly beforehand.
java comparator binary-search
I have tried all the answers from the related questions, like following:
Implement binary search using the `Collections.binarySearch` signature
Can't use binary search with Object Arraylist?
But none of them have worked for me.
The thing is that I want to do binarySearch()
to find an object with a particular attribute in an ArrayList
.
I am using the following code for this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SearchingThread extends Thread {
private String search;
private ArrayList<Vehicle> vehicles;
public SearchingThread(String search, ArrayList<Vehicle> vehicles) {
this.search = search;
this.vehicles = vehicles;
}
public void run() {
Comparator<Vehicle> comp = new Comparator<Vehicle>() {
@Override
public int compare(Vehicle o1, Vehicle o2) {
return o1.getModel().compareTo(o2.getModel());
}
};
int index = Collections.binarySearch(vehicles, search, comp);
}
}
Here search
is the variable with the model
that I want to search in the ArrayList vehicles
.
I am getting the following error for this:
The method binarySearch(List, T, Comparator) in the type Collections is not applicable for the arguments (ArrayList, String, Comparator)
I am not able to use it, can anyone help me understand the cause and solution to the error.
Edit:
Sorry for not posting this before, sorting is not an issue. I have sorted the array list accordingly beforehand.
java comparator binary-search
java comparator binary-search
edited Nov 10 at 10:52
Mureinik
177k22128197
177k22128197
asked Nov 10 at 10:28
Manhar
69113
69113
Just for the record: you understand that a prerequisite for binary search is that your array/list is already sorted? Is that the case here?
– GhostCat
Nov 10 at 10:40
1
"I was too lazy" you are supremely lazy if you can't be bothered to do s/eihcle/ehicle/g.
– Andy Turner
Nov 10 at 10:43
@AndyTurner yes :)
– Manhar
Nov 10 at 10:50
@GhostCat apologies, see the edited question.
– Manhar
Nov 10 at 10:50
add a comment |
Just for the record: you understand that a prerequisite for binary search is that your array/list is already sorted? Is that the case here?
– GhostCat
Nov 10 at 10:40
1
"I was too lazy" you are supremely lazy if you can't be bothered to do s/eihcle/ehicle/g.
– Andy Turner
Nov 10 at 10:43
@AndyTurner yes :)
– Manhar
Nov 10 at 10:50
@GhostCat apologies, see the edited question.
– Manhar
Nov 10 at 10:50
Just for the record: you understand that a prerequisite for binary search is that your array/list is already sorted? Is that the case here?
– GhostCat
Nov 10 at 10:40
Just for the record: you understand that a prerequisite for binary search is that your array/list is already sorted? Is that the case here?
– GhostCat
Nov 10 at 10:40
1
1
"I was too lazy" you are supremely lazy if you can't be bothered to do s/eihcle/ehicle/g.
– Andy Turner
Nov 10 at 10:43
"I was too lazy" you are supremely lazy if you can't be bothered to do s/eihcle/ehicle/g.
– Andy Turner
Nov 10 at 10:43
@AndyTurner yes :)
– Manhar
Nov 10 at 10:50
@AndyTurner yes :)
– Manhar
Nov 10 at 10:50
@GhostCat apologies, see the edited question.
– Manhar
Nov 10 at 10:50
@GhostCat apologies, see the edited question.
– Manhar
Nov 10 at 10:50
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Collections#binarySearch
searches a List
for the same type of value that the list holds. Here, you're trying to search a list of vehicles with a string, and thus getting the error your shared.
One approach is to create a fake vehicle just so its model can be search:
Vehicle modelDummy = new Vehicle();
modelDummy.setModel(search);
int index = Collections.binarySearch(vehicles, modelDummy, comp);
Note that in order to use binarySearch
like that the list must be sorted according to the Comparator
you provides (i.e., sorted according to the model in this case). If this assumption is not true, you's have to use an O(n) search. E.g.:
Vehicle vehicle = vehicles.stream().filter(v -> v.getModel().eqauls(search)).findFirst();
add a comment |
up vote
0
down vote
Create a view of the list:
List<String> view = new AbstractList<String>() {
@Override public int size() {
return vehicles.size();
}
@Override public String get(int i) {
return vehicles.get(i).getModel();
}
};
Then apply binary search to the view.
int index = Collections.binarySearch(view, search);
Note that whilst it would work using an anonymous class, like this, it would be better to define a named class (e.g. a nested class, or even a local class), in order that it can both extend AbstractList
and implement RandomAccess
, so that the binary search knows it can access efficiently by index.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238038%2fhow-to-use-collections-binarysearch-to-do-a-binary-search-though-an-arraylis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Collections#binarySearch
searches a List
for the same type of value that the list holds. Here, you're trying to search a list of vehicles with a string, and thus getting the error your shared.
One approach is to create a fake vehicle just so its model can be search:
Vehicle modelDummy = new Vehicle();
modelDummy.setModel(search);
int index = Collections.binarySearch(vehicles, modelDummy, comp);
Note that in order to use binarySearch
like that the list must be sorted according to the Comparator
you provides (i.e., sorted according to the model in this case). If this assumption is not true, you's have to use an O(n) search. E.g.:
Vehicle vehicle = vehicles.stream().filter(v -> v.getModel().eqauls(search)).findFirst();
add a comment |
up vote
1
down vote
accepted
Collections#binarySearch
searches a List
for the same type of value that the list holds. Here, you're trying to search a list of vehicles with a string, and thus getting the error your shared.
One approach is to create a fake vehicle just so its model can be search:
Vehicle modelDummy = new Vehicle();
modelDummy.setModel(search);
int index = Collections.binarySearch(vehicles, modelDummy, comp);
Note that in order to use binarySearch
like that the list must be sorted according to the Comparator
you provides (i.e., sorted according to the model in this case). If this assumption is not true, you's have to use an O(n) search. E.g.:
Vehicle vehicle = vehicles.stream().filter(v -> v.getModel().eqauls(search)).findFirst();
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Collections#binarySearch
searches a List
for the same type of value that the list holds. Here, you're trying to search a list of vehicles with a string, and thus getting the error your shared.
One approach is to create a fake vehicle just so its model can be search:
Vehicle modelDummy = new Vehicle();
modelDummy.setModel(search);
int index = Collections.binarySearch(vehicles, modelDummy, comp);
Note that in order to use binarySearch
like that the list must be sorted according to the Comparator
you provides (i.e., sorted according to the model in this case). If this assumption is not true, you's have to use an O(n) search. E.g.:
Vehicle vehicle = vehicles.stream().filter(v -> v.getModel().eqauls(search)).findFirst();
Collections#binarySearch
searches a List
for the same type of value that the list holds. Here, you're trying to search a list of vehicles with a string, and thus getting the error your shared.
One approach is to create a fake vehicle just so its model can be search:
Vehicle modelDummy = new Vehicle();
modelDummy.setModel(search);
int index = Collections.binarySearch(vehicles, modelDummy, comp);
Note that in order to use binarySearch
like that the list must be sorted according to the Comparator
you provides (i.e., sorted according to the model in this case). If this assumption is not true, you's have to use an O(n) search. E.g.:
Vehicle vehicle = vehicles.stream().filter(v -> v.getModel().eqauls(search)).findFirst();
answered Nov 10 at 10:39
Mureinik
177k22128197
177k22128197
add a comment |
add a comment |
up vote
0
down vote
Create a view of the list:
List<String> view = new AbstractList<String>() {
@Override public int size() {
return vehicles.size();
}
@Override public String get(int i) {
return vehicles.get(i).getModel();
}
};
Then apply binary search to the view.
int index = Collections.binarySearch(view, search);
Note that whilst it would work using an anonymous class, like this, it would be better to define a named class (e.g. a nested class, or even a local class), in order that it can both extend AbstractList
and implement RandomAccess
, so that the binary search knows it can access efficiently by index.
add a comment |
up vote
0
down vote
Create a view of the list:
List<String> view = new AbstractList<String>() {
@Override public int size() {
return vehicles.size();
}
@Override public String get(int i) {
return vehicles.get(i).getModel();
}
};
Then apply binary search to the view.
int index = Collections.binarySearch(view, search);
Note that whilst it would work using an anonymous class, like this, it would be better to define a named class (e.g. a nested class, or even a local class), in order that it can both extend AbstractList
and implement RandomAccess
, so that the binary search knows it can access efficiently by index.
add a comment |
up vote
0
down vote
up vote
0
down vote
Create a view of the list:
List<String> view = new AbstractList<String>() {
@Override public int size() {
return vehicles.size();
}
@Override public String get(int i) {
return vehicles.get(i).getModel();
}
};
Then apply binary search to the view.
int index = Collections.binarySearch(view, search);
Note that whilst it would work using an anonymous class, like this, it would be better to define a named class (e.g. a nested class, or even a local class), in order that it can both extend AbstractList
and implement RandomAccess
, so that the binary search knows it can access efficiently by index.
Create a view of the list:
List<String> view = new AbstractList<String>() {
@Override public int size() {
return vehicles.size();
}
@Override public String get(int i) {
return vehicles.get(i).getModel();
}
};
Then apply binary search to the view.
int index = Collections.binarySearch(view, search);
Note that whilst it would work using an anonymous class, like this, it would be better to define a named class (e.g. a nested class, or even a local class), in order that it can both extend AbstractList
and implement RandomAccess
, so that the binary search knows it can access efficiently by index.
answered Nov 10 at 10:53
Andy Turner
79.4k878132
79.4k878132
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238038%2fhow-to-use-collections-binarysearch-to-do-a-binary-search-though-an-arraylis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just for the record: you understand that a prerequisite for binary search is that your array/list is already sorted? Is that the case here?
– GhostCat
Nov 10 at 10:40
1
"I was too lazy" you are supremely lazy if you can't be bothered to do s/eihcle/ehicle/g.
– Andy Turner
Nov 10 at 10:43
@AndyTurner yes :)
– Manhar
Nov 10 at 10:50
@GhostCat apologies, see the edited question.
– Manhar
Nov 10 at 10:50