functional programming - Converting List To Maps in Java 8 -


i loading log file input stream , need aggregate processing on matching values on each line, need store duplicates while saving lines in multimap, finding trouble collecting below stream stored list multimap<string, list<string>>

try (stream<string> stream = files.lines(paths.get(infilename))) {     list<string> matchedvalues = stream                     .flatmap(s -> multipatternspliterator.matches(s, p1))                     .map(r -> r.group(1))                     .collect(collectors.tolist());      matchedvalues.foreach(system.out::println); } 

how convert same store values in map duplicate values.

it's hard want, given need "log line <retimestamp, rehostname, reservicetime>" stored "map<<retimestamp>, <retimestamp, rehostname, reservicetime>" i'd go with:

        map<string, list<string>> matchedvalues = stream                 .flatmap(multipatternspliterator::matches)                 .map(r -> arrays.aslist(r.group(1), r.group(2), r.group(3)))                 .collect(collectors.tomap(                         groups -> groups.get(0),                         function.identity())); 

if key (i.e. timestamp) can duplicated, use (list)multimap give list of lists per key:

        listmultimap<string, list<string>> matchedvalues = stream                 .flatmap(multipatternspliterator::matches)                 .map(r -> arrays.aslist(r.group(1), r.group(2), r.group(3)))                 .collect(toimmutablelistmultimap(                         groups -> groups.get(0),                         function.identity())); 

i easier if showed example input , output.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -