If you will compare, searching between List and Set, Set will be better because of the underline Hashing algorithm.
In the case of a list, in worst case scenario, contains will search till the end.
In case of Set, because of hashing and bucket, it will search only subset.
Sample use case:
Add 1 to 100_000 integer to ArrayList and HashSet.
Search each integer in ArrayList and HashSet.
Set will take 9 milliseconds where as List will take 16232 seconds.
private static void compareSetvsList(){
List<Integer> list = new ArrayList<>() ;
Set<Integer> set = new HashSet<>() ;
System.out.println("Setting values in list and set .... ");
int counter = 100_000 ;
for(int i =0 ; i< counter ; i++){
list.add(i);
set.add(i);
}
System.out.println("Checking time .... ");
long l1 = System.currentTimeMillis();
for(int i =0 ; i< counter ; i++) list.contains(i);
long l2 = System.currentTimeMillis();
System.out.println(" time taken for list : "+ (l2-l1));
for(int i =0 ; i< counter ; i++)set.contains(i);
long l3 = System.currentTimeMillis();
System.out.println(" time taken for set : "+ (l3-l2));
// for 10000 time taken for list : 123 time taken for set : 4
// for 100000 time taken for list : 16232 time taken for set : 9
// for 1000000 time taken for list : hung time taken for set : 26
}
So which better to use in inverted index. – jsingh Feb 04 '17 at 05:39