Skip to content

Instantly share code, notes, and snippets.

@guilhermesilveira
Created March 9, 2012 17:43
Show Gist options
  • Save guilhermesilveira/2007714 to your computer and use it in GitHub Desktop.
Save guilhermesilveira/2007714 to your computer and use it in GitHub Desktop.
biased? imagina...
import java.util.*;
void getTimeDiffGroupedByCat() {
Scanner sc = new Scanner(new File("textfile.txt"));
Map<String, Long> lastCatTime = new HashMap<String, Long>();
TreeMap<String, Vector<Long>> catTimeDiffs = new TreeMap<String, Vector<Long>>();
while (sc.hasNext()) {
Long thisTime = sc.nextLong();
String category = sc.next();
Long oldCatTime = lastCatTime.put(category, thisTime);
if(oldCatTime != null) {
if(catTimeDiffs.get(category) == null) {
catTimeDiffs.put(category, new Vector<Long>());
}
catTimeDiffs.get(category).add(thisTime - oldCatTime);
}
}
for(Map.Entry<String, Vector<Long>> thisEntry: catTimeDiffs.entrySet()) {
System.out.println("Category:" + thisEntry.getKey());
Iterator it = thisEntry.getValue().iterator();
while(it.hasNext()) {
System.out.println(it.next());
if(it.hasNext())
System.out.print(", ");
}
}
}
@peas
Copy link

peas commented Mar 9, 2012

use new Scanner(new File(....))

and use a guava MultiMap instead of a bunch of ifs to populate a new Vecotr.

@peas
Copy link

peas commented Mar 9, 2012

use new Scanner(new File(....))

and use a guava MultiMap instead of a bunch of ifs to populate a new Vecotr.

@eribeiro
Copy link

eribeiro commented Mar 9, 2012

E para que ele usou um Vector??? Não era melhor usar um ArrayList, ou, se necessário uma lista Thread Safe então era melhor usar um Collections.synchronizedList.

@gustavopinto
Copy link

collections.synchronized* são pseudo-thread-safe. o melhor caso era usar o pacote java.util.concurrent.

@peas
Copy link

peas commented Mar 13, 2012 via email

@eribeiro
Copy link

A questão é: neste exemplo precisa ser thread-safe??? A meu ver não ...

@gustavopinto
Copy link

gustavopinto commented Mar 13, 2012 via email

@guilhermesilveira
Copy link
Author

guilhermesilveira commented Mar 13, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment