Skip to content

Instantly share code, notes, and snippets.

@skuro
Created March 17, 2016 13:47
Show Gist options
  • Select an option

  • Save skuro/b1f3b2f85f6ed1c33f04 to your computer and use it in GitHub Desktop.

Select an option

Save skuro/b1f3b2f85f6ed1c33f04 to your computer and use it in GitHub Desktop.
package io.sytac;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created by skuro on 17/03/16.
*/
public class Start {
public static void main(String[] args) {
Start start = new Start();
start.perform();
}
private void perform() {
List<Tupel> list = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get("/Users/skuro/Development/Sytac/fp/foo/src/main/resources/file.txt"))){
Integer lineNumber=stream.limit(1).mapToInt(Integer::parseInt).sum();
Stream<String> finalStream = Files.lines(Paths.get("/Users/skuro/Development/Sytac/fp/foo/src/main/resources/file.txt"));
list = finalStream.skip(1).limit(lineNumber)
.map(String::trim)
.map(line -> {
String clientId = line.split("\\s{6}")[0];
String miles = line.split("\\s{6}")[1];
Integer milesValue;
if(miles.endsWith("|")){
milesValue = Integer.parseInt(miles.replace("|","0"));
milesValue *= -1;
} else {
milesValue = Integer.parseInt(miles);
}
return new Tupel(clientId, milesValue);
})
.collect(Collectors.groupingBy(Tupel::getId)).entrySet().stream()
.map(entry->{
List<Tupel> tuples=entry.getValue();
Integer sum=tuples.stream().mapToInt(tupel->tupel.getMiles()).sum();
return new Tupel(entry.getKey(),sum);
}).collect(Collectors.toList());
}
catch(IOException ioe){
ioe.printStackTrace();
}
list.forEach(System.out::println);
}
class Tupel {
private final String id;
private final Integer miles;
public Tupel(final String id, final Integer miles) {
this.id = id;
this.miles = miles;
}
public String getId() {
return id;
}
public Integer getMiles() {
return miles;
}
@Override
public String toString() {
return "Tupel{" +
"id='" + id + '\'' +
", miles=" + miles +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment