使用枚举和Java 8查找胜出程序 [英] Finding wining program using enums and Java 8

查看:95
本文介绍了使用枚举和Java 8查找胜出程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储如下数据:

(VCX, 1, VProgram), (ACF, 2, AProgram), (IFL, 3, IProgram) - //[ProgramType, priority, programName]

Input: inputProgramTypes: {ABC, VCX, IFL}

Output: winning programName based on priority // In this case: VProgram

所以我打算使用具有多个值的Enum:

So I am plannig to use a Enum with multiple values:

public enum ProgramType {
VCX("VProgram", 1),
ACF("AProgram", 2),
IFL("IProgram", 3);

ProgramType(final String name) {
    this.name = name;
}

ProgramType(final String name, final int prio) {
    this.name = name;
    this.prio = prio;
}

还有更好的存储方式吗?

Is there any better way to store?

此外,我不确定使用流使用getTopPrio方法的实现. (我可以实施)

Also, I am not sure on the implemention of the method to getTopPrio using streams. (I can implement)

method: getTopPriority(List<> programTypeList) {
    impl using streams??
}

推荐答案

tl; dr

EnumSet
.copyOf( List.of( Month.JUNE , Month.FEBRUARY ) )
.iterator()
.next()
.toString()

2月

详细信息

如果您的优先级编号恰好与枚举定义中每个枚举对象位置的序号一致,那么您的问题就会变得更加简单.也就是说,如果第一个枚举对象的优先级为1,第二个枚举对象的优先级为2,第三个枚举对象的优先级为3,依此类推.

Details

If your priority number happens to coincide with the ordinal number of each enum object’s place within the enum definition, your problem becomes simpler. That is, if the first enum object’s priority is 1, and the second is 2, and the third is 3, and so on.

枚举对象的示例List.

List < Month > months = List.of( Month.JUNE , Month.FEBRUARY );

EnumSetSet的实现,已优化用于处理枚举对象. EnumSet占用很少的内存并且执行非常快.

EnumSet is an implementation of Set optimized for handling enum objects. An EnumSet takes little memory and executes very quickly.

Set < Month > set = EnumSet.copyOf( months );

EnumSet的迭代器顺序恰好是在该枚举上定义枚举对象的顺序.因此,在我们的6月& 2月,2月在集合中排在第一位.我们只是从其迭代器中请求第一个对象来解决您的问题.

The iterator order of an EnumSet happens to be the order in which the enum objects are defined on that enum. So in our example of June & February, February comes first in the set. We simply ask for the first object from its iterator to solve your Question.

Month month = set.iterator().next();

month.toString():2月

month.toString(): FEBRUARY

因此,您实际上不需要在枚举中具有该prio成员字段.通过将一个枚举对象加到由错误命名的方法Enum::ordinal返回的int中,您可以获得每个枚举对象的序号.

So you do not actually need to have that prio member field on your enum. You can get the ordinal number of each enum object by adding one to the int returned by the misnamed method Enum::ordinal.

Month.DECEMBER.ordinal() + 1

12

从您的枚举构造函数中省略prio参数.添加方法getPriority.

Omit the prio argument from your enum constructor. Add a method getPriority.

package work.basil.example;

import java.util.EnumSet;
import java.util.List;
import java.util.Objects;

public enum ProgramType
{
    VCX( "VProgram" ), ACF( "AProgram" ), IFL( "IProgram" );

    private String displayName;

    ProgramType ( final String name )
    {
        this.displayName = name;
    }

    public String getDisplayName ( )
    {
        return this.displayName;
    }

    public int getPriorityLevel ( )
    {
        int priority = this.ordinal() + 1;  // `Enum::ordinal` method is misnamed, actually returning a zero-based index number rather than one-based ordinal number.
        return priority;
    }

    static ProgramType highestPriority ( List < ProgramType > programTypes )
    {
        Objects.requireNonNull( programTypes , "Received null list of program types. Message # 539b0b72-d28c-4e54-b82e-2e3b4d760f28." );
        if ( programTypes.isEmpty() ) { throw new IllegalArgumentException( "No program types specified (empty list). Message # 485b0856-a384-4090-aa5f-0d412cb0c8f6." );}

        ProgramType pt = 
                EnumSet
                .copyOf( programTypes )
                .iterator()
                .next()
        ;

        return pt ;  // Or return pt.getPriorityLevel() if the number was your intention.
    }
}

示例用法.

List < ProgramType > programTypes = List.of( ProgramType.IFL , ProgramType.ACF );
ProgramType pt = ProgramType.highestPriority( programTypes );

pt.toString()= ACF

pt.toString() = ACF

pt.getPriorityLevel()= 2

pt.getPriorityLevel() = 2

pt.getDisplayName()= AProgram

pt.getDisplayName() = AProgram

这篇关于使用枚举和Java 8查找胜出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆