如何使用Comparator接口 [英] How to use the Comparator interface

查看:163
本文介绍了如何使用Comparator接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手,我并没有真正了解如何使用比较器接口。
我在库存 ArrayList Item s $ c> class和 Item 类。
Item 类中我写道:

I'm new to java, and I'm not really getting how to use the comparator interface. I have an ArrayList of Items in an Inventory class and an Item class. In the Item class I wrote:

public class Item implements Comparator<Item> {
    //stuff
    ...
    @Override
    public int compare(Item a, Item b) {
        if (a.getID().compareToIgnoreCase(b.getID())>0)
            return 1;
        else if (a.getID().compareToIgnoreCase(b.getID())<0)
            return -1;
        else
            return 0;
    }
}

getID()方法只提供id,我必须使用按字母顺序排列项目。
我不确定这是不对的,它让我把 @Override 注释,我不知道为什么。我还写了一个界面,只是说:

The getID() method just gives the id, which I have to use to alphabetize the items. I'm not sure if this is right, it made me put the @Override annotation, I'm not sure why. Also I wrote an interface that just says:

 public interface Comparator<Item>
{
    int compare(Item a, Item b);
}

我不确定这一点。另外,我如何实现此方法来对库存类中创建的arraylist进行排序?

I'm not sure about that bit. Also how do I implement this method to sort the arraylist created in the inventory class?

谢谢,如果我的问题没有意义或需要澄清,请告诉我。

Thanks, if my question doesn't make sense or needs clarification just let me know.

推荐答案

使用比较器接口,你必须实现它并将其作为匿名类传递给 Collections.sort(List list,Comparator c) 作为第二个参数。

To use the Comparator interface you have to implement it and pass it as an anonymous class to Collections.sort(List list, Comparator c) as the second parameter.

如果您只想将列表传递给 Collections.sort(列表列表)然后你的 Item class必须使用实现可比较界面。

If you want to pass only the list to Collections.sort(List list) then your Item class has to the implement Comparable interface.

所以在这两种情况下 Collections.sort 方法知道如何订购列表中的元素

So in both cases the Collections.sort methods know how to order the elements in your list

这里有一些示例代码:

项目类实施可比较 +库存持有物品清单

Item class implementing Comparable + Inventory holding a list of items

public class Item implements Comparable<Item> {

    String id = null;

    public Item(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return id;
    }

    @Override
    public int compareTo(Item o) {
        return - id.compareToIgnoreCase(o.id);
    }
}


public class Inventory {

    List<Item> items = new ArrayList<>();

    public void addItem(Item item) {
        items.add(item);
    }

    public static void main(String[] args) {
        Inventory inventory = new Inventory();
        inventory.addItem(new Item("2"));
        inventory.addItem(new Item("4"));
        inventory.addItem(new Item("1"));
        inventory.addItem(new Item("7"));

        Collections.sort(inventory.items, new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return o1.id.compareToIgnoreCase(o2.id);
            }
        });
        System.out.println(inventory.items);

        Collections.sort(inventory.items);
        System.out.println(inventory.items);

    }
}

输出

[1, 2, 4, 7] // ascending
[7, 4, 2, 1] // descending since the compareTo method inverts the sign of the comparison result.

这篇关于如何使用Comparator接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆