0

What else i can use instead of contains method of collection to find out the similar entry in the collection.As contains is a very heavy(time consuming) method to use.

Right now i am using contains like this

if (EntityTree.setMobileEnablerTxnList.contains(iMobileEnablerTxnList)) {

 }

Here setMobileEnablerTxnList is a set.

Java_Alert
  • 1,159
  • 6
  • 24
  • 50
  • 2
    *As contains is a very heavy(time consuming) method to use* what makes you think this ? On e.g. a `HashSet` this operation is rather fast. – Robin Aug 30 '12 at 06:51
  • @Robin I consider your comment to be the correct answer - ie use a `HashSet`. Consider making it an answer – Bohemian Aug 30 '12 at 06:53
  • @Robing because internally it will check each and every node to find out the right solution. – Java_Alert Aug 30 '12 at 06:54
  • @Sunil no it won't, only those in the same bucket (at least if it's a `HashSet`, you haven't said what implementation of `Set` you're using). – Frank Pavageau Aug 30 '12 at 07:43
  • @FrankPavageau yes,Sorry for incomplete information.I am using hashset here and have implemented hashcode as well as equals method. – Java_Alert Aug 30 '12 at 12:03

2 Answers2

3

The contains method is not necessarily very heavy (time consuming). For example if you use a HashSet it is rather fast. For a HashSet it will calculate the hashCode of the object, and only go over the objects in the corresponding 'bucket'. It will certainly not go over all elements (or you would have a very poor implementation of the hashCode method).

This can be seen in the source code of the HashSet#contains method, which ultimately calls the following code in HashMap:

final Entry<K,V> getEntry(Object key) {
    int hash = (key == null) ? 0 : hash(key.hashCode());
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
         e != null;
         e = e.next) {
        Object k;
        if (e.hash == hash &&
            ((k = e.key) == key || (key != null && key.equals(k))))
            return e;
    }
    return null;
}

This clearly shows it loops only over a limited set of objects.

See also this question for more information about a HashMap (which is used internally by a HashSet)

Last piece of advise: if you have performance problems, use a profiler to see where the actual bottleneck is located. I doubt it will be in the contains call.

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
0

The implementation of List.contains() iterates through the list checking if the target.equals(element).

If possible, put your list into a HashSet, whose contains() method always returns very quickly.

Using a HashSet would only be of benefit if you can reuse it, because the cost of filling it would tpyically be greater than executing List.contains().

You code code look like this:

Set set = new HashSet(EntityTree.setMobileEnablerTxnList);

if (set.contains(iMobileEnablerTxnList)) {

You would have to make sure that MobileEnablerTxnList implements hashCode() and equals() properly.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I know it is confusing, but in the question is already stated that ** setMobileEnablerTxnList is a set** (poor naming BTW) – Robin Aug 30 '12 at 07:12
  • 1
    @Robin I'm beginning to think that the issue is he has `Set`, and that the List hasn't implemented `equals()` or `hashCode()` properly (to reflect its *contents*) – Bohemian Aug 30 '12 at 07:19
  • Even if the collection isn't already a `Set`, the copy in a `Set` probably only makes sense if it's done once and then used for multiple `contains()` calls. Otherwise you're already iterating on the original collection once to fill the `Set`, calling `hashCode()` on the items, then iterating on the items in the same bucket to call `equals()`. – Frank Pavageau Aug 30 '12 at 07:48
  • yes,I m using hashset here,and implemented hascode and equals method also. – Java_Alert Aug 30 '12 at 11:59
  • @Robin Thanks a lot robin for highlighting issues,in future will be taking care of them. – Java_Alert Aug 30 '12 at 12:00