TreeMap不适用于某些键

时间:2018-02-24 20:16:22

标签: java dictionary treemap

我今天在java中实现TreeMap来跟踪下一页的导航。 我在树形图中有5个条目,其中3个有效,2个不起作用。

NavigationHelper.java:

public class NavigationHelper {

private static String hhSection = HXConstants.CSR_SECTION_FAMILY_DETAILS;
private static String[] hhPages = {
        HXConstants.CSR_PAGE_ID_HOUSEHOLD_MEMBERS, 
            HXConstants.CSR_PAGE_ID_HOUSEHOLD_RELATIONSHIP, 
                HXConstants.CSR_PAGE_ID_HOUSEHOLD_ADDITIONAL_QUESTIONS,
                    HXConstants.CSR_PAGE_ID_HOUSEHOLD_SUMMARY_NEW,
                        HXConstants.CSR_PAGE_ID_HOUSEHOLD_PRIVACY_AGREEMENT
};
private static String hhPath = "household";
public static HashMap<String, NavLocation> getHHNavMap()
{

    HashMap<String, NavLocation> hhNavMap = new HashMap<String, NavLocation>();
    for (int i=0;i<hhPages.length;i++ ) {
        hhNavMap.put(hhPath+"/"+hhPages[i], new NavLocation(hhSection,hhPages[i]));
    }
    return hhNavMap;
}

public static Map<NavLocation,String> getHHBackNavMap() {
    TreeMap<NavLocation,String> hhBackNavMap = new TreeMap<NavLocation, String>();
    HashMap<String, NavLocation> hhNavMap = getHHNavMap();
    for(Entry<String, NavLocation> entry : hhNavMap.entrySet()) {
        hhBackNavMap.put(entry.getValue(), entry.getKey());
    }
    return hhBackNavMap;
}


public static class NavLocation implements Comparable<NavLocation>{
    private String section;
    public NavLocation(String s, String p) {
        this.section = s;
        this.page = p;
    }
    public String getSection() {
        return section;
    }

    public String getPage() {
        return page;
    }
    private String page;

    @Override
    public int compareTo(NavLocation navObj) {
        if(navObj.getPage().equals(this.page) && (navObj.getSection().equals(this.section)))
            return 0;
        return 1;
    }

}
}

AppAggNavigationHelper.java:

public class AppAggNavigationHelper extends RestServiceBaseTest {

private static String hhSection = HXConstants.CSR_SECTION_FAMILY_DETAILS;
private static String[] hhPages = {
        HXConstants.CSR_PAGE_ID_HOUSEHOLD_MEMBERS, 
            HXConstants.CSR_PAGE_ID_HOUSEHOLD_RELATIONSHIP, 
                HXConstants.CSR_PAGE_ID_HOUSEHOLD_ADDITIONAL_QUESTIONS,
                    HXConstants.CSR_PAGE_ID_HOUSEHOLD_SUMMARY_NEW,
                        HXConstants.CSR_PAGE_ID_HOUSEHOLD_PRIVACY_AGREEMENT
};

NavigationHelper navigationHelper = new NavigationHelper();
List<NavLocation> navList = new ArrayList<NavLocation>();

@Before
public void populateNavLocations() {
    for(int i = 0 ; i < hhPages.length ; i++) {
        navList.add(new NavLocation(hhSection, hhPages[i]));
    }
}

@Test
public void test() {
    testWithoutRest();
}



public void testWithoutRest() {
    TreeMap<NavLocation,String> map = (TreeMap<NavLocation, String>) navigationHelper.getHHBackNavMap();
    for(Map.Entry<NavLocation, String> entry : map.entrySet()) {
        NavLocation nav = entry.getKey();
        System.out.println(nav.getPage() + " " + nav.getSection());
        System.out.println(entry.getValue());

    }
    p("*****");
    for(NavLocation navLocation : navList) {
        System.out.println(navLocation.getPage() + " " + navLocation.getSection() + " " + map.get(navLocation));
    }
}
}

然后输出有线,对于成员,摘要,隐私它正在工作。 但对于关系和问题,它不是。 :

member familydetails household/member

relation familydetails null

question familydetails null

summary familydetails household/summary

privacy familydetails household/privacy

为什么关系和问题不起作用?

1 个答案:

答案 0 :(得分:3)

您的compareTo方法已损坏。如果两个NavLocation个对象,ab的网页或部分不同,则a.compareTo(b)b.compareTo(a)都会返回1,从而违反了该方法的一般合同,可能会导致意外结果。

相反,根据对象属性实现这种方法的经典方法可能如下所示:

@Override
public int compareTo(NavLocation other) {
    int result = getPage().compareTo(other.getPage());
    if (result != 0) {
        return result;
    }

    return getSection().compareTo(other.getSection());
}