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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:03:00+00:00 2026-06-17T16:03:00+00:00

In ArrayList API add() takes an argument of generic parameter type, but contains() and

  • 0

In ArrayList API add() takes an argument of generic parameter type, but contains() and indexOf() take arguments of type Object.

public class ArrayList<E> ...{
     public boolean add(E e);  
     public boolean contains(Object o);
     public int indexOf(Object o);
     ....
}

Java Doc for ArrayList

So i am just wondering if its something to do with Generics or it’s design in-consistency ?

I looked at Openjdk implementation but couldn’t find any specific reason for this.

  • 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-17T16:03:01+00:00Added an answer on June 17, 2026 at 4:03 pm

    What the API is saying is that:

    1. You can’t add() anything that isn’t an E;
    2. You are, however, allowed to go searching for things that are not E (but that could compare equal to an instance of E).

    Consider the following example:

    public class Main {
        public static class Key {
            private final int k;
            public Key(int k) {
                this.k = k;
            }
            @Override
            public boolean equals(Object obj) {
                if (!(obj instanceof Key)) {
                    return false;
                }
                Key rhs = (Key)obj;
                return k == rhs.k;
            }
            @Override
            public int hashCode() {
                //...
                return 0;
            }
        }
        public static class Data extends Key {
            private final int d;
            public Data(int k, int d) {
                super(k);
                this.d = d;
            }
        }
        public static void main(String[] args) {
            List<Data> l = new ArrayList<Data>();
            l.add(new Data(123, 456));
            l.add(new Data(42, 24));
            System.out.println(l.contains(new Key(789)));
            System.out.println(l.contains(new Key(123)));
            System.out.println(l.contains(new Key(42)));
        }
    }
    

    The last three lines wouldn’t compile if contains() were restricted to taking Data.

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

Sidebar

Related Questions

Using the SharePoint API (the one with the assemblies you add, but requires you
For the method add of the ArrayList Java API states: The add operation runs
This is the code ArrayList<MyObject> list = new ArrayList<MyObject>(); list.add(new MyObject()); Intent intent =
Here's my current situation: import java.util.ArrayList; import java.util.Scanner; import com.sun.xml.internal.ws.api.pipe.NextAction; import static java.lang.System.*; public
The code is pretty simple: List lst1 = new ArrayList(){{add(1);}}; Iterator it = lst1.iterator();
Trying to construct a helper class that will return an arraylist, but I'm getting
I would like to ADD a ACL to a Google Storage object using the
I make Web Application on Google App Engine and I use AsyncURLFetch Api. But,
I pretty much copy and pasted the code from the API PDF, but am
Tried on Dropbox forum but no luck: I'm reaching Dropbox API from Android, i

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.