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
4221

被折叠的 条评论
为什么被折叠?



