2012-05-05 50 views
4

我想知道是否有任何方式訂購不同類的枚舉。例如,如果我有一組固定的化學物質,它們以不同的方式與其他化學物質發生反應,一些化學物質強烈,一些物質弱。我基本上希望能夠根據小組應該反應的化學物質(即取決於班級)切換排列順序。我知道我應該使用Comparable,但我不知道如何去做。如果我不夠清楚,留下評論,我會進一步解釋。訂購枚舉值

謝謝。

public static enum Chem { 
    H2SO4, 2KNO3, H20, NaCl, NO2 
}; 

所以我有一些看起來像這樣的東西,我已經知道每種化學物質如何反應一些其他化學物質。我只是想根據化學反應來安排化學物質。這幾乎是我所有的。

+0

你是說你有,你要根據不同的標準進行排序化學成分的列表? –

+0

我不會說清單。它們是枚舉類型的。 – Ester

+0

絕對不夠清楚。你能展示一些代碼嗎? –

回答

10

實現不同Comparator的(見http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

Comparator comparator1 = new Comparator<MyEnum>() { 

    public int compare(MyEnum e1, MyEnum e2) { 
    //your magic happens here 
    if (...) 
     return -1; 
    else if (...) 
     return 1; 

    return 0; 
    } 
}; 

//and others for different ways of comparing them 

//Then use one of them: 
MyEnum[] allChemicals = MyEnum.values(); 
Arrays.sort(allChemicals, comparator1); //this is how you sort them according to the sort critiera in comparator1. 
1

假設你有元素的枚舉:

enum Elements {Oxygen, Hydrogen, Gold} 

,你想他們在一個給定的順序進行排序,然後我就可以做:

Elements[] myElements = Elements.values(); 
Arrays.sort(myElements, new ElementComparator()); 

其中ElementComparator可以是例如:

public class ElementComparator implements java.util.Comparator<Elements> { 
    public int compare(Elements left, Elements right){ 
     return right.compareTo(left); //use your criteria here 
    } 
} 

排序標準的性質在您的問題中不清楚。這似乎是關於化學反應的事情。我猜想標準應該在Comparator中去決定哪個枚舉元素比給定化學反應的元素大。

0

你的意思是代表你的化學物質的實際物體是枚舉?如果我可以這麼說,那麼這就像一個奇怪的實現:枚舉實際上更適合於某些東西的「屬性」。

但無論如何......如果你真的想把它們表示爲枚舉,然後對它們進行排序,我會建議實現一個比較器,它可以對你的特定類型的枚舉進行排序,然後在它的compare()方法中進行適當的比較,例如:

Comparator<Chemical> comp = new Comparator<Chemical>() { 
    public int compare(Chemical o1, Chemical o2) { 
     if (o1.reactivity() > o2.reactivity()) { 
      return 1; 
     } else if (o1.reactivity() < o2.reactivity()) { 
      return -1; 
     } else { 
     return integer depending on whatever else you want to order by... 
     } 
    } 
}; 

然後,您可以將此比較器傳遞給sort方法,包含有序集合構造函數等。

您可以擴展您的枚舉子類以包含任何額外的方法,例如,反應性(),你需要。

4

在這裏是表示你根據不同的標準來分類的枚舉的相同的值的示例:

import java.util.Arrays; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 

public class SortEnum { 

    public enum TestEnum { 
     A(1, 2), B(5, 1), C(3, 0); 
     private int value1; 
     private int value2; 

     private TestEnum(int value1, int value2) { 
      this.value1 = value1; 
      this.value2 = value2; 
     } 

     public int getValue1() { 
      return value1; 
     } 

     public int getValue2() { 
      return value2; 
     } 
    } 

    public static void main(String[] args) { 
     List<TestEnum> list = Arrays.asList(TestEnum.values()); 
     System.err.println(list); 
     Collections.sort(list, new Comparator<TestEnum>() { 
      @Override 
      public int compare(TestEnum o1, TestEnum o2) { 
       return o1.getValue1() - o2.getValue1(); 
      } 
     }); 
     System.err.println(list); 
     Collections.sort(list, new Comparator<TestEnum>() { 
      @Override 
      public int compare(TestEnum o1, TestEnum o2) { 
       return o1.getValue2() - o2.getValue2(); 
      } 
     }); 
     System.err.println(list); 
    } 
} 

OUTPUT是

[A,B,C] [A,C, B] [C,B,A]

1

你可以把儘可能多的比較在枚舉,只要你喜歡,見下面的例子:

import java.util.Comparator; 

public enum Day { 
    MONDAY(1, 3), 
    TUESDAY(2, 6), 
    WEDNESDAY(3, 5), 
    THURSDAY(4, 4), 
    FRIDAY(5, 2), 
    SATURDAY(6, 1), 
    SUNDAY(0, 0); 

    private final int calendarPosition; 
    private final int workLevel; 

    Day(int position, int level) { 
     calendarPosition = position; 
     workLevel = level; 
    } 

    int getCalendarPosition(){ return calendarPosition; } 
    int getWorkLevel() { return workLevel; } 

    public static Comparator<Day> calendarPositionComparator = new Comparator<Day>() { 
     public int compare(Day d1, Day d2) { 
     return d1.getCalendarPosition() - d2.getCalendarPosition(); 
     } 
    }; 

    public static Comparator<Day> workLevelComparator = new Comparator<Day>() { 
     public int compare(Day d1, Day d2) { 
     // descending order, harder first 
     return d2.getWorkLevel() - d1.getWorkLevel(); 
     } 
    }; 
} 

在行動中看到的比較:

import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 

public class EnumDayTest 
{ 
    public static void main (String[] args) { 
    List<Day> allDays = Arrays.asList(Day.values()); 
    System.out.println("===\nListing days in order of calendar position:"); 
    Collections.sort(allDays, Day.calendarPositionComparator); 
    showItems(allDays); 
    System.out.println("===\nListing days in order of work level:"); 
    Collections.sort(allDays, Day.workLevelComparator); 
    showItems(allDays); 
    } 

    public static void showItems(List<Day> days) { 
    for (Day day : days) { 
     System.out.println(day.name()); 
    } 
    } 
}