Group a list of strings by length Is it possible to write this using the stream element? I can get the most of it with stream but it is the add adding string to list which is in the map that I am caught on.

/**
 * Given a list of strings, returns a map where the keys are the unique strings lengths found in the list and the values are the strings matching each length
 * [hello,world,cat,ambulance] would return {5 -&gt; [hello, world], 3 -&gt; [cat] -&gt; 3, 9 -&gt; [ambulance]}
 * @param strings
 * @return
 */
public Map&lt;Integer,List&lt;String&gt;&gt; mapLengthToMatchingStrings(List&lt;String&gt; strings){

    Map&lt;Integer,List&lt;String&gt;&gt; mapILS = new HashMap&lt;Integer, List&lt;String&gt;&gt;();

    for (String str : strings) {
        int length = str.length();

        if (mapILS.containsKey(length)) {
            // Add str to list at key
            //Get list at key
            List&lt;String&gt; rs = mapILS.get(length);
            //Add str to list
            rs.add(str);
            //Update key in map
            mapILS.put(length, rs);
        }else {
            // Add new key list to map
            //Create list
            List&lt;String&gt; rs = new ArrayList&lt;String&gt;();
            //Add string
            rs.add(str);
            //Add key and list to map
            mapILS.put(length, rs);
        }
    }       
    return mapILS;
}
It's not only possible but also a great example of how functional programming can make code much simpler and readable:

  public Map&lt;Integer,List&lt;String&gt;&gt; mapLengthToMatchingStrings(List&lt;String&gt; strings){
        return strings.stream().distinct().collect(Collectors.groupingBy(String::length));
    }


(The distinct part is optional, regarding whether you want duplicates in the lists)