![]() Of course, trying to merge maps with duplicate keys will result in an exception.Ī little known version of the same method accepts athird lambda parameter, known as the "merger". The collector would use the keys and values of the existing maps to create entries in the resulting map. What happens here is rather straightforward. This key-value pair would then serve for creating a new entry in the final map.Ĭombining the first two points, our resulting Map instance would so far look like this: Map totalVisitCounts = ncat(visitCounts1.entrySet().stream(), visitCounts2.entrySet().stream()) The first lambda is supposed to extract and return a key, whereas the second lambda is supposed to extract and return a value from the same entry. Upon iterating over the stream, both lambda parameters get called and passed the current stream entry as an input parameter. The default implementation of Collectors.toMap() takes two lambda parameters: public static Collector> toMap(FunctionMapCollector keyMapper, FunctionFunctionMapCollector valueMapper) I will use Collectors.toMap() for my purposes. Most common collectors reside in the factory class. It takes a given stream, applies all transformations (mostly from map, filter, etc) and outputs an instance of a common Java colelction type: List, Set, Map, etc. Collecting in Java 8 streams is a final operation. There are multiple ways of doing this but my preferred one is using ncat() and passing the entry sets of all maps as parameters: ncat(visitCounts1.entrySet().stream(), visitCounts2.entrySet().stream()) ![]() The Solutionįirst, we need to combine all maps into a unified Stream. We want to merge those two maps using streams, such that where the same key (user ID) appears in both, we want to take the sum (total) of user visits across all parts of the system. The key of each map is the ID of a certain user, and the value is the number of user visits to given part of the system. Suppose, we have two maps visitCounts1, visitCounts2 : Map where the maps represent the results of different search queries. Moreover, it reduced the need of having to store intermediate state in variables, eliminating the possibility of some other code corrupting that state at runtime. Java 8 brought with itself the concept of streams, and opened the door of possibilities for solving such problems in a declarative functional manner. Yet, is this the best way to do it? Though guaranteed to work, such solutions could easily get out of hand and become incomprehensible at a later point of development. With a few variables to store state, and a couple of nested loops, and several if-else statements, even people new to Java could program a solution within minutes. In most imperative programming languages, including Java, this is a trivial problem. Often, we're faced with situations, in which we have to merge multiple Map instances into a single one, and guarantee that key duplicates are handled properly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |