Skip to main content
added 2 characters in body
Source Link
Spotted
  • 4.1k
  • 1
  • 20
  • 34

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception;IOException; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

Going a second step further, you don't even need to bother with BufferedReader anymore if you use Files ! This can be taken down to the following lambda:

CheckedFunction<String, String> lambda = path -> Files.lines(Paths.get(path)).collect(Collectors.joining()); 

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

Going a second step further, you don't even need to bother with BufferedReader anymore if you use Files ! This can be taken down to the following lambda:

CheckedFunction<String, String> lambda = path -> Files.lines(Paths.get(path)).collect(Collectors.joining()); 

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws IOException; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

Going a second step further, you don't even need to bother with BufferedReader anymore if you use Files ! This can be taken down to the following lambda:

CheckedFunction<String, String> lambda = path -> Files.lines(Paths.get(path)).collect(Collectors.joining()); 
added 198 characters in body
Source Link
Spotted
  • 4.1k
  • 1
  • 20
  • 34

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

Going a second step further, you don't even need to bother with BufferedReader anymore if you use Files ! This can be taken down to the following lambda:

CheckedFunction<String, String> lambda = path -> Files.lines(Paths.get(path)).collect(Collectors.joining()); 

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

Going a second step further, you don't even need to bother with BufferedReader anymore if you use Files ! This can be taken down to the following lambda:

CheckedFunction<String, String> lambda = path -> Files.lines(Paths.get(path)).collect(Collectors.joining()); 
deleted 1 character in body
Source Link
Spotted
  • 4.1k
  • 1
  • 20
  • 34

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takingtakes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources taking care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

As dimo414 said, first declare a new functional interface

@FunctionalInterface public interface CheckedFunction<U, V> { public V apply(U u) throws Exception; } 

Then I'll go one step further by refactoring the lambda expression into the following:

CheckedFunction<String, String> lambda = path -> { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.lines().collect(Collectors.joining()); } }; 

The try-with-resources takes care of closing the BufferedRead in any case and using the conciseness of the stream API to concatenate all the lines.

Source Link
Spotted
  • 4.1k
  • 1
  • 20
  • 34
Loading