java8/src/main/java/com/company/FileReader.java
public String nioTreeMap(String filepath) {
        try {
            List<String> lines = Files.readAllLines(Paths.get(filepath), StandardCharsets.UTF_8);
            SortedMap<Integer, List<String>> linesByLength = new TreeMap<>((i1, i2) -> i2.compareTo(i1)); // shortest lines first

            for( String line : lines ) {
                int key = line.length();
                List<String> value = linesByLength.getOrDefault(key, new ArrayList<String>());
                value.add(line);
                linesByLength.put(key, value);
            }

            Integer averageLineLength = linesByLength
                    .entrySet()
                    .stream()
                    .map( (entry) -> (Integer) (entry.getKey() * entry.getValue().size()) )
                    .collect(Collectors.summingInt(Integer::intValue))
                    / lines.size()
            ;

            String content = linesByLength
                    .values()
                    .stream()
                    .flatMap(List::stream)
                    //.collect(Collectors.toList());   // flatten to List<String>
                    .collect(Collectors.joining("\n")) // flatten to String.join()
            ;

            System.out.println("nioTreeMap() - totalLines: " + lines.size());
            System.out.println("nioTreeMap() - averageLineLength: " + averageLineLength);
            System.out.println("nioTreeMap() - longestLineLength: " + linesByLength.firstKey());

            return content;

        } catch (IOException exception) {
            System.out.println("nioTreeMap() - IOException: " + exception.toString());
        }

        return "";
    }
/**
     * Returns file contents sorted by reverse line length, using TreeMap and Java 8 Streams
     * @param filepath
     * @return
     */
Returns file contents sorted by reverse line length, using TreeMap and Java 8 Streams
@param filepath
@return
Returns file contents sorted by reverse line length using TreeMap and Java 8 Streams @param filepath @return
public String nioTreeMap(String filepath) {
        try {
            List<String> lines = Files.readAllLines(Paths.get(filepath), StandardCharsets.UTF_8);
            SortedMap<Integer, List<String>> linesByLength = new TreeMap<>((i1, i2) -> i2.compareTo(i1)); 
            for( String line : lines ) {
                int key = line.length();
                List<String> value = linesByLength.getOrDefault(key, new ArrayList<String>());
                value.add(line);
                linesByLength.put(key, value);
            }
            Integer averageLineLength = linesByLength
                    .entrySet()
                    .stream()
                    .map( (entry) -> (Integer) (entry.getKey() * entry.getValue().size()) )
                    .collect(Collectors.summingInt(Integer::intValue))
                    / lines.size()
            ;
            String content = linesByLength
                    .values()
                    .stream()
                    .flatMap(List::stream)
                    .collect(Collectors.joining("\n")) 
            ;
            System.out.println("nioTreeMap() - totalLines: " + lines.size());
            System.out.println("nioTreeMap() - averageLineLength: " + averageLineLength);
            System.out.println("nioTreeMap() - longestLineLength: " + linesByLength.firstKey());
            return content;
        } catch (IOException exception) {
            System.out.println("nioTreeMap() - IOException: " + exception.toString());
        }
        return "";
    }
public String nioTreeMap ( String filepath ) { try { List < String > lines = Files . readAllLines ( Paths . get ( filepath ) , StandardCharsets . UTF_8 ) ; SortedMap < Integer , List < String > > linesByLength = new TreeMap < > ( ( i1 , i2 ) -> i2 . compareTo ( i1 ) ) ; for ( String line : lines ) { int key = line . length ( ) ; List < String > value = linesByLength . getOrDefault ( key , new ArrayList < String > ( ) ) ; value . add ( line ) ; linesByLength . put ( key , value ) ; } Integer averageLineLength = linesByLength . entrySet ( ) . stream ( ) . map ( ( entry ) -> ( Integer ) ( entry . getKey ( ) * entry . getValue ( ) . size ( ) ) ) . collect ( Collectors . summingInt ( Integer :: intValue ) ) / lines . size ( ) ; String content = linesByLength . values ( ) . stream ( ) . flatMap ( List :: stream ) . collect ( Collectors . joining ( "\n" ) ) ; System . out . println ( "nioTreeMap() - totalLines: " + lines . size ( ) ) ; System . out . println ( "nioTreeMap() - averageLineLength: " + averageLineLength ) ; System . out . println ( "nioTreeMap() - longestLineLength: " + linesByLength . firstKey ( ) ) ; return content ; } catch ( IOException exception ) { System . out . println ( "nioTreeMap() - IOException: " + exception . toString ( ) ) ; } return "" ; }
Returns file contents sorted by reverse line length, using TreeMap and Java 8 Streams
 @param filepath
 @return
Returns file contents sorted by reverse line length using TreeMap and Java 8 Streams @param filepath @return
// shortest lines first //.collect(Collectors.toList());   // flatten to List<String> // flatten to String.join()