Skip to content

Instantly share code, notes, and snippets.

@pdemanget
Last active April 15, 2016 08:52
Show Gist options
  • Select an option

  • Save pdemanget/18cd1df4d69c4ef9cfa64287511a14de to your computer and use it in GitHub Desktop.

Select an option

Save pdemanget/18cd1df4d69c4ef9cfa64287511a14de to your computer and use it in GitHub Desktop.

Revisions

  1. pdemanget revised this gist Apr 15, 2016. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion Collection8Utils.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * grouper sur 2 niveaux
    * Group on 2 levels
    */
    protected <K,V> Map<K, Map<K,List<V>>> groupByFields2 (List<V> modelList,Function<? super V, ? extends K> classifier1,Function<? super V, ? extends K> classifier2) {

    @@ -9,4 +9,11 @@ protected <K,V> Map<K, Map<K,List<V>>> groupByFields2 (List<V> modelList,Functio
    ,groupingBy2
    );
    return modelList.stream().collect(groupingBy);
    }

    /* Simple Example */
    public static void main (String[] args) {
    Map map = groupByFields2(new ArrayList(Currency.getAvailableCurrencies()) , Currency::getDefaultFractionDigits, Currency::getDisplayName);
    System.out.println(map.toString().replace("{","\n{"));

    }
  2. pdemanget created this gist Apr 15, 2016.
    12 changes: 12 additions & 0 deletions Collection8Utils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /**
    * grouper sur 2 niveaux
    */
    protected <K,V> Map<K, Map<K,List<V>>> groupByFields2 (List<V> modelList,Function<? super V, ? extends K> classifier1,Function<? super V, ? extends K> classifier2) {

    Collector<V, ?, Map<K, List<V>>> groupingBy2 = Collectors.groupingBy(classifier2);
    Collector<V, ?, Map<K, Map<K,List<V>>>> groupingBy = Collectors.groupingBy(
    classifier1
    ,groupingBy2
    );
    return modelList.stream().collect(groupingBy);
    }