java胜出,使用枚举和Java 8查找胜出程序

I want to store data like:

(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

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?

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()

FEBRUARY

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.

Example List of enum objects.

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

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 );

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(): FEBRUARY

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

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.

}

}

Example usage.

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

ProgramType pt = ProgramType.highestPriority( programTypes );

pt.toString() = ACF

pt.getPriorityLevel() = 2

pt.getDisplayName() = AProgram

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值