Last active
June 21, 2018 15:21
-
-
Save lWeRl/0b9d6ce2e9b650a06377e0eb67cb4592 to your computer and use it in GitHub Desktop.
This file contains 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
package puzz; | |
import java.util.*; | |
import static java.util.stream.Collectors.*; | |
public class Puzz1 { | |
public static void main(String[] args) { | |
final Puzz1 puzz = new Puzz1(); | |
puzz.test(); | |
} | |
Source source = new Source(); | |
Target target = new Target(); | |
public static class FeatureData { | |
private String name; | |
private String data; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getData() { | |
return data; | |
} | |
public void setData(String data) { | |
this.data = data; | |
} | |
} | |
public static class Classification { | |
private List<FeatureData> features = new ArrayList<>(); | |
public List<FeatureData> getFeatures() { | |
return features; | |
} | |
public void setFeatures(List<FeatureData> features) { | |
this.features = features; | |
} | |
} | |
public static class Source { | |
private List<Classification> classifications = new ArrayList<>(); | |
public List<Classification> getClassifications() { | |
return classifications; | |
} | |
public void setClassifications(List<Classification> classifications) { | |
this.classifications = classifications; | |
} | |
} | |
public static class Target { | |
private Map<String, List<String>> features; | |
public Map<String, List<String>> getFeatures() { | |
return features; | |
} | |
public void setFeatures(Map<String, List<String>> features) { | |
this.features = features; | |
} | |
} | |
private List<String> getStringValues(FeatureData data) { | |
return Arrays.asList(data.getData().split(" ")); | |
} | |
private List<String> mergeLists(List<String> a, List<String> b) { | |
final ArrayList<String> result = new ArrayList<>(a); | |
result.addAll(b); | |
return result; | |
} | |
void test() { | |
Optional.ofNullable(source.getClassifications()) | |
.map(classifications -> classifications.stream() | |
.flatMap(classification -> classification.getFeatures().stream()) | |
.collect(toMap(FeatureData::getName, this::getStringValues))) | |
.ifPresent(target::setFeatures); | |
Optional.ofNullable(source.getClassifications()) | |
.map(classifications -> classifications.stream() | |
.flatMap(classification -> classification.getFeatures().stream()) | |
.collect(toMap(FeatureData::getName, this::getStringValues, this::mergeLists, HashMap::new))) | |
.ifPresent(target::setFeatures); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment