Created
March 9, 2012 17:43
-
-
Save guilhermesilveira/2007714 to your computer and use it in GitHub Desktop.
biased? imagina...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(", "); | |
} | |
} | |
} |
use new Scanner(new File(....))
and use a guava MultiMap instead of a bunch of ifs to populate a new Vecotr.
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.
collections.synchronized* são pseudo-thread-safe. o melhor caso era usar o pacote java.util.concurrent.
só nao é thread safe o iterator.
mas tambem prefiro as collections do JUC.
##
Paulo Silveira
Caelum | Ensino e Inovação
www.caelum.com.br
2012/3/13 Gustavo
[email protected]:
… collections.synchronized\* são pseudo-thread-safe. o melhor caso era usar o pacote java.util.concurrent.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2007714
A questão é: neste exemplo precisa ser thread-safe??? A meu ver não ...
Eh dificil dizer olhando so esse trecho de codigo..
Em 13/03/2012 14:58, "Edward Ribeiro" <
[email protected]>
escreveu:
… A questo : neste exemplo precisa ser thread-safe??? A meu ver no ...
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2007714
Opa pessoal,
Nao precisa de multi thread nao pq os objetos sao instanciados e morrem
dentro do metodo, entao eles nao sao acessados por duas threads.
Abrao!
On Tuesday, March 13, 2012, Gustavo wrote:
Eh dificil dizer olhando so esse trecho de codigo..
Em 13/03/2012 14:58, "Edward Ribeiro" <
***@***.*** javascript:;>
escreveu:
> A questo : neste exemplo precisa ser thread-safe??? A meu ver no ...
> ---
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/2007714
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2007714
##
Guilherme Silveira
Caelum | Ensino e Inovao
http://www.caelum.com.br/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use new Scanner(new File(....))
and use a guava MultiMap instead of a bunch of ifs to populate a new Vecotr.