Skip to content

Instantly share code, notes, and snippets.

@trishagee
Last active January 4, 2017 14:17
Show Gist options
  • Save trishagee/bf5ea884bef0f9ba348118f419b1a6f0 to your computer and use it in GitHub Desktop.
Save trishagee/bf5ea884bef0f9ba348118f419b1a6f0 to your computer and use it in GitHub Desktop.
Refactoring to pipelines, Java
public static class CityAndPhone {
private String city;
private String phone;
public CityAndPhone(String city, String phone) {
this.city = city;
this.phone = phone;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CityAndPhone that = (CityAndPhone) o;
if (city != null ? !city.equals(that.city) : that.city != null) {
return false;
}
return phone != null ? phone.equals(that.phone) : that.phone == null;
}
@Override
public int hashCode() {
int result = city != null ? city.hashCode() : 0;
result = 31 * result + (phone != null ? phone.hashCode() : 0);
return result;
}
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
boolean firstLine = true;
List<CityAndPhone> result = new ArrayList<>();
for (String line : lines) {
if (firstLine) {
firstLine = false;
continue;
}
if (Objects.equals("", line.trim())) {
continue;
}
String[] record = line.split(",");
if (Objects.equals("China", record[1].trim())) {
result.add(new CityAndPhone(record[0].trim(), record[2].trim()));
}
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
boolean firstLine = true;
List<CityAndPhone> result = new ArrayList<>();
List<String> loopItems = Arrays.stream(lines)
.collect(Collectors.toList())
;
for (String line : loopItems) {
if (firstLine) {
firstLine = false;
continue;
}
if (Objects.equals("", line.trim())) {
continue;
}
String[] record = line.split(",");
if (Objects.equals("China", record[1].trim())) {
result.add(new CityAndPhone(record[0].trim(), record[2].trim()));
}
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
List<CityAndPhone> result = new ArrayList<>();
List<String> loopItems = Arrays.stream(lines)
.skip(1)
.collect(Collectors.toList())
;
for (String line : loopItems) {
if (Objects.equals("", line.trim())) {
continue;
}
String[] record = line.split(",");
if (Objects.equals("China", record[1].trim())) {
result.add(new CityAndPhone(record[0].trim(), record[2].trim()));
}
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
List<CityAndPhone> result = new ArrayList<>();
List<String> loopItems = Arrays.stream(lines)
.skip(1)
.filter(line -> !Objects.equals("", line.trim()))
.collect(Collectors.toList())
;
for (String line : loopItems) {
String[] record = line.split(",");
if (Objects.equals("China", record[1].trim())) {
result.add(new CityAndPhone(record[0].trim(), record[2].trim()));
}
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
List<CityAndPhone> result = new ArrayList<>();
List<String[]> loopItems = Arrays.stream(lines)
.skip(1)
.filter(line -> !Objects.equals("", line.trim()))
.map(line -> line.split(","))
.collect(Collectors.toList())
;
for (String[] line : loopItems) {
String[] record = line;
if (Objects.equals("China", record[1].trim())) {
result.add(new CityAndPhone(record[0].trim(), record[2].trim()));
}
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
List<CityAndPhone> result = new ArrayList<>();
List<String[]> loopItems = Arrays.stream(lines)
.skip(1)
.filter(line -> !Objects.equals("", line.trim()))
.map(line -> line.split(","))
.filter(record -> Objects.equals("China", record[1].trim()))
.collect(Collectors.toList())
;
for (String[] line : loopItems) {
String[] record = line;
result.add(new CityAndPhone(record[0].trim(), record[2].trim()));
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
List<CityAndPhone> result = new ArrayList<>();
List<CityAndPhone> loopItems = Arrays.stream(lines)
.skip(1)
.filter(line -> !Objects.equals("", line.trim()))
.map(line -> line.split(","))
.filter(record -> Objects.equals("China", record[1].trim()))
.map(record -> new CityAndPhone(record[0].trim(), record[2].trim()))
.collect(Collectors.toList())
;
for (CityAndPhone line : loopItems) {
CityAndPhone record = line;
result.add(record);
}
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
List<CityAndPhone> result = Arrays.stream(lines)
.skip(1)
.filter(line -> !Objects.equals("", line.trim()))
.map(line -> line.split(","))
.filter(record -> Objects.equals("China", record[1].trim()))
.map(record -> new CityAndPhone(record[0].trim(), record[2].trim()))
.collect(Collectors.toList())
;
return result;
}
public static List<CityAndPhone> acquireData(String input) {
String[] lines = input.split("\n");
return Arrays.stream(lines)
.skip(1)
.filter(line -> !Objects.equals("", line.trim()))
.map(line -> line.split(","))
.filter(fields -> Objects.equals("China", fields[1].trim()))
.map(fields -> new CityAndPhone(fields[0].trim(), fields[2].trim()))
.collect(Collectors.toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment