Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:05:02+00:00 2026-06-09T13:05:02+00:00

This is a simplified example. I have this enum declaration as follows: public enum

  • 0

This is a simplified example. I have this enum declaration as follows:

public enum ELogLevel {
    None,
    Debug,
    Info,
    Error
}

I have this code in another class:

if ((CLog._logLevel == ELogLevel.Info) || (CLog._logLevel == ELogLevel.Debug) || (CLog._logLevel == ELogLevel.Error)) {
    System.out.println(formatMessage(message));
}

My question is if there is a way to shorten the test. Ideally i would like somethign to the tune of (this is borrowed from Pascal/Delphi):

if (CLog._logLevel in [ELogLevel.Info, ELogLevel.Debug, ELogLevel.Error])

Instead of the long list of comparisons. Is there such a thing in Java, or maybe a way to achieve it? I am using a trivial example, my intention is to find out if there is a pattern so I can do these types of tests with enum value lists of many more elements.

EDIT: It looks like EnumSet is the closest thing to what I want. The Naïve way of implementing it is via something like:

if (EnumSet.of(ELogLevel.Info, ELogLevel.Debug, ELogLevel.Error).contains(CLog._logLevel))

But under benchmarking, this performs two orders of magnitude slower than the long if/then statement, I guess because the EnumSet is being instantiated every time it runs. This is a problem only for code that runs very often, and even then it’s a very minor problem, since over 100M iterations we are talking about 7ms vs 450ms on my box; a very minimal amount of time either way.

What I settled on for code that runs very often is to pre-instantiate the EnumSet in a static variable, and use that instance in the loop, which cuts down the runtime back down to a much more palatable 9ms over 100M iterations.

So it looks like we have a winner! Thanks guys for your quick replies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:05:03+00:00Added an answer on June 9, 2026 at 1:05 pm

    what you want is an enum set

    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/EnumSet.html

    put the elements you want to test for in the set, and then use the Set method contains().

    import java.util.EnumSet;
    
    
    public class EnumSetExample
    {
      enum Level { NONE, DEBUG, INFO, ERROR };
    
      public static void main(String[] args)
      {
        EnumSet<Level> subset = EnumSet.of(Level.DEBUG, Level.INFO);
    
        for(Level currentLevel : EnumSet.allOf(Level.class))
        {
          if (subset.contains(currentLevel))
          {
            System.out.println("we have " + currentLevel.toString());
          }
          else
          {
            System.out.println("we don't have " + currentLevel.toString());
          }
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like this (this is a simplified example): function callback_func($matches) {
Suppose that we have this code (simplified example): $propertyName = 'items'; $foo = new
I've simplified this example as much as I can. I have a remote function:
I've simplified my JavaScript code to the example below; this code is giving me
[This is a simplified example] I have a collection (myCollection) in which exists three
I have an HTML table that is similar to this simplified example: <table> <tbody>
So I have two tables in this simplified example: People and Houses. People can
I have created a stored procedure similar to this simplified example: CREATE PROCEDURE dbo.sp_MyStoredProcedure
I have a larger SQL that produces a similar output as this simplified example:
Consider this simplified example: #include <list> typedef std::list<int> IntList; class KindaIntList { public: IntList::const_iterator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.