Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
Utilizing switch statements with enums and marker interface

I've got a marker interface

public interface Marker{}

and two enums which implement the Marker

public enum Sharpie implements Marker{
    RED,
    BLUE,
    BLACK
}

public enum Crayola implements Marker{
    PURPLE,
    ORANGE,
    GREEN
}

What I'm trying to do is utilize a switch statement, such as

public boolean isOwned(Marker m){
    // Take in a marker of either Sharpie, or Crayola
    switch(m){
        case BLUE:
        case BLACK:
        case GREEN:
            return true;
        default:
            return false;
    }
}

Is there a way to do this without using an expensive instanceof call?

Something like this would work, but I'm trying to avoid using instanceof, and frankly it looks kind of ugly.

public boolean isOwned(Marker m){

    // First determine instanceof and then cast the marker 
    // to the appropriate Type before utilizing the switch statement
    if (m instanceof Sharpie){
       switch((Sharpie) m){
           Case BLUE:
           Case BLACK:
               return true;
           default:
               return false;
       }
    } else {
       switch((Crayola) m){
           case Green:
               return true;
           default:
               return false;
       }
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Looks like a good scenario to try new Java Feature Sealed Interface and Pattern Matching for switch Expressions(* this is a preview feature as at jdk 17)

First make Marker as sealed interface

public sealed interface Marker permits Crayola, Sharpie {}

Then we can use switch expression to get rid of those instanceof checking.

    public boolean isOwned(Marker marker) {
        boolean isOwned = switch (marker) {
            case Sharpie s -> s == Sharpie.BLACK || s == Sharpie.BLUE;
            case Crayola c -> c == Crayola.GREEN;
        };
        return isOwned;
    }
over 4 years ago · Santiago Trujillo Relatório

0

Just change the switch to if. There is no need for instanceof:

public boolean isOwned(Marker m){
    if(m == Sharpie.BLUE || m == Sharpie.BLACK || m == Crayola.GREEN)
        return true;
    return false;
}
over 4 years ago · Santiago Trujillo Relatório

0

i don't know what are you doing. this example is using unique function for 2 diffrents enum. i think you should use extending in enum like this.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda