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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:51:03+00:00 2026-05-16T18:51:03+00:00

is it possible to determine the ArrayList-index of an element inside the ArrayList based

  • 0

is it possible to determine the ArrayList-index of an element inside the ArrayList based on one of its attributes?

In my particular case I have an Object “worker” with the attributes “name” and “worker_id”. The worker_id is an ID I get from the database.

Since the first worker-object inside the ArrayList doesn’t necessarily start with 0 (and may have gaps), it’s not the same ID as the index each element has inside this ArrayList.

Now I want to determine this particular index of a worker-object inside the ArrayList based on its attribute “worker_id”. However: since the list can be very long, I’d like to not have to iterate over the worker-ArrayList everytime.

Using a HashMap might be a solution, but I’d like to use an ArrayList if possible, because I normally won’t need a key (except in this case).

Any suggestions are welcome!

Thank you in advance,
Igor.

  • 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-16T18:51:03+00:00Added an answer on May 16, 2026 at 6:51 pm

    Since the first worker-object inside the ArrayList doesn’t necessarily start with 0 (and may have gaps), it’s not the same ID as the index each element has inside this ArrayList.

    It sounds to me that the list is at least sorted? In that case, you could use a binary search, which is slightly faster than a linear scan of the list.

    Another option is to define two workers to be equal if they have the same worker_id. Then you could simply do int index = workerList.indexOf(new Worker(idToSearchFor));

    Using a HashMap might be a solution, but I’d like to use an ArrayList if possible, because I normally won’t need a key (except in this case).

    Well, to be honest, when I use a map it is seldom because I couldn’t do it with a list, but rather because the interface of a map is nicer to work with, and results in cleaner code. So, even though you may think that a HashMap is an overkill, it may be worth it, solely because it’s slightly more logical and the code ends up being a bit easier to read.

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

Sidebar

Related Questions

Possible Duplicate: Determine original name of variable after its passed to a function. I
Possible Duplicate: determine target url based on roles in spring security 3.1 Hello all
Is it possible to determine the tables and its column in MS Access in
Is it possible to determine where Oracle is installed using pure PL/SQL? Use case:
Is it possible to determine whether a particular youtube video is encoded with H.264
Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which
Is it possible to determine the byte, or bytes, that have changed from a
Possible Duplicate: Determine device (iPhone, iPod Touch) with iOS I have tried to get
Is it possible to determine which submit button was used? I have a confirmation
Possible Duplicate: Determine word size of my processor It is One Interview question today.

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.