Switch case implementation in Java for an integer pair combinationasMap-implementation for Java (based on...

Find the smallest value of the function

Why did Mr. Elliot have to decide whose boots were thickest in "Persuasion"?

Stuck on a Geometry Puzzle

Can you determine if focus is sharp without diopter adjustment if your sight is imperfect?

Why is it that Bernie Sanders is always called a "socialist"?

Is there a way to not have to poll the UART of an AVR?

Are the positive and negative planes inner or outer planes in the Great Wheel cosmology model?

Need help with a circuit diagram where the motor does not seem to have any connection to ground. Error with diagram? Or am i missing something?

Article. The word "Respect"

Do authors have to be politically correct in article-writing?

Equivalent of "illegal" for violating civil law

Why is that max-Q doesn't occur in transonic regime?

Count repetitions of an array

What is a good reason for every spaceship to carry a weapon on board?

Why is 'diphthong' pronounced the way it is?

Translation needed for 130 years old church document

Prevent Nautilus / Nemo from creating .Trash-1000 folder in mounted devices

Is there a verb that means to inject with poison?

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

How big is a framed opening for a door relative to the finished door opening width?

How to completely remove a package in Ubuntu (like it never existed)

What species should be used for storage of human minds?

Crack the bank account's password!

How do you get out of your own psychology to write characters?



Switch case implementation in Java for an integer pair combination


asMap-implementation for Java (based on Arrays.asList)Optimizing code block using Switch CaseRefactoring switch case statement in different classesInteger quicksort in JavaCombination generator in JavaCombination generator in Java - 2nd iterationCalculating pair combinations in JavaBinary Search Implementation for Integer ArraySwitch-like functionality for non-switchable types in JavaJava implementation of well-separated pair decomposition













4












$begingroup$


I have following Python code:




def get_subject_from_stream_id_and_subject_id(stream_id, subject_id):
#(stream_id, subject_id): ("subject_name")
return {
(1, 1): "Accounts",
(1, 2): "English",
(1, 3): "Organization of Commerce",
(2, 1): "Physics",
(2, 2): "English",
(2, 3): "Biology"
}.get((stream_id, subject_id), "None")



In this code, I want to get subject name from the integer pair combination i.e. stream_id, subject_id e.g. (1, 2) is for English. It was implemented using a Python tuple.



I want to implement the same piece of code in Java.



Could someone write this in a better way in Java?



public String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
switch (streamId) {
case 1:
switch (subjectId) {
case 1:
return "Accounts";
case 2:
return "English";
case 3:
return "Organization of Commerce";
default:
return null;
}

case 2:
switch (subjectId) {
case 1:
return "Physics";
case 2:
return "English";
case 3:
return "Biology";
default:
return null;
}
default:
return null;
}
}









share|improve this question









New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    That doesn't look like syntactically valid Java code.
    $endgroup$
    – 200_success
    9 hours ago










  • $begingroup$
    It's lacking method parameter types. Otherwise it compiles just great.
    $endgroup$
    – TorbenPutkonen
    9 hours ago












  • $begingroup$
    @200_success I updated it
    $endgroup$
    – Edge Goldberg
    8 hours ago










  • $begingroup$
    This looks like a fairly simple 2D array lookup. Just subtract 1 from the indices, and check they're in bounds. Any solution involving a dictionary is inefficient - both in terms of performance and code complexity.
    $endgroup$
    – bace1000
    50 mins ago
















4












$begingroup$


I have following Python code:




def get_subject_from_stream_id_and_subject_id(stream_id, subject_id):
#(stream_id, subject_id): ("subject_name")
return {
(1, 1): "Accounts",
(1, 2): "English",
(1, 3): "Organization of Commerce",
(2, 1): "Physics",
(2, 2): "English",
(2, 3): "Biology"
}.get((stream_id, subject_id), "None")



In this code, I want to get subject name from the integer pair combination i.e. stream_id, subject_id e.g. (1, 2) is for English. It was implemented using a Python tuple.



I want to implement the same piece of code in Java.



Could someone write this in a better way in Java?



public String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
switch (streamId) {
case 1:
switch (subjectId) {
case 1:
return "Accounts";
case 2:
return "English";
case 3:
return "Organization of Commerce";
default:
return null;
}

case 2:
switch (subjectId) {
case 1:
return "Physics";
case 2:
return "English";
case 3:
return "Biology";
default:
return null;
}
default:
return null;
}
}









share|improve this question









New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    That doesn't look like syntactically valid Java code.
    $endgroup$
    – 200_success
    9 hours ago










  • $begingroup$
    It's lacking method parameter types. Otherwise it compiles just great.
    $endgroup$
    – TorbenPutkonen
    9 hours ago












  • $begingroup$
    @200_success I updated it
    $endgroup$
    – Edge Goldberg
    8 hours ago










  • $begingroup$
    This looks like a fairly simple 2D array lookup. Just subtract 1 from the indices, and check they're in bounds. Any solution involving a dictionary is inefficient - both in terms of performance and code complexity.
    $endgroup$
    – bace1000
    50 mins ago














4












4








4





$begingroup$


I have following Python code:




def get_subject_from_stream_id_and_subject_id(stream_id, subject_id):
#(stream_id, subject_id): ("subject_name")
return {
(1, 1): "Accounts",
(1, 2): "English",
(1, 3): "Organization of Commerce",
(2, 1): "Physics",
(2, 2): "English",
(2, 3): "Biology"
}.get((stream_id, subject_id), "None")



In this code, I want to get subject name from the integer pair combination i.e. stream_id, subject_id e.g. (1, 2) is for English. It was implemented using a Python tuple.



I want to implement the same piece of code in Java.



Could someone write this in a better way in Java?



public String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
switch (streamId) {
case 1:
switch (subjectId) {
case 1:
return "Accounts";
case 2:
return "English";
case 3:
return "Organization of Commerce";
default:
return null;
}

case 2:
switch (subjectId) {
case 1:
return "Physics";
case 2:
return "English";
case 3:
return "Biology";
default:
return null;
}
default:
return null;
}
}









share|improve this question









New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I have following Python code:




def get_subject_from_stream_id_and_subject_id(stream_id, subject_id):
#(stream_id, subject_id): ("subject_name")
return {
(1, 1): "Accounts",
(1, 2): "English",
(1, 3): "Organization of Commerce",
(2, 1): "Physics",
(2, 2): "English",
(2, 3): "Biology"
}.get((stream_id, subject_id), "None")



In this code, I want to get subject name from the integer pair combination i.e. stream_id, subject_id e.g. (1, 2) is for English. It was implemented using a Python tuple.



I want to implement the same piece of code in Java.



Could someone write this in a better way in Java?



public String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
switch (streamId) {
case 1:
switch (subjectId) {
case 1:
return "Accounts";
case 2:
return "English";
case 3:
return "Organization of Commerce";
default:
return null;
}

case 2:
switch (subjectId) {
case 1:
return "Physics";
case 2:
return "English";
case 3:
return "Biology";
default:
return null;
}
default:
return null;
}
}






java






share|improve this question









New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 hours ago









200_success

129k15153417




129k15153417






New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 9 hours ago









Edge GoldbergEdge Goldberg

212




212




New contributor




Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Edge Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    That doesn't look like syntactically valid Java code.
    $endgroup$
    – 200_success
    9 hours ago










  • $begingroup$
    It's lacking method parameter types. Otherwise it compiles just great.
    $endgroup$
    – TorbenPutkonen
    9 hours ago












  • $begingroup$
    @200_success I updated it
    $endgroup$
    – Edge Goldberg
    8 hours ago










  • $begingroup$
    This looks like a fairly simple 2D array lookup. Just subtract 1 from the indices, and check they're in bounds. Any solution involving a dictionary is inefficient - both in terms of performance and code complexity.
    $endgroup$
    – bace1000
    50 mins ago


















  • $begingroup$
    That doesn't look like syntactically valid Java code.
    $endgroup$
    – 200_success
    9 hours ago










  • $begingroup$
    It's lacking method parameter types. Otherwise it compiles just great.
    $endgroup$
    – TorbenPutkonen
    9 hours ago












  • $begingroup$
    @200_success I updated it
    $endgroup$
    – Edge Goldberg
    8 hours ago










  • $begingroup$
    This looks like a fairly simple 2D array lookup. Just subtract 1 from the indices, and check they're in bounds. Any solution involving a dictionary is inefficient - both in terms of performance and code complexity.
    $endgroup$
    – bace1000
    50 mins ago
















$begingroup$
That doesn't look like syntactically valid Java code.
$endgroup$
– 200_success
9 hours ago




$begingroup$
That doesn't look like syntactically valid Java code.
$endgroup$
– 200_success
9 hours ago












$begingroup$
It's lacking method parameter types. Otherwise it compiles just great.
$endgroup$
– TorbenPutkonen
9 hours ago






$begingroup$
It's lacking method parameter types. Otherwise it compiles just great.
$endgroup$
– TorbenPutkonen
9 hours ago














$begingroup$
@200_success I updated it
$endgroup$
– Edge Goldberg
8 hours ago




$begingroup$
@200_success I updated it
$endgroup$
– Edge Goldberg
8 hours ago












$begingroup$
This looks like a fairly simple 2D array lookup. Just subtract 1 from the indices, and check they're in bounds. Any solution involving a dictionary is inefficient - both in terms of performance and code complexity.
$endgroup$
– bace1000
50 mins ago




$begingroup$
This looks like a fairly simple 2D array lookup. Just subtract 1 from the indices, and check they're in bounds. Any solution involving a dictionary is inefficient - both in terms of performance and code complexity.
$endgroup$
– bace1000
50 mins ago










3 Answers
3






active

oldest

votes


















3












$begingroup$

Java equivalent of the above Python code goes more like this:



private static final Map<List<Integer>, String> SUBJECT_MAP = createSubjectMap();

private static Map<List<Integer>, String> createSubjectMap() {
Map<List<Integer>, String> map = new HashMap<>();
map.put(asList(1, 1), "Accounts");
map.put(asList(1, 2), "English");
map.put(asList(1, 3), "Organization of Commerce");
map.put(asList(2, 1), "Physics");
map.put(asList(2, 2), "English");
map.put(asList(2, 3), "Biology");
return map;
}

public static String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
return SUBJECT_MAP.get(asList(streamId, subjectId));
}





share|improve this answer











$endgroup$













  • $begingroup$
    Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
    $endgroup$
    – rath
    5 hours ago



















2












$begingroup$

Neither is good, but surely we can all agree that the Java example is way uglier. The biggest problem, however, is that both examples hard code data into code.



Separate data, i.e. stream and subject IDs and their titles into a data class and in that class implement code that accesses the data structure without detailed knowledge about the actual data. Responsibility of setting up the data structure to resemble your stream and subject numbering is left to a separate component (load it from file or set up in static code).



Whether the data class is a recursive structure or just a wrapper for a HashMap depends on the complexity of your data.






share|improve this answer











$endgroup$





















    1












    $begingroup$

    When something is difficult in Java it's nearly always because there is a better way to do it (in Java, it generally means you are missing a class). In your case, it looks like you need a "Subject" class.



    Let's say you had a "Subject" class, how would it be implemented? Here's one way (Not my favorite)



    enum Subject {
    Accounts(1,1),
    English(1,2),
    …;
    public static Subject getSubject(streamId, subjectId) {
    // Iterate over Subject.values and return one that matches
    }


    This means that your call becomes simple:



    assertEquals(Subjects.Accounts, Subject.getSubject(1,1));


    I don't completely love this because it's still hard-coded. I would personally make the Subjects class a full class and load it from either a database or a data file, but it should still have a getSubject() static.



    Just in case you think there is some kind of performance issue caused by looping instead of a switch, note that A) java is 10x faster than python to start and B) premature optimization is the root of all evil (well, lots of evil)






    share|improve this answer











    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "196"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Edge Goldberg is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214234%2fswitch-case-implementation-in-java-for-an-integer-pair-combination%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3












      $begingroup$

      Java equivalent of the above Python code goes more like this:



      private static final Map<List<Integer>, String> SUBJECT_MAP = createSubjectMap();

      private static Map<List<Integer>, String> createSubjectMap() {
      Map<List<Integer>, String> map = new HashMap<>();
      map.put(asList(1, 1), "Accounts");
      map.put(asList(1, 2), "English");
      map.put(asList(1, 3), "Organization of Commerce");
      map.put(asList(2, 1), "Physics");
      map.put(asList(2, 2), "English");
      map.put(asList(2, 3), "Biology");
      return map;
      }

      public static String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
      return SUBJECT_MAP.get(asList(streamId, subjectId));
      }





      share|improve this answer











      $endgroup$













      • $begingroup$
        Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
        $endgroup$
        – rath
        5 hours ago
















      3












      $begingroup$

      Java equivalent of the above Python code goes more like this:



      private static final Map<List<Integer>, String> SUBJECT_MAP = createSubjectMap();

      private static Map<List<Integer>, String> createSubjectMap() {
      Map<List<Integer>, String> map = new HashMap<>();
      map.put(asList(1, 1), "Accounts");
      map.put(asList(1, 2), "English");
      map.put(asList(1, 3), "Organization of Commerce");
      map.put(asList(2, 1), "Physics");
      map.put(asList(2, 2), "English");
      map.put(asList(2, 3), "Biology");
      return map;
      }

      public static String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
      return SUBJECT_MAP.get(asList(streamId, subjectId));
      }





      share|improve this answer











      $endgroup$













      • $begingroup$
        Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
        $endgroup$
        – rath
        5 hours ago














      3












      3








      3





      $begingroup$

      Java equivalent of the above Python code goes more like this:



      private static final Map<List<Integer>, String> SUBJECT_MAP = createSubjectMap();

      private static Map<List<Integer>, String> createSubjectMap() {
      Map<List<Integer>, String> map = new HashMap<>();
      map.put(asList(1, 1), "Accounts");
      map.put(asList(1, 2), "English");
      map.put(asList(1, 3), "Organization of Commerce");
      map.put(asList(2, 1), "Physics");
      map.put(asList(2, 2), "English");
      map.put(asList(2, 3), "Biology");
      return map;
      }

      public static String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
      return SUBJECT_MAP.get(asList(streamId, subjectId));
      }





      share|improve this answer











      $endgroup$



      Java equivalent of the above Python code goes more like this:



      private static final Map<List<Integer>, String> SUBJECT_MAP = createSubjectMap();

      private static Map<List<Integer>, String> createSubjectMap() {
      Map<List<Integer>, String> map = new HashMap<>();
      map.put(asList(1, 1), "Accounts");
      map.put(asList(1, 2), "English");
      map.put(asList(1, 3), "Organization of Commerce");
      map.put(asList(2, 1), "Physics");
      map.put(asList(2, 2), "English");
      map.put(asList(2, 3), "Biology");
      return map;
      }

      public static String getSubjectFromStreamIdAndSubjectId(int streamId, int subjectId) {
      return SUBJECT_MAP.get(asList(streamId, subjectId));
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 3 hours ago









      mdfst13

      17.8k62157




      17.8k62157










      answered 6 hours ago









      abuzittin gillifircaabuzittin gillifirca

      5,907924




      5,907924












      • $begingroup$
        Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
        $endgroup$
        – rath
        5 hours ago


















      • $begingroup$
        Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
        $endgroup$
        – rath
        5 hours ago
















      $begingroup$
      Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
      $endgroup$
      – rath
      5 hours ago




      $begingroup$
      Might be better to use a custom SubjectIdentifier class since a List seems rather inefficient.
      $endgroup$
      – rath
      5 hours ago













      2












      $begingroup$

      Neither is good, but surely we can all agree that the Java example is way uglier. The biggest problem, however, is that both examples hard code data into code.



      Separate data, i.e. stream and subject IDs and their titles into a data class and in that class implement code that accesses the data structure without detailed knowledge about the actual data. Responsibility of setting up the data structure to resemble your stream and subject numbering is left to a separate component (load it from file or set up in static code).



      Whether the data class is a recursive structure or just a wrapper for a HashMap depends on the complexity of your data.






      share|improve this answer











      $endgroup$


















        2












        $begingroup$

        Neither is good, but surely we can all agree that the Java example is way uglier. The biggest problem, however, is that both examples hard code data into code.



        Separate data, i.e. stream and subject IDs and their titles into a data class and in that class implement code that accesses the data structure without detailed knowledge about the actual data. Responsibility of setting up the data structure to resemble your stream and subject numbering is left to a separate component (load it from file or set up in static code).



        Whether the data class is a recursive structure or just a wrapper for a HashMap depends on the complexity of your data.






        share|improve this answer











        $endgroup$
















          2












          2








          2





          $begingroup$

          Neither is good, but surely we can all agree that the Java example is way uglier. The biggest problem, however, is that both examples hard code data into code.



          Separate data, i.e. stream and subject IDs and their titles into a data class and in that class implement code that accesses the data structure without detailed knowledge about the actual data. Responsibility of setting up the data structure to resemble your stream and subject numbering is left to a separate component (load it from file or set up in static code).



          Whether the data class is a recursive structure or just a wrapper for a HashMap depends on the complexity of your data.






          share|improve this answer











          $endgroup$



          Neither is good, but surely we can all agree that the Java example is way uglier. The biggest problem, however, is that both examples hard code data into code.



          Separate data, i.e. stream and subject IDs and their titles into a data class and in that class implement code that accesses the data structure without detailed knowledge about the actual data. Responsibility of setting up the data structure to resemble your stream and subject numbering is left to a separate component (load it from file or set up in static code).



          Whether the data class is a recursive structure or just a wrapper for a HashMap depends on the complexity of your data.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago









          mdfst13

          17.8k62157




          17.8k62157










          answered 8 hours ago









          TorbenPutkonenTorbenPutkonen

          25518




          25518























              1












              $begingroup$

              When something is difficult in Java it's nearly always because there is a better way to do it (in Java, it generally means you are missing a class). In your case, it looks like you need a "Subject" class.



              Let's say you had a "Subject" class, how would it be implemented? Here's one way (Not my favorite)



              enum Subject {
              Accounts(1,1),
              English(1,2),
              …;
              public static Subject getSubject(streamId, subjectId) {
              // Iterate over Subject.values and return one that matches
              }


              This means that your call becomes simple:



              assertEquals(Subjects.Accounts, Subject.getSubject(1,1));


              I don't completely love this because it's still hard-coded. I would personally make the Subjects class a full class and load it from either a database or a data file, but it should still have a getSubject() static.



              Just in case you think there is some kind of performance issue caused by looping instead of a switch, note that A) java is 10x faster than python to start and B) premature optimization is the root of all evil (well, lots of evil)






              share|improve this answer











              $endgroup$


















                1












                $begingroup$

                When something is difficult in Java it's nearly always because there is a better way to do it (in Java, it generally means you are missing a class). In your case, it looks like you need a "Subject" class.



                Let's say you had a "Subject" class, how would it be implemented? Here's one way (Not my favorite)



                enum Subject {
                Accounts(1,1),
                English(1,2),
                …;
                public static Subject getSubject(streamId, subjectId) {
                // Iterate over Subject.values and return one that matches
                }


                This means that your call becomes simple:



                assertEquals(Subjects.Accounts, Subject.getSubject(1,1));


                I don't completely love this because it's still hard-coded. I would personally make the Subjects class a full class and load it from either a database or a data file, but it should still have a getSubject() static.



                Just in case you think there is some kind of performance issue caused by looping instead of a switch, note that A) java is 10x faster than python to start and B) premature optimization is the root of all evil (well, lots of evil)






                share|improve this answer











                $endgroup$
















                  1












                  1








                  1





                  $begingroup$

                  When something is difficult in Java it's nearly always because there is a better way to do it (in Java, it generally means you are missing a class). In your case, it looks like you need a "Subject" class.



                  Let's say you had a "Subject" class, how would it be implemented? Here's one way (Not my favorite)



                  enum Subject {
                  Accounts(1,1),
                  English(1,2),
                  …;
                  public static Subject getSubject(streamId, subjectId) {
                  // Iterate over Subject.values and return one that matches
                  }


                  This means that your call becomes simple:



                  assertEquals(Subjects.Accounts, Subject.getSubject(1,1));


                  I don't completely love this because it's still hard-coded. I would personally make the Subjects class a full class and load it from either a database or a data file, but it should still have a getSubject() static.



                  Just in case you think there is some kind of performance issue caused by looping instead of a switch, note that A) java is 10x faster than python to start and B) premature optimization is the root of all evil (well, lots of evil)






                  share|improve this answer











                  $endgroup$



                  When something is difficult in Java it's nearly always because there is a better way to do it (in Java, it generally means you are missing a class). In your case, it looks like you need a "Subject" class.



                  Let's say you had a "Subject" class, how would it be implemented? Here's one way (Not my favorite)



                  enum Subject {
                  Accounts(1,1),
                  English(1,2),
                  …;
                  public static Subject getSubject(streamId, subjectId) {
                  // Iterate over Subject.values and return one that matches
                  }


                  This means that your call becomes simple:



                  assertEquals(Subjects.Accounts, Subject.getSubject(1,1));


                  I don't completely love this because it's still hard-coded. I would personally make the Subjects class a full class and load it from either a database or a data file, but it should still have a getSubject() static.



                  Just in case you think there is some kind of performance issue caused by looping instead of a switch, note that A) java is 10x faster than python to start and B) premature optimization is the root of all evil (well, lots of evil)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 hours ago

























                  answered 3 hours ago









                  Bill KBill K

                  34117




                  34117






















                      Edge Goldberg is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Edge Goldberg is a new contributor. Be nice, and check out our Code of Conduct.













                      Edge Goldberg is a new contributor. Be nice, and check out our Code of Conduct.












                      Edge Goldberg is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Code Review Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214234%2fswitch-case-implementation-in-java-for-an-integer-pair-combination%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Cannot install PyQt5 The Next CEO of Stack OverflowCannot install tcpreplay 3.4.4cannot...

                      Kapp-Putsch Acontecimentos | Outros artigos | Menu de navegação

                      Why did early computer designers eschew integers? The Next CEO of Stack OverflowWhat register...