Skip to content

Instantly share code, notes, and snippets.

@NathanPB
Last active December 20, 2018 03:24
Show Gist options
  • Save NathanPB/c23ac5039510cff88135eea708cf9014 to your computer and use it in GitHub Desktop.
Save NathanPB/c23ac5039510cff88135eea708cf9014 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
import java.util.stream.Collectors;
public class ExercicioMap {
public static void main(String[] args) {
ArrayList<List<String>> csv = new ArrayList<>();
for(String line : readFile()){
csv.add(Arrays.asList(line.split(",")));
}
Set<String> locaisVisitados = new HashSet<>();
for(List<String> line : csv){
locaisVisitados.add(line.get(0));
}
Map<String, Double> dinheiroGasto = new HashMap<>();
for(List<String> line : csv){
if(!dinheiroGasto.containsKey(line.get(0))){
dinheiroGasto.put(line.get(0), Double.parseDouble(line.get(3)));
} else {
dinheiroGasto.put(line.get(0), dinheiroGasto.get(line.get(0))+Double.parseDouble(line.get(3)));
}
}
System.out.printf("Locais Visitados: %s\n", locaisVisitados);
System.out.printf("Dinheiro Gasto: %s", dinheiroGasto);
}
public static String[] readFile() {
try {
BufferedReader br = new BufferedReader(new FileReader("viagens.csv"));
return br.lines().collect(Collectors.toList()).toArray(new String[0]);
} catch (Exception ex){
ex.printStackTrace();
}
return null;
}
}
import java.io.BufferedReader
import java.io.FileReader
import kotlin.streams.toList
fun main(args: Array<String>) {
BufferedReader(FileReader("viagens.csv")).lines().map { it.split(",") }.toList()
.let { csv ->
csv.map { it[0] }.toSet().also { println("Locais Visitados: $it") }
mutableMapOf<String, Double>().also { map ->
csv.forEach {
map[it[0]] = (map[it[0]] ?: 0.0) + it[3].toDouble()
}
}.also { println("Dinheiro Gasto: $it") }
}
}
Maceió 11/12/2017 7 4000
Campo Grande 11/12/2016 1 100
Bonito 11/12/2014 5 1000000
Maceió 11/12/2013 5 4000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment