根据实现它们的类动态显示枚举值

时间:2019-05-31 17:39:38

标签: java enums

我正在构建JavaFx应用程序。 我想在不同的组合框中动态显示枚举值。

例如,在控制器A中,我只想显示枚举值“ high”,在控制器B中,我只想显示所有枚举值。

有人可以帮忙吗?

谢谢!

public enum EnumPrio {

    High("high"),
    Medium("medium"),
    Low("low");

    private String prio;

    EnumPrio(String prio) {
        this.prio = prio;
    }

}

1 个答案:

答案 0 :(得分:0)

public enum EnumPrio {

    High("high"),
    Medium("medium"),
    Low("low");

    private String prio;

    EnumPrio(String prio) {
        this.prio = prio;
    }

    public static List<EnumPrio> getEnumPrioListByController(String controller) {
        Stream<EnumPrio> enumPrioStream=Stream.of(EnumPrio.values());

        return controller.equals("A")?enumPrioStream
                .filter(enumPrio->enumPrio.prio.equals("high")).collect(Collectors.toList()):enumPrioStream
                .filter(enumPrio->!enumPrio.prio.equals("high")).collect(Collectors.toList());
    }
    public static void main(String[] args) {

        System.out.println("enumPrioListForControllerA="+EnumPrio.getEnumPrioListByController("A"));
        // enumPrioListForControllerA=[High]
        System.out.println("enumPrioListForControllerB="+EnumPrio.getEnumPrioListByController("B"));
        // enumPrioListForControllerB=[Medium, Low]
    }
}