list - Java Collections containsAll Weired Behavior -


i have following code , using superlist , sublist , want check sublist sublist of superlist.

my objects not implement hashcode or equals methods. have created similar situation in test. when run test result show big performance difference between results jdk collection , common collections.after running test getting following output.

time lapsed java collection api 8953 milliseconds & result true time lapsed commons collection api 78 milliseconds & result true

my question why java collection , slow in processing containsall operation. doing wrong there? have no control on collection types getting legacy code. know if use hashset superlist big performance gains using jdk containsall operation, unfortunately not possible me.

package com.mycompany.tests;  import java.util.arraylist; import java.util.collection; import java.util.hashset;  import org.apache.commons.collections.collectionutils; import org.junit.before; import org.junit.test;  public class collectioncomparison_unittest {      private collection<myclass> superlist = new arraylist<myclass>();     private collection<myclass> sublist = new hashset<myclass>(50000);      @before     public void setup() throws exception {          (int = 0; < 50000; i++) {             myclass myclass = new myclass(i + "a string");             superlist.add(myclass);         sublist.add(myclass);     } }  @test public void testit() {     long starttime = system.currenttimemillis();     boolean issublist = superlist.containsall(sublist);     system.out.println("time lapsed java collection api "             + (system.currenttimemillis() - starttime)             + " milliseconds & result " + issublist);      starttime = system.currenttimemillis();     issublist = collectionutils.issubcollection(sublist, superlist);     system.out.println("time lapsed commons collection api "             + (system.currenttimemillis() - starttime)             + " milliseconds & result " + issublist); } 

}

class myclass { string mystring;

myclass(string mystring) {     this.mystring = mystring; }  string getmystring() {     return mystring; } 

}

different algorithms:

arraylist.containsall() offers o(n*n), while collectionutils.issubcollection() offers o(n+n+n).


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -