An alternative for the switch statement when all possible cases are known?

Im brand new to Java and I came across the following problem.

Im looking at a list of movies from a txt file, one of the fields is a string representation of which genre is classified as a movie, as well as a numerical representation of 1-5, meaning the movie has received one or more awards.

EX. one film may have the following meaning in this field β€œ12bSt”, this would mean that the film is b = biographical, S = sports, 2 = received an Academy Award. atm i do this:

String[] genreStringToArray(String genre) { char[] genreCharArray = genre.toCharArray(); this.genreArr = new String[genreCharArray.length]; for (int i = 0; i < genreCharArray.length; i++) { switch (genreCharArray[i]) { case 'a': genreArr[i] = "Action"; break; case 'A': genreArr[i] = "Animation"; break; case 'b': genreArr[i] = "biographical"; break; case 'c': genreArr[i] = "comedy"; break; case 'C': genreArr[i] = "children"; break; case 'd': genreArr[i] = "drama"; break; case 'D': genreArr[i] = "documentary"; break; case 'e': genreArr[i] = "epic"; break; ..... etc case 2:genreArr[i] = "Academy award"; break; case 3:genreArr[i] = "Palme d`or"; break; case 4:genreArr[i] = "Sight & sound"; break; case 5:genreArr[i] = "AFI top 100"; break; } } return genreArr; } 

My question is: which implementation will be more efficient than this?

+4
source share
5 answers

Use a card with a symbol (or just a string) as a key, and a string as a value.

 Map<Character, String> genres = new HashMap<Character, String>() {{ put('b', "biographical"); put('C', "Children"); put('2', "Academy Award"); // etc... }}; String genre = "b2C"; List<String> info = new ArrayList<String>(); for (int i = 0; i < genre.length(); i++) { info.add(genres.get(genre.charAt(i)); } System.out.println(StringUtils.join(info, ", ")); Outputs: biographical, Academy Award, Children 

Essentially, the same thing can be done with enum s, and if you need to convey this information, it would be better to do it in different ways.

+2
source

Create a map (once) and find the values ​​(still a loop through the char array).

 Map<Character, String> map= new HashMap<Character, String>(); .... genreArr[i] = map.get(genreCharArray[i]); .... 

ps Keep in mind that you have a bug in your current code. case 2: should be case '2':

+4
source

You can first put them all in a HashMap and then just access it. Do this if you need more access to this mapping. If you only check it once, you can let it do it (I suggest you put it in a method).

0
source

I think it is best to use object-oriented programming in this instance, that is, create an object representing what you are trying to do. Here is an example without knowing your domain.

 public class Film { private Genre genre; //Make this a List if necessary private Award award; // Make this a List if necessary //Other things that define a Film } public class Genre { enum GenreDefinition { ACTION("Action") //Add more as necessary } //Other things that define a genre } public class Award { enum AwardTypes { OSCAR("Oscar") //Add more as necessary } //Other things that define an award } 

Now you can save each of these objects to any structure that you need. Now, if you click these values ​​on a map or list, you can use the quick search time of any of these structures.

0
source

I would use a database table for this.

-1
source

Source: https://habr.com/ru/post/1380263/


All Articles