Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Collection.Contains() use to check for existing objects?

I have a strongly typed list of custom objects, MyObject, which has a property Id, along with some other properties.

Let's say that the Id of a MyObject defines it as unique and I want to check if my collection doesn't already have a MyObject object that has an Id of 1 before I add my new MyObject to the collection.

I want to use if(!List<MyObject>.Contains(myObj)), but how do I enforce the fact that only one or two properties of MyObject define it as unique?

I can use IComparable? Or do I only have to override an Equals method? If so, I'd need to inherit something first, is that right?

like image 475
topwik Avatar asked Sep 03 '10 17:09

topwik


People also ask

How do you use contains in collections?

Collection contains() method in Java with ExamplesThis method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false. Parameters: This method accepts a mandatory parameter element of type Object which is to be checked in this collection.

Which collection contains method?

Java Collection contains() Method The contains() method of Java Collection Interface returns a Boolean value 'true', if it contains the specified element in the collection.

How does contains work in Java for Object?

The contains(Object o) method of Set From Java Doc, the contains() method returns true if and only if this set contains an element e such that (o==null ? e==null : o. equals(e)). So the contains() method actually use equals() method to check equality.

Which method can be used to check a particular value exists in the collection?

The contains() method is used to check if a particular value exists.


1 Answers

List<T>.Contains uses EqualityComparer<T>.Default, which in turn uses IEquatable<T> if the type implements it, or object.Equals otherwise.

You could just implement IEquatable<T> but it's a good idea to override object.Equals if you do so, and a very good idea to override GetHashCode() if you do that:

public class SomeIDdClass : IEquatable<SomeIDdClass> {     private readonly int _id;     public SomeIDdClass(int id)     {         _id = id;     }     public int Id     {         get { return _id; }     }     public bool Equals(SomeIDdClass other)     {         return null != other && _id == other._id;     }     public override bool Equals(object obj)     {         return Equals(obj as SomeIDdClass);     }     public override int GetHashCode()     {         return _id;     } } 

Note that the hash code relates to the criteria for equality. This is vital.

This also makes it applicable for any other case where equality, as defined by having the same ID, is useful. If you have a one-of requirement to check if a list has such an object, then I'd probably suggest just doing:

return someList.Any(item => item.Id == cmpItem.Id); 
like image 162
Jon Hanna Avatar answered Oct 12 '22 04:10

Jon Hanna