簡體   English   中英

如何以所需的方式對對象的ArrayList進行排序

[英]How do I sort an ArrayList of objects the way that I want

我想對ArrayList對象進行排序。 這是我的目標:

    public class PaintRegion extends JPanel {       
        public ArrayList<Region> m_region = new ArrayList<Region>(); 
        // some codes
}

及其類:

public class Region {


    private String m_region;
    private Integer m_totalCount;
    private double m_normalizedCount;


    public String getRegion() {
        return m_region;
    }

    public void setRegion(String region) {
        this.m_region=region;
    }


    public Integer getTotalCount() {
        return m_totalCount;
        }

    public void setTotalCount(Integer totalCount) {
       this.m_totalCount = totalCount;
        }

    public double getNormalizedCount() {
        return m_normalizedCount;
        }

    public void setNormalizedCount(double normalizedCount) {
       this.m_normalizedCount = normalizedCount;
        }



    public boolean Print_Region() {


              System.out.println("State::Print_Region(): " +
                 this.getRegion()+ ", "+
                 this.getTotalCount()+ ", "+
                 this.getNormalizedCount());

              return true;
           }

}

我有5個地區名稱,分別是{“ Southeast”,“ Southwest”,“ Northeast”,“ West”,“ Midwest”}

我想按“西方”,“中西部”,“東南”,“西南”,“東北”對這個對象進行分類

我該如何分類?

如果您使用一個自定義的Object而不是String,例如一個名為Region的類(屬性:名稱和排序索引),則會容易得多。 然后,您可以實現Comparable並使用Collections.sort()對其進行排序。 或使用諸如TreeSet之類的排序集合類型作為開始。

因為您想要一個非常不適合該Region類的其他用途的特定順序,所以我的建議是使用Comparator 以下是有關您的方案的外觀和使用方法的快速草稿。

Comparator<Region> compassComporator = (r1, r2) -> {
    //this assumes both objects (and their m_region field) are not null, else do a check for null

    if (r1.m_region.equals(r2.m_region)) {
        return 0;
    }
    if ("West".equals(r1.m_region)) {
        return 1;
    }
    if ("West".equals(r2.m_region)) {
        return -1;
    }
    if ("Midwest".equals(r1.m_region)) {
        return 1;
    }
    if ("Midwest".equals(r2.m_region)) {
        return -1;
    }
    if ("Southeast".equals(r1.m_region)) {
        return 1;
    }
    if ("Southeast".equals(r2.m_region)) {
        return -1;
    }
    if ("Southwest".equals(r1.m_region)) {
        return 1;
    }
    if ("Southwest".equals(r2.m_region)) {
        return -1;
    }
    if ("Northeast".equals(r1.m_region)) {
        return 1;
    }
//        if ("Northeast".equals(r2.m_region)) {
    return -1;
//        }

};

public class PaintRegion extends JPanel {       
    public ArrayList<Region> m_region = new ArrayList<Region>(); 
    // some codes
    m_region.sort(compassComporator);
}

您可以檢查Java的流功能,可以根據類的任何變量對對象列表進行排序

這是解決問題的非常聰明的方法。 無需編寫所有重要邏輯。 只需創建一個排序區域的數組列表。

現在,關於arraylist的妙處是對元素進行了索引並且對所有元素的索引也進行了排序(0、1、2、3等)。 現在,只需使用索引本身對Region進行排序即可。

希望清楚。

 List<Region> regions = new ArrayList<Region>(); 
 List<String> sortedRegionNames = new ArrayList<>();
 sortedRegionNames.addAll(Arrays.asList(new String[]{ "West","Midwest","Southeast","Southwest","Northeast"}));
 Comparator<Region> comp = new Comparator<Region>() {
    @Override
    public int compare(Region r1, Region r2) {

        return new Integer(sortedRegionNames.indexOf(r1.getRegion())).compareTo(sortedRegionNames.indexOf(r2.getRegion()));
    }
};
Collections.sort(regions, comp );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM