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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:42:00+00:00 2026-06-13T06:42:00+00:00

Why is, in Collection<T> , the method boolean remove(Object o) not defined as boolean

  • 0

Why is, in Collection<T>, the method boolean remove(Object o) not defined as boolean remove(T o)?

When I have a Collection<String> list i’m sure that I don’t want to do list.remove(new Date()) for instance.

Edit: already solved: Why aren't Java Collections remove methods generic?

Question 2:

When I have a class:

class Value {
   Object getValue() {
   // return something;
   }
}

and then I want to implement a new cool list:

class CoolList<T extends Value> implements Collection<T> {
    // ...

    @Override
    public boolean remove(Object o) {
        try {
           T value = (T) o; // I mean the cast here
               removeInternal(value);
        } catch (ClassCastException e) {
           return false;
        }
    }
}

How can I do a clean cast without producing a warning (unchecked cast) and without using @SuspressWarning, is this even possible?

Edit:
Internally I want the list to be able to recover deleted items and because of that I have internally a List<T> deletedItems.
So the removeInternal() method appends the item to be deleted to the internal deletedItems list and then deletes it and because of that I need some sort of cast.

Thank you.

  • 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-13T06:42:02+00:00Added an answer on June 13, 2026 at 6:42 am

    Regarding your second question: just make removeInternal non-generic. It should use the same logic that is described in the answer to your first question: removeInternal(o) removes an object e such that (o==null ? e==null : o.equals(e)) is true. (If you implement logic that is not equivalent to this, you are violating the contract for the Collection interface.) There’s no need for a generic removeInternal.

    EDIT Here’s an example of how one might implement a non-generic removeInternal:

    private boolean removeInternal(Object o) {
        for (int i = 0; i < currentSize; ++i) {
            T elt = get(i);
            if (o == null ? elt == null : o.equals(elt)) {
                removeElementAtPosition(i);
                deletedItems.add(elt);
                return true;
            }
            return false;
        }
    }
    

    This assumes that removeElementAt(int) will correctly transfer the ith item to deletedItems and adjust the internal count of items. It’s important to note that the element that was removed is not necessarily the same object as is passed in the argument; it just the first element that satisfies the equality predicate.

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

Sidebar

Related Questions

I'd like to have an object that implements both the Map and the List
My case is that i have a method that takes Collection and i have
I'm new to Reactive Extensions. I have objects collection and call a method for
remove() method without any argument removes all documents inside the collection. $this->db->$collection->remove(); But how
I have a method with a nested foreach collection (iterate a set of objects
I'd like to create an extension method to the IDictionary collection Exception.Data that allows
I am having problems using the update method of scala.collection.immutable.HashMap.I don't see the reason
I'm working with an observable collection of a Job class I have defined. I
I have a method <T> void f (Collection<T> c, T t) Alas, sometimes I
Please help me complete my isEmpty method: public static boolean isEmpty(Object test){ if (test==null){

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.