TreeMap 不适用于某些键

TreeMap doesn't work for some of keys

我今天在 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

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

您的 compareTo 方法已损坏。如果两个 NavLocation 对象,ab 在它们的页面或部分不同,a.compareTo(b)b.compareTo(a) 将 return 1 ,从而违反了方法的一般约定,可能会导致意想不到的结果。

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

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

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