Take an array of values and compare them to Enum values in java

SilverEnsign99

So what I am trying to do in JAVA is take in a set of values and then compare those values against the same category of values in an Enum so I can find a specific enum.

In other words I have a bunch of Enums

public enum Fruits{ 
     APPLE("Red", "Round", "Fruit", true),
     //about 20 or so more Constants
     ORANGE("Orange","Sphere","Citrus", true);

     private String color;
     private String shape;
     private String category;
     private boolean edible;

private Fruits(String color, String shape, String category, boolean edible){
     this.color = color;
     this.shape = shape;
     this.category = category;
     this.edible = edible;
}

//getter methods for each variable
}

Now I have an array of strings

String[] myStringArray = {"Blue", "Oblong", "Berry"};
String[] myStringArrayTwo = {"Red", "Round", "Fruit"};

how could I compare my String Arrays to the String values in the enums so I can figure out which enum Constant my String Array Represents.

In Reality I have a class that reads in these string values from a file into an array and then I want to assign an enum to the class so I can give it a predefined classification that can be used by other classes.

fge

(note: the constructor needs not be private -- it is by default for enums)

You could code a method in the enum such as:

public static findByCharacteristics(final String[] characteristics)
{
    for (final Fruits candidate: values())
        if (candidate.color.equals(characteristics[0])
            && candidate.shape.equals(characteristics[1])
            && candidate.category.equals(characteristics[2]))
            return candidate:
    return null;
}

Then you would:

final Fruits f = Fruits.findByCharacteristics(theArray);
// check whether f is null; if not, you have a match

However, imho, the code "smells"; you should try a different approach. One such "smell" is that the method above requires an array with at least three elements.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Typescript enum values as array

分類Dev

compare values in arrays and if any values match, increment values in first array

分類Dev

Compare array values with same key in PHP

分類Dev

How to Return Enum objects with all properties from Enum values in Java

分類Dev

Appending session values to an Array in Java

分類Dev

How to find negative imaginary parts of values in an array then turning them to positive?

分類Dev

How to get distinct values from an array in mongoDB and group them by ID

分類Dev

Getting values of all checked checkboxes in PHP and put them in an array

分類Dev

How do I take row values from a column and combine them, sep by "," based on different column value?

分類Dev

Can you take values within certain groups and put them each in a new column in R?

分類Dev

Template specialization for enum values

分類Dev

Check enum for multiple values

分類Dev

PHP - How to remove duplicate values from array (compare 2 arrays)

分類Dev

Java array values being put into wrong array

分類Dev

Compare to multiple values in an if statement

分類Dev

Java:enum values()およびvalueOf(String)

分類Dev

I need to compare and retain multiple values from a Java Stream

分類Dev

counting values and group them without empty values

分類Dev

Take common values in nested dictionary

分類Dev

jsonschema enum values conditional on another enum value

分類Dev

Replacing values with for-loop in array in Java

分類Dev

How to get the values of generic array in Java?

分類Dev

Jackson: ignore unknown enum values

分類Dev

How values method work in enum

分類Dev

Compare Worksheet Values (Text and Arithmetic)

分類Dev

Database SQL queries, compare values

分類Dev

PHP compare arrays and replace values

分類Dev

Compare array values and find next value in the array based on custom value (PHP)

分類Dev

How to split an array element with multiple values to get only one of them in PHP

Related 関連記事

  1. 1

    Typescript enum values as array

  2. 2

    compare values in arrays and if any values match, increment values in first array

  3. 3

    Compare array values with same key in PHP

  4. 4

    How to Return Enum objects with all properties from Enum values in Java

  5. 5

    Appending session values to an Array in Java

  6. 6

    How to find negative imaginary parts of values in an array then turning them to positive?

  7. 7

    How to get distinct values from an array in mongoDB and group them by ID

  8. 8

    Getting values of all checked checkboxes in PHP and put them in an array

  9. 9

    How do I take row values from a column and combine them, sep by "," based on different column value?

  10. 10

    Can you take values within certain groups and put them each in a new column in R?

  11. 11

    Template specialization for enum values

  12. 12

    Check enum for multiple values

  13. 13

    PHP - How to remove duplicate values from array (compare 2 arrays)

  14. 14

    Java array values being put into wrong array

  15. 15

    Compare to multiple values in an if statement

  16. 16

    Java:enum values()およびvalueOf(String)

  17. 17

    I need to compare and retain multiple values from a Java Stream

  18. 18

    counting values and group them without empty values

  19. 19

    Take common values in nested dictionary

  20. 20

    jsonschema enum values conditional on another enum value

  21. 21

    Replacing values with for-loop in array in Java

  22. 22

    How to get the values of generic array in Java?

  23. 23

    Jackson: ignore unknown enum values

  24. 24

    How values method work in enum

  25. 25

    Compare Worksheet Values (Text and Arithmetic)

  26. 26

    Database SQL queries, compare values

  27. 27

    PHP compare arrays and replace values

  28. 28

    Compare array values and find next value in the array based on custom value (PHP)

  29. 29

    How to split an array element with multiple values to get only one of them in PHP

ホットタグ

アーカイブ