如果为Equal,则使用值和具有时间优先级的LinkedHashMap进行排序

时间:2014-07-16 15:42:12

标签: java sorting hashmap treemap linkedhashmap

我知道这个主题可以很好地涵盖,但是它们只使用只使用一个值的Treemap,如果值相同则不会返回我想要的值。

这是我的目标:

public classe myObjectToMap
{
    long id;
    double price;
    String name;
    long date;

    public static void GetMyObject(long id, double price, String name)
    {
        myObjectToMap newObject = new myObjectToMap;
        newObject.id = id;
        newObject.price = price;
        newObject.name = name;
        newObject.date = new Date().getTime();
        PlaceOfMyMap.myMap.put(id, newObject);

    }

这是我的地图所在的位置,我放了一个LinkedHashMap,我没有哪个是Hashmap,TreeMap和LinkedHashMap之间最好的,我确实看到TreeMap给出了Value的比较器,但我没有到达比较不止一个值。

public class PlaceOfMyMap
{
    public static LinkedHashmap<Long, myObjectToMap> myMap = new LinkedHashmap<~>;
}

最后,这是我的主要计划:

public class MainClass
{
    public static void main(String args[]) throws Exception
    {
        MyObjectToHashmap.GetMyObject(1, 26, "Mat")
        MyObjectToHashmap.GetMyObject(4, 25, "Tommy")
        MyObjectToHashmap.GetMyObject(16, 24, "Kate")
        MyObjectToHashmap.GetMyObject(63, 26, "Mary")
        MyObjectToHashmap.GetMyObject(99, 24, "Ronny")

 }

}

首先:我想将它们从最高价格到最低价格进行排序,具有时间优先权,这意味着我希望Mat成为第一个,Kate成为第二个。

第二:我想将它们从最低价格到最高价格进行排序,时间优先,这意味着我希望Kate成为第一个,Ronny成为第二个。

有关正确排序的提示吗?

1 个答案:

答案 0 :(得分:1)

尝试以下代码。您可以反转compare()方法以获得反向排序

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

class MyObject
{
    private long id;
    private double price;
    private String name;
    private long date;

    public MyObject(long id, double price, String name) {
    this.id = id;
    this.price = price;
    this.name = name;
    this.date = new Date().getTime();
    }

    @Override
    public String toString() {
        return "MyObject [id=" + id + ", price=" + price + ", name=" + name
                + "]";
    }

    public double getPrice() {
        return price;
    }
    public long getDate() {
        return date;
    }
}

public class MapSort {

    public static void main(String[] args) {
        Map<Long, MyObject> myMap = new LinkedHashMap<Long, MyObject>();

        myMap.put(1L, new MyObject(1, 26, "Mat"));
        myMap.put(4L, new MyObject(4, 25, "Tommy"));
        myMap.put(16L, new MyObject(16, 24, "Kate"));
        myMap.put(63L, new MyObject(63, 26, "Mary"));
        myMap.put(99L, new MyObject(99, 24, "Ronny"));

        System.out.println("Before Sorting");
        System.out.println(myMap);
        System.out.println("\nAfter Sorting");
        System.out.println(sortMap(myMap));
    }

    private static Map<Long, MyObject> sortMap(
            Map<Long, MyObject> unsortedMap) {

        List<Entry<Long, MyObject>> list = new LinkedList<Entry<Long, MyObject>>(unsortedMap.entrySet());

        Collections.sort(list,
                new Comparator<Entry<Long, MyObject>>() {

                    @Override
                    public int compare(Entry<Long, MyObject> o1, Entry<Long, MyObject> o2) {
                        int priceResult = Double.valueOf(o1.getValue().getPrice()).compareTo(Double.valueOf(o2.getValue().getPrice()));
                        if(priceResult != 0) return priceResult;
                        return Long.valueOf(o1.getValue().getDate()).compareTo(Long.valueOf(o2.getValue().getDate()));
                    }
                });

        Map<Long, MyObject> sortedMap = new LinkedHashMap<Long, MyObject>();
        for(Entry<Long, MyObject> item : list){
            sortedMap.put(item.getKey(), item.getValue());
        }
        return sortedMap;
    }
}

<强>输出

Before Sorting
{1=MyObject [id=1, price=26.0, name=Mat], 4=MyObject [id=4, price=25.0, name=Tommy], 16=MyObject [id=16, price=24.0, name=Kate], 63=MyObject [id=63, price=26.0, name=Mary], 99=MyObject [id=99, price=24.0, name=Ronny]}

After Sorting
{16=MyObject [id=16, price=24.0, name=Kate], 99=MyObject [id=99, price=24.0, name=Ronny], 4=MyObject [id=4, price=25.0, name=Tommy], 1=MyObject [id=1, price=26.0, name=Mat], 63=MyObject [id=63, price=26.0, name=Mary]}