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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:40:50+00:00 2026-05-16T04:40:50+00:00

I’ve got the following Java code but List.indexOf() seems to do pretty much the

  • 0

I’ve got the following Java code but List.indexOf() seems to do pretty much the same thing (including returning -1 if not found. Is there any way of passing indexOf() an Object that expresses the idea that the object is not 0?

/**
 * Find the first non-zero element in a List of Integers
 * @param row List of Integers
 * @return -1 if all zeros in row
 * otherwise position of first non zero element
 */
public static int leading(List<Integer> row) {

 for (int i = 0; i < row.size(); i++) {
   if (row.get(i)!= 0) {
      return i;
   }
 }

 return -1;
}

Re: Thorbjørn Ravn Andersen: If I passed in null into IndexOf() it will always return -1 because my list always contains Integers. I want to do something like row.indexOf(Integer a where !a.equals(0)). Not sure if possible

  • 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-16T04:40:51+00:00Added an answer on May 16, 2026 at 4:40 am

    List.indexOf "solution"

    List.indexOf(Object o) is defined as follows:

    Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that

    (o==null ? get(i)==null : o.equals(get(i)))
    

    is true, or -1 if there is no such index.

    It is tempting to try to give a "meta" element object that is not really in the List, and may not even be of the same type as the actual elements of the List, and yet is equals to some desired element based on a predicate. This should work, since indexOf is defined in terms of the given Object o‘s equals method against an element in the list (and not the other way around), but is a really "hacky" way of achieving what you want.

    Here’s a proof of concept:

    // PROOF OF CONCEPT ONLY! DO NOT IMITATE!
    // abusing indexOf(Object) to find index of a negative integer in List
    
    List<Integer> nums = Arrays.asList(3,4,5,-6,7);
    Object equalsNegativeInteger = new Object() {
        @Override public boolean equals(Object o) {
            return (o instanceof Integer) && ((Integer) o) < 0;
        }
    };
    System.out.println(nums.indexOf(equalsNegativeInteger));
    // prints 3
    

    The "meta" element object is equals to any negative Integer, and yet no Integer can ever be equals to it. This asymmetry is a gross violation of the equals contract, but it "works" nonetheless.


    Guava solution

    A much better solution that conveys the intent is using Guava’s higher-order functions. Here’s one from com.google.commons.collect.Iterables:

    <T> int indexOf(Iterable<T> iterable, Predicate<? super T> predicate)

    Returns the index in iterable of the first element that satisfies the provided predicate, or -1 if the Iterable has no such elements. More formally, returns the lowest index i such that:

     predicate.apply(Iterables.get(iterable, i))
    

    is true, or -1 if there is no such index.

    Snippet

    Here’s a snippet to illustrate the expressive power of Guava’s higher-order functions:

    import com.google.common.collect.*;
    import com.google.common.base.*;
    import java.util.*;
    
    public class IterablesPredicateExample {
        public static void main(String[] args) {
            List<Integer> nums = Arrays.asList(1,2,-3,4,-5,6,-7,-8);
            Predicate<Integer> isNegative = new Predicate<Integer>() {
                @Override public boolean apply(Integer n) {
                    return n < 0;
                }
            };          
            // Get index of first negative number
            System.out.println(Iterables.indexOf(nums, isNegative));
            // 2
    
            // Find that number
            System.out.println(Iterables.find(nums, isNegative));
            // -3
    
            // Find all negative numbers
            System.out.println(Iterables.filter(nums, isNegative));
            // [-3, -5, -7, -8]
    
            // Are all numbers negative?
            System.out.println(Iterables.all(nums, isNegative));
            // false
    
            // Find all non-negative numbers
            System.out.println(Iterables.filter(nums, Predicates.not(isNegative)));
            // [1, 2, 4, 6]
        }
    }
    

    Summary

    • List.indexOf(Object) can be abused to find elements that satisfy a given predicate, but this is a violation the equals contract
    • Using Guava’s Predicate and higher-order functions like indexOf, find, filter, all, any etc allows you to express these operations in a much more powerfully expressive ways
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.