1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Switch case implementation in Java for an integer pair combination

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

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


    Thank you.

    Continue reading...

Compartilhe esta Página