Created
August 2, 2018 16:54
-
-
Save StFS/65cd3524590be75bd356662f3de9bee8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
List<String> toollist = // a list of tool ids | |
Set<String, String> mapToolId = // a map convert ids to fully qualified names | |
Set<String> fqToolList = toollist.stream() | |
.peek( n -> { if (!mapToolId.containsKey(n.trim())) logger.warn("Ignoring unexpected tool id value '{}'", n); }) | |
.filter( n -> mapToolId.containsKey(n.trim()) ) | |
.map( n -> mapToolId.get(n.trim())) // map shorthand tool names to fully qualified tool names | |
.collect( Collectors.toCollection(HashSet::new) ); // make sure we have a mutable set |
Hvað með þetta? Jújú, það er heilt nýtt fall hérna, en þú getur fært það einhvert annað í filtera-safn eða hvað sem þú myndir vilja gera. Gerir .filter() aðeins betra
List<String> toollist = new ArrayList<>();
Map<String, String> mapToolId = new HashMap<>();
Set<String> fqToolList = toollist.stream()
.filter( contains( mapToolId ) )
.map( n -> mapToolId.get( n.trim() ) ) // map shorthand tool names to fully qualified tool names
.collect( Collectors.toCollection( HashSet::new ) ); // make sure we have a mutable set
public Predicate<String> contains( final Map<String, String> map ) {
return n -> {
if( map.containsKey( n.trim() ) ) {
return true;
}
else {
logger.warn( "Ignoring unexpected tool id value '{}'", n );
return false;
}
};
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
önnur leið er svona:
Kosturinn við þetta er að conditionið er ekki tvítekið... en gallinn er að þetta er ljótt og gerir filterinn mun ólæsilegri