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

  • Home
  • SEARCH
  • 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 8061097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:13:00+00:00 2026-06-05T10:13:00+00:00

Is there a built-in method to search a java.util.List specifying the first item to

  • 0

Is there a built-in method to search a java.util.List specifying the first item to start the search from? Like you can do with Strings

I know I can easily implement something on my own, but I’d rather not reinvent the wheel if Java or http://commons.apache.org/collections/api-release/org/apache/commons/collections/package-summary.html already has it.

I’m not asking how to implement this, I’m asking whether something is already available A lot of the suggestions here were buggy.

If anybody cares for receiving credit for the right answer, please update your answer to say that there is not a built-in way to do it (if you know for sure)

Here’s what I would like to do

List<String> strings = new ArrayList<String>();
// Add some values to the list here
// Search starting from the 6th item in the list
strings.indexOf("someValue", 5);

Right now I’m using

/**
 * This is like List.indexOf(), except that it allows you to specify the index to start the search from
 */
public static int indexOf(List<?> list, Object toFind, int startingIndex) {
    for (int index = startingIndex; index < list.size(); index++) {
        Object current = list.get(index);
        if (current != null && current.equals(toFind)) {
            return index;
        }
    }
    return -1;
}

And I’ve also implemented it as

public static int indexOf(List<?> list, Object toFind, int startingIndex) {
    int index = list.subList(startingIndex).indexOf(toFind);
    return index == -1 ? index : index + startingIndex;
}
  • 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-05T10:13:02+00:00Added an answer on June 5, 2026 at 10:13 am

    No not a single method, but there is a simple documented way of doing this with 1-2 lines of code. It even says so in the documentation for this method:

    strings.subList(5, strings.size()).indexOf("someValue");
    

    Possibly add 5 to the result (if not -1), depending on if you want to keep that sublist around or not etc:

    int result = list.subList(startIndex, list.size()).indexOf(someValue);
    return result== -1 ? -1 : result+startIndex;
    

    Note:
    subList does not create a new List, just a view into the original one.

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

Sidebar

Related Questions

There is no built in reverse method for Python's str object. How can I
Is there any built-in method in OpenCV ( C++ API ) to subtract two
Is there a built-in data structure in Java to represent a Crit-bit tree? Or
Is there a built-in way to get from a UIView to its UIViewController ?
Is there a built in Java code that will parse a given folder and
The require method in ruby will search the lib_path and load the first matching
How could I change the following method to a recursive method: public List<ScanItem> search(List<ScanItem>
using Java. the List is sorted so I want to use binary search. I'd
Are there any plugins or built-in methods in Vim for performing refactoring on C
Is there any built-in support for that? And if not, is there any consensus

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.