public static < K , V > Map < K , List < V > > groupByKey ( List < Pair < K , V > > listInput ) { Map < K , List < V > > ret = new HashMap <> ( ) ; for ( Pair < K , V > pair : listInput ) { List < V > currList = ret . get ( pair . getFirst ( ) ) ; if ( currList == null ) { currList = new ArrayList <> ( ) ; ret . put ( pair . getFirst ( ) , currList ) ; } currList . add ( pair . getSecond ( ) ) ; } return ret ; }
Group the input pairs by the key of each pair .