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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:36:22+00:00 2026-05-23T14:36:22+00:00

In my code I have a List<Person> . Attributes to the objects in this

  • 0

In my code I have a List<Person>. Attributes to the objects in this list may include something along the lines of:

  • ID
  • First Name
  • Last Name

In a part of my application, I will be allowing the user to search for a specific person by using any combination of those three values. At the moment, I have a switch statement simply checking which fields are filled out, and calling the method designated for that combination of values.

i.e.:

switch typeOfSearch    
if 0, lookById()    
if 1, lookByIdAndName()  
if 2, lookByFirstName()

and so on. There are actually 7 different types.

This makes me have one method for each statement. Is this a ‘good’ way to do this? Is there a way that I should use a parameter or some sort of ‘filter’? It may not make a difference, but I’m coding this in Java.

  • 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-05-23T14:36:23+00:00Added an answer on May 23, 2026 at 2:36 pm

    You can do something more elgant with maps and interfaces. Try this for example,

    interface LookUp{
        lookUpBy(HttpRequest req);
    }
    
    Map<Integer, LookUp> map = new HashMap<Integer, LookUp>();
    
    map.put(0, new LookUpById());
    map.put(1, new LookUpByIdAndName());
    

    …

    in your controller then you can do

    int type = Integer.parseInt(request.getParameter(type));
    Person person = map.get(type).lookUpBy(request);
    

    This way you can quickly look up the method with a map. Of course you can also use a long switch but I feel this is more manageable.

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

Sidebar

Related Questions

I have a list of objects, each containing an Id, Code and Description. I
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
Hej, assuming I have a code that looks like this: List<User> userList = GetUserByName
I have a this aspx-code: (sample) <asp:DropDownList runat=server ID=ddList1></asp:DropDownList> With this codebehind: List<System.Web.UI.WebControls.ListItem> colors
I have found this example on StackOverflow: var people = new List<Person> { new
Suppose I have this code (pseudocode) class SomeClass { class Person { public static
(code examples are python) Lets assume we have a list of percentages that add
I have a list box control: <asp:ListBox runat=server id=lbox autoPostBack=true /> The code behind
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
I currently have some code that pulls down a list of users in a

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.