简体   繁体   English

在java中为元组计数排序

[英]Counting sort in java for tuples

I am building a class that has a mapping of strings to integers. 我正在构建一个具有字符串到整数的映射的类。 So if I have 3 apples I would have a mapping of apples to 3. 所以,如果我有3个苹果,我会将苹果映射到3个。

I need to write a class that sorts the name of the objects by decreasing numbers. 我需要编写一个类,通过减少数字来排序对象的名称。

So if I have 所以,如果我有

(apples, 3) (oranges, 2) (bananas, 5) (苹果,3)(橙子,2)(香蕉,5)

I will get (bananas, 5), (apples, 3), (oranges 2) 我会得到(香蕉,5),(苹果,3),(橙子2)

I was wondering if there's already a class out there that would make my life easier or how I would implement this. 我想知道是否已经有一个课程可以让我的生活更轻松,或者我将如何实现这一点。

Thanks. 谢谢。

You should be able to put your objects (apples, 3) (oranges, 2) (bananas, 5) into a List and then call Collections.sort(yourlist). 您应该能够将对象(苹果,3)(橙子,2)(香蕉,5)放入List中,然后调用Collections.sort(yourlist)。 You'd then want to make sure the object you declared implements the Comparable interface. 然后,您需要确保声明的对象实现Comparable接口。

More information is available at http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html 有关更多信息,请访问http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Let's say you declared you object as 假设你宣称你反对为

public class FruitAndCount implements Comparable<FruitAndCount> {
    private final String name;
    private final Integer count;

    public FruitAndCount(String name, int count) {
        this.name = name;
        this.count = count;
    }

    public String name() { return name;  }
    public int count()   { return count; }

    public int compareTo(FruitAndCount o) {
        return this.count.compareTo(o.count);
    }
}

You should then be able to make the following call which will sort your list: 然后,您应该能够进行以下调用,对您的列表进行排序:

FruitAndCount fruitArray[] = {
    new FruitAndCount("Apples", 3),
    new FruitAndCount("Oranges", 2),
    new FruitAndCount("Bananas", 5)
};

List<FruitAndCount> fruit = Arrays.asList(fruitArray);
Collections.sort(fruit);

You should then have a sorted list of fruit. 然后你应该有一个排序的水果列表。

It's always nice to be able to make a class implement Comparable , but sometimes you can't, or it is undesirable (for instance, if you need to be able to compare the same type in different ways, based on different attributes). 能够使类实现Comparable总是很好,但有时你不能,或者它是不可取的(例如,如果你需要能够以不同的方式比较同一类型,基于不同的属性)。

In this case, it is advisable to use the overloaded Collections.sort() method, which takes a List<T> to sort and a Comparator<T> to determine how the objects should be sorted. 在这种情况下,建议使用重载的Collections.sort()方法,它接受List<T>进行排序,并使用Comparator<T>来确定对象的排序方式。 This is much cleaner than making new tuples out of your old tuples, and can be more flexible than implementing Comparable (which is also a valid solution). 这比从旧元组中创建新元组要清晰得多,并且比实现Comparable (这也是一个有效的解决方案)更灵活。

You really want to take a look at TreeMap . 你真的想看看TreeMap

Assuming the counts are unique, you simply reverse the tuples, storing the count as the key and the name of the fruit as the value. 假设计数是唯一的,您只需反转元组,将计数作为键存储,将水果的名称存储为值。 TreeMap then stores the items sorted in ascending order by the key value, and you can read the values back. 然后,TreeMap按键值存储按升序排序的项目,您可以返回值。 Since the sorting is done on the insertion the retrieval time is very low. 由于在插入时完成排序,因此检索时间非常短。

If you have non-unique counts there's an easy solution here that will let you take advantage of TreeMap. 如果您有非唯一计数,那么这里有一个简单的解决方案 ,可以让您利用TreeMap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM