Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreaquiles/6e28b159ec1c35be830d to your computer and use it in GitHub Desktop.
Save alexandreaquiles/6e28b159ec1c35be830d to your computer and use it in GitHub Desktop.
Comparação de alternativas que utilizam recursos de diferentes versões do Java para o post: http://blog.caelum.com.br/manipulando-arquivos-com-recursos-do-java-8/

Pequena comparação (no estilo melhor de 3) de alternativas de solução do post Manipulando arquivos com recursos do Java 8 do blog da Caelum utilizando recursos de diferentes versões do Java.

Uma comparação mais robusta deveria utilizar estatística e ferramentas como jmh ou caliper.

Códigos compilados a partir da JDK 8 com a opção -source do javac para fixar as versões e executei na JRE 8.

antigão (-source 1.3 e usando pacote java.io)

real	0m5.847s
user	0m6.492s
sys 0m0.168s

com java 7 (-source 7) e usando o pacote java.nio (newBufferedReader)

real 0m5.645s
user 0m6.436s
sys 0m0.167s

com streams sequenciais do Java 8

real	0m5.634s
user	0m6.944s
sys 0m0.165s

com streams paralelas do Java 8 (.parallel() depois de Files.lines)

real	0m3.812s
user 0m11.159s
sys 0m0.597s
package cargaHorariaDosServidores;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CargaHorariaServidoresAntigaoComJavaIO {
private static final Pattern HORAS = Pattern
.compile(".*([0-9]{2}(,[0-9])? HORAS SEMANAIS|DEDICACAO EXCLUSIVA).*");
public static void main(String[] args) throws Exception {
Map agrupamento = new TreeMap();
String arquivo = System.getProperty("user.home")
+ "/Downloads/201402_Servidores/20140228_Cadastro.csv";
FileInputStream fis = new FileInputStream(arquivo);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = null;
try {
br = new BufferedReader(isr);
String linha;
while ((linha = br.readLine()) != null) {
Matcher matcher = HORAS.matcher(linha);
if (matcher.matches()) {
String horas = matcher.group(1);
Long contagem = (Long) agrupamento.get(horas);
if (contagem == null) {
agrupamento.put(horas, Long.valueOf(0));
} else {
agrupamento.put(horas,
Long.valueOf(contagem.longValue() + 1));
}
}
}
Iterator keysIt = agrupamento.keySet().iterator();
while (keysIt.hasNext()) {
String k = (String) keysIt.next();
Long v = (Long) agrupamento.get(k);
System.out.println(v + "\t" + k);
}
} finally {
if (br != null) {
br.close();
}
}
}
}
package cargaHorariaDosServidores;
import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CargaHorariaServidoresAteJava7ComJavaNIO {
private static final Pattern HORAS = Pattern
.compile(".*([0-9]{2}(,[0-9])? HORAS SEMANAIS|DEDICACAO EXCLUSIVA).*");
public static void main(String[] args) throws Exception {
Map<String, Long> agrupamento = new TreeMap<>();
Path caminho = Paths.get(System.getProperty("user.home"),
"Downloads/201402_Servidores/20140228_Cadastro.csv");
try (BufferedReader br = Files.newBufferedReader(caminho,
StandardCharsets.ISO_8859_1)) {
String linha;
while ((linha = br.readLine()) != null) {
Matcher matcher = HORAS.matcher(linha);
if (matcher.matches()) {
String horas = matcher.group(1);
Long contagem = agrupamento.get(horas);
if (contagem == null) {
agrupamento.put(horas, 0L);
} else {
agrupamento.put(horas, contagem + 1);
}
}
}
}
for (String k : agrupamento.keySet()) {
Long v = (Long) agrupamento.get(k);
System.out.println(v + "\t" + k);
}
}
}
package cargaHorariaDosServidores;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class CargaHorariaServidoresComStreamsDoJava8 {
private static final Pattern HORAS = Pattern
.compile(".*([0-9]{2}(,[0-9])? HORAS SEMANAIS|DEDICACAO EXCLUSIVA).*");
public static void main(String[] args) throws IOException {
Predicate<String> isEmpty = String::isEmpty;
Path caminho = Paths.get(System.getProperty("user.home"),
"Downloads/201402_Servidores/20140228_Cadastro.csv");
Files.lines(caminho, StandardCharsets.ISO_8859_1)
.map(linha -> {
Matcher matcher = HORAS.matcher(linha);
return matcher.matches() ? matcher.group(1) : "";
})
.filter(isEmpty.negate())
.collect(
Collectors.groupingBy(Function.identity(),
TreeMap::new, Collectors.counting()))
.forEach((k, v) -> System.out.println(v + "\t" + k));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment