在 ArrayList 中搜索对象

发布于 2024-10-20 15:24:04 字数 1796 浏览 4 评论 0原文

我将对象存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等风也等你 2024-10-27 15:24:04

您可以使用

POJOSortableContacts contact = Collections.binarySearch(contactArrayList,
                                               new POJOSortableContacts(20, ""),
                                               COMPARE_BY_ID);

新的 POJOSortableContacts 只是一个虚拟对象来充当密钥。

当然,只有当您的列表开始时按 ID 排序时,这才有效 - 您不能在未排序的列表(或以不同方式排序的列表)上使用二分搜索。

You can use

POJOSortableContacts contact = Collections.binarySearch(contactArrayList,
                                               new POJOSortableContacts(20, ""),
                                               COMPARE_BY_ID);

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).

戈亓 2024-10-27 15:24:04

我宁愿建议你使用 HashMap。

Map<Long,POJOSortableContacts> contactMap = new HashMap<Long,POJOSortableContacts>();

像这样填写你的 contactMap:

contactMap.put(myContact.getId(), myContact);

然后搜索就变得微不足道了:

POJOSortableContacts myContact = contactMap.get(myID);

I will rather suggest that you use a HashMap.

Map<Long,POJOSortableContacts> contactMap = new HashMap<Long,POJOSortableContacts>();

Fill up your contactMap like this:

contactMap.put(myContact.getId(), myContact);

Searching then becomes trivial:

POJOSortableContacts myContact = contactMap.get(myID);
九局 2024-10-27 15:24:04

为了能够使用二分搜索,您的集合必须进行排序。您可以每次在搜索之前对 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).

涙—继续流 2024-10-27 15:24:04

ArrayList 有一个方法 - BinarySearch,它以要搜索的对象作为参数。

POJOSortableContacts contactToSearch = new POJOSortableContacts(someId, "Name");
POJOSortableContacts myContact = contactArrayList.BinarySearch(contactToSearch);    

希望这有帮助。

ArrayList has a method - BinarySearch, which takes object to search as a parameter.

POJOSortableContacts contactToSearch = new POJOSortableContacts(someId, "Name");
POJOSortableContacts myContact = contactArrayList.BinarySearch(contactToSearch);    

Hope this helps.

浮世清欢 2024-10-27 15:24:04

稍微回避一下这个问题,如果列表中不能有重复项,那么使用 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 using binarySearch anymore...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文