Skip to content

Instantly share code, notes, and snippets.

@charlesreid1
Last active January 30, 2017 06:38
Show Gist options
  • Save charlesreid1/767ff5a74b05351619bac0acc29badc6 to your computer and use it in GitHub Desktop.
Save charlesreid1/767ff5a74b05351619bac0acc29badc6 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Timing {
public static void main(String[] args) {
System.out.println("============================================");
System.out.println(" Lists vs. Sets ");
System.out.println("============================================");
int n = 1000;
String[] labels1 = {"banana","strawberry","apple","grape","orange"};
String[] labels2 = {"banana","carrot","cabbage","lettuce","celery"};
Set<String> firstSet = getSet(labels1, n);
Set<String> secondSet = getSet(labels2, n);
List<String> firstList = getList(labels1, n);
List<String> secondList = getList(labels2, n);
double t_start = System.currentTimeMillis();
// do some stuff
double t_end = System.currentTimeMillis();
double t_duration = t_end - t_start;
}
public static Set<String> getSet( String[] labels, int n) {
Set<String> result = new HashSet<String>();
for( String s : labels ) {
for( int i=0; i<n; i++) {
// banana 1, banana 2, banana 3, etc
String label = s + " " + i;
result.add(label);
}
}
return result;
}
public static List<String> getList( String[] labels, int n) {
List<String> result = new LinkedList<String>();
for( String s : labels ) {
for( int i=0; i<n; i++) {
// banana 1, banana 2, banana 3, etc
String label = s + " " + i;
result.add(label);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment