在 ArrayList 中搜索对象
我将对象存储在 ArrayList 中,其中我的 pojo 为 as
public class POJOSortableContacts {
private Long id;
private String displayName;
public POJOSortableContacts(Long id, String displayName) {
super();
this.id = id;
this.displayName = displayName;
}
//Setter and Getters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
//This will be used to sectioned header.
public String getLabel() {
return Character.toString(displayName.charAt(0)).toUpperCase();
}
//Sortable categories
//Sort by Contact name
public static Comparator<POJOSortableContacts> COMPARE_BY_NAME = new Comparator<POJOSortableContacts>() {
public int compare(POJOSortableContacts one, POJOSortableContacts other) {
return one.getDisplayName().compareToIgnoreCase(other.getDisplayName());
//return s1.toLowerCase().compareTo(s2.toLowerCase()); //it returns lower_case word first and then upper_case
}
};
//Sort by id
public static Comparator<POJOSortableContacts> COMPARE_BY_ID = new Comparator<POJOSortableContacts>() {
public int compare(POJOSortableContacts one, POJOSortableContacts other) {
return one.id.compareTo(other.id);
}
};
}
,Arraylist 结构为 as
ArrayList<POJOSortableContacts> contactArrayList = new ArrayList<POJOSortableContacts>()
,我想通过 id 从 contactArrayList 搜索一个对象(例如我想要一个 id 为 20 的对象),我想为此使用二进制搜索。那么会怎样呢?
I am storing objects in ArrayList, where my pojo is as
public class POJOSortableContacts {
private Long id;
private String displayName;
public POJOSortableContacts(Long id, String displayName) {
super();
this.id = id;
this.displayName = displayName;
}
//Setter and Getters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
//This will be used to sectioned header.
public String getLabel() {
return Character.toString(displayName.charAt(0)).toUpperCase();
}
//Sortable categories
//Sort by Contact name
public static Comparator<POJOSortableContacts> COMPARE_BY_NAME = new Comparator<POJOSortableContacts>() {
public int compare(POJOSortableContacts one, POJOSortableContacts other) {
return one.getDisplayName().compareToIgnoreCase(other.getDisplayName());
//return s1.toLowerCase().compareTo(s2.toLowerCase()); //it returns lower_case word first and then upper_case
}
};
//Sort by id
public static Comparator<POJOSortableContacts> COMPARE_BY_ID = new Comparator<POJOSortableContacts>() {
public int compare(POJOSortableContacts one, POJOSortableContacts other) {
return one.id.compareTo(other.id);
}
};
}
and Arraylist structure is as
ArrayList<POJOSortableContacts> contactArrayList = new ArrayList<POJOSortableContacts>()
, I want to search an object from contactArrayList by id (for example I want an object which id is 20), I want to use binarysearch for this. So how can it will be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
新的
POJOSortableContacts
只是一个虚拟对象来充当密钥。当然,只有当您的列表开始时按 ID 排序时,这才有效 - 您不能在未排序的列表(或以不同方式排序的列表)上使用二分搜索。
You can use
The new
POJOSortableContacts
is just a dummy object to act as the key.Of course, this will only work if your list is sorted by ID to start with - you can't use a binary search on an unsorted list (or on a list which is sorted in a different way).
我宁愿建议你使用 HashMap。
像这样填写你的 contactMap:
然后搜索就变得微不足道了:
I will rather suggest that you use a HashMap.
Fill up your contactMap like this:
Searching then becomes trivial:
为了能够使用二分搜索,您的集合必须进行排序。您可以每次在搜索之前对
ArrayList
进行排序,但这会抵消使用二分搜索的优势(您可以只对未排序的列表进行线性搜索,但仍然可以快点)。To be able to use binary search, your collection must be sorted. You could sort your
ArrayList
each time before your search, but that would negate the advantage of using binary search (you could just do a linear search over the unsorted list and still be faster).ArrayList 有一个方法 - BinarySearch,它以要搜索的对象作为参数。
希望这有帮助。
ArrayList has a method - BinarySearch, which takes object to search as a parameter.
Hope this helps.
稍微回避一下这个问题,如果列表中不能有重复项,那么使用
SortedSet
来存储联系人可能会更好。使用binarySearch
之前不再进行排序...Sidestepping the question a bit, if you can't have duplicates in the list you'd likely be better served by using a
SortedSet
to store the contacts. No sorting before usingbinarySearch
anymore...