Simple Map-reduce Java Example

Source: Internet
Author: User

The

requirement is the value of the map of the list that removes a key that is long and value is the integer of the element.

public class Test1 {public static void main (string[] args) {map<long, integer> Map = Maps.newhashmapwithexpe
    Ctedsize (5);
    Map.put (1l, Objects.hashcode (Lists.newarraylist (1, 2, 3));
    Map.put (2l, Objects.hashcode (Lists.newarraylist (2, 3, 4));
    Map.put (3l, Objects.hashcode (Lists.newarraylist (1, 2, 3));
    Map.put (4l, Objects.hashcode (Lists.newarraylist (1, 2, 5));
    Map.foreach ((k, v)-> System.out.println (k + "-" + V));
    System.out.println ("---------------------------------"); System.out.println (Map.entryset (). Stream (). Collect (Groupingby (Map.entry::getvalue)). values (). Stream (
    ). Map (v-> v.get (0). Getkey ()). Collect (Collectors.tolist ()));
    System.out.println ("---------------------------------");
            System.out.println (Map.entryset (). Stream (). Collect (Groupingby (Map.entry::getvalue)). values (). Stream () . Map (e-> e.stream (). Reduce (e1, E2)-> E1). Get (). Getkey ()). Collect (collectors.toList ())); }
}

This uses the JAVA8 encoding style:
1. First initialize a key is a long, value is a list of map
2. Code is divided into two ways, a map operation, the output to remove duplicate key value, the second is after the map operation, Output duplicate key value after the reduce operation.
3. First obtain the map of the EntrySet stream, and then access a collector, the collector inside the Groupingby operation, Groupingby is the Java8 method, in the map value grouping, the map back to the map, The key of the new map is the duplicate value of the old map, and the value of the new map is the list of the key of the new map corresponding to the old map, a bit around. The value of each element of the new map is the key of the distinct value, which is the 1l and 3l of (1,2,3) in the corresponding code. Then we take any of the value of each new map.
4. Get the values of the new map, make a stream, take the first value of each element and collect it as a list, which is to remove the key value of the duplicate old map.
5. By means of reduce, after obtaining the values of the new map, make a stream in the map operation, call the reduce operation, see the reduce source is visible, the incoming parameter is a two-element operator, the following open java8 lambda expression. Binary operations pass in two variables of the same type, outputting a return value of the same type. Here the two-dollar operation is to pass in the E1,e2, and then return to E1, very simple logic. Of course the logic here can also return E2, because it is arbitrary to select an element to return.

system.out.println (Map.entryset (). Stream (). Collect (Groupingby (Map.entry::getvalue)). Values (). Stream (). Map (e-> {return e.stream (). Reduce (New Binaryoperator<map.entry<lon G, integer>> () {@Override public map.entry<long, integer> apply (map.entry<
                Long, Integer> E1, Map.entry<long, integer> E2) {return e1;
            }). Get (). Getkey (); }). Collect (Collectors.tolist ()); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.