0

Why does it take more time for insertion in the same SET the first time and much less time when I try to insert the second time. Why there is a change in the behavior for the same code? The Employee class contains two fields and generates a hash code. This is of no use here but related to a different program. What I am trying to do is to calculate the complexity of various Collections for better performance on CRUD operations. But when I first add 200000 elements into the HashSet it takes more time. When the same code runs again it takes much lesser time. I don't know how it is working internally. Any help is appreciated.

IN THE OUTPUT THE HASHMAP CORRESPONDS TO THE HASHSET ONLY. REFER TO THE CODE.

class Employee {
    String name;
    int age;
    
    @Override
    public int hashCode() {
        return age;
    }
}

public class NonDuplicateObjects {
    private static final int COLLECTION_SIZE = 2000000;
    private static final Object DUMMY =`enter code here` new Object();
    public static void main(String[] args) {    
        Employee e = new Employee();
        e.age = 0;
        e.name = "Rahim";
        Employee[] eArr = new Employee[COLLECTION_SIZE];
        ArrayList<Employee> al = new ArrayList<>();
        HashMap<Employee, Object> hm = new HashMap<>(COLLECTION_SIZE);
        HashSet<Employee> hset = new HashSet<>(COLLECTION_SIZE);
        long startTime;
        long timeDiff;
        
        System.out.println("PROGRAM FOR TIME COMPARISION ON THE BASIS OF CRUD OPERATIONS");
        System.out.println("PERFORMING ADDITION/INSERTION:");
    
        //  Insertion in an array
        startTime = System.currentTimeMillis();
        for (int i=0; i<COLLECTION_SIZE; ++i)
        {
            ++e.age;
            eArr[i] = e;
        }
        timeDiff = System.currentTimeMillis() - startTime;
        System.out.println("Time taken by array: " + timeDiff);
        System.out.println("SIZE: " + eArr.length);
        
        //  Insertion in a ArrayList
        e.age = 0;
        startTime = System.currentTimeMillis();
        for (int i=0; i<COLLECTION_SIZE; ++i) {
            ++e.age;
            al.add(e);
        }
        timeDiff = System.currentTimeMillis() - startTime;
        System.out.println("Time taken by ArrayList: " + timeDiff);
        System.out.println("SIZE: " + al.size());
    
        //  Insertion in a HashSet
        e.age = 0;
        startTime = System.currentTimeMillis();
        for (int i=0; i<COLLECTION_SIZE; ++i) {
            ++e.age;
            hset.add(e);
        }
        timeDiff = System.currentTimeMillis() - startTime;
        System.out.println("Time taken by HashMap: " + timeDiff);hset.clear();
        System.out.println("SIZE: " + hset.size());
        //  Insertion in a HashSet
        e.age = 0;
        startTime = System.currentTimeMillis();
        for (int i=0; i<COLLECTION_SIZE; ++i) {
            ++e.age;
            hset.add(e);
        }
        timeDiff = System.currentTimeMillis() - startTime;
        System.out.println("Time taken by HashSet: " + timeDiff);
        System.out.println("SIZE: " + hset.size());
    }
}

OUTPUT OF THE PROGRAM. THIRD TIME OUTPUT IS FOR HASHSET ONLY AND NOT HASHMAP. REFER TO THE CODE

shade0
  • 1
  • 2

0 Answers0