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 9098319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:20:09+00:00 2026-06-17T00:20:09+00:00

I need suggestion for the right approach to apply conditions in Java. I have

  • 0

I need suggestion for the right approach to apply conditions in Java.

I have 100 conditions based on which I have to change value of a String variable that would be displayed to the user.

an example condition: a<5 && (b>0 && c>8) && d>9 || x!=4

More conditions are there but variables are the same more or less.

I am doing this right now:

    if(condition1)
    else if(condition2)
    else if(condition3)
    ...

A switch case alternative would obviously be there nested within if-else’s i.e.

if(condition1)
 switch(x)
  {
   case y:
     blah-blah
   }        
else if(condition2)
switch(x)
  {
   case y:
     blah-blah
   }  
else if(condition3)
...

But I am looking for some more elegant solution like using an Interface for this with polymorphic support , What could be the thing that I could possibly do to avoid lines of code or what should be the right approach.

—Edit—


enter image description here

I actualy require this on an android device. But its more of a java construct here.

This is a small snapshot of conditions that I have with me. More will be added if a few pass/fail. That obviously would require more if-else’s with/without nesting. In that case would the processing go slow.

I am as of now storing the messages in a separate class with various string variables those I have kept static so if a condition gets true
then I pick the static variable from the only class and display that
one. Would that be right about storing the resultant messages.

  • 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-17T00:20:11+00:00Added an answer on June 17, 2026 at 12:20 am

    Depending on the number of conditional inputs, you might be able to use a look-up table, or even a HashMap, by encoding all inputs or even some relatively simple complex conditions in a single value:

    int key = 0;
    
    key |= a?(1):0;
    key |= b?(1<<1):0;
    key |= (c.size() > 1)?(1<<2):0;
    ...
    
    String result = table[key]; // Or result = map.get(key);
    

    This paradigm has the added advantage of constant time (O(1)) complexity, which may be important in some occasions. Depending on the complexity of the conditions, you might even have fewer branches in the code-path on average, as opposed to full-blown if-then-else spaghetti code, which might lead to performance improvements.

    We might be able to help you more if you added more context to your question. Where are the condition inputs coming from? What are they like?

    And the more important question: What is the actual problem that you are trying to solve?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need suggestion about YAMI library . I have a system which receives Json
I need some suggestion over this approach - please see examples below. I have
I need a excel-like grid control in MFC, do anyone have good suggestion to
Locally I need to code against Java 1.4. I tried the suggestion in this
I need to write a java application which can merge docx files. Any suggestions?
I need to store pairs of float,int in which the int value stores the
I have created an application (a web-based application) which now has a large number
I know my question isn't about programming problem, I just need a suggestion to
I need suggestions on how can I download attachments from my IMAP mails which
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0

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.