如何排序HashMap?

时间:2017-01-29 05:28:09

标签: java sorting hashmap

我的HashMap有一个String键和一个类对象,如下所示.....

HashMap<String, MyInterval> myMap = new HashMap();

这是下面的MyInterval类......

public class MyInterval {

        public enum Priority{FIRST, NONE, LAST};

        String name;
        double start;
        double end;
        boolean fixed;
        Priority myPriority;

        MyInterval(String theName, double theStart, double theEnd){ //MyInterval Constructor in order to
            name = theName;                                         //initialize all instance variables
            start = theStart;
            end = theEnd;
            fixed = false;
            myPriority = Priority.NONE;
        }

        public String getName(){ //Returning name
            return name;
        }

        public double getStart(){ //Returning start
            return start;
        }

        public double getEnd(){ //Returning end
            return end;
        }

    }

这是下面的HashMap的添加方法.....

public int add(String theName, MyInterval theMyInterval){ //Adds new elements to the HashMap
    if(!(correctOrder(theMyInterval))){
        return 1;
    }else if(!(isInsideTheMainInterval(theMyInterval))){
        return 2;
    }else if(isNameNull(theName) == 3){
        return 3;
    }else if(isSameName(theName) == 4){
        return 4;
    }else if(!(isOverlapping(theMyInterval))){
        return 5;
    }
    myMap.put(theName, theMyInterval);
    return 6;
}

我真的想找到一种方法来按照开始时间的升序对HashMap进行排序。非常感谢先进。

0 个答案:

没有答案