JavaCodeKata – Lambdas

There are some unit tests to validate the solution. I’ve found it a good opportunity to exercise the use of lambdas so I decided to solve it. Below is my solution to this kata.

The first method should take the String list and sort all the String elements in ascending (ASCII) order:

Java

1

2

3

4

5

6

7

8

9

10

11

/**

* This method should take the String List and sort all the String elements in ascending (ASCII) order.

*

* @return The sorted values in ascending ASCII order.

*/

publicList<String>getSortedStrings(){

returnvalues

.stream()

.sorted()

.collect(Collectors.toList());

}

The other method should take the String list and:

filter the elements that contains one or more digits

transform (map) the remaining Strings into Integers

sort the Integers in ascending order

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* This method should take the String List and:

* <ol>

* <li>filter the elements that contains one or more digits;</li>

* <li>transform (map) the remaining Strings into Integers;</li>

* <li>sort the Integers in ascending order.</li>

* </ol>

*

* @return

*/

publicList<Integer>getSortedIntegers(){

returnvalues

.stream()

.filter(s->s.matches("\\d+"))

.map(Integer::valueOf)

.sorted()

.collect(Collectors.toList());

}

The last method should take the String list and:

filter the elements that contains one or more digits

transform (map) the remaining Strings into Integers

sort the Integers in descending order

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

/**

* This method should take the String List and:

* <ol>

* <li>filter the elements that contains one or more digits;</li>

* <li>transform (map) the remaining Strings into Integers;</li>

* <li>sort the Integers in descending order.</li>

* </ol>

*

* @return

*/

publicList<Integer>getSortedDescendingIntegers(){

returnvalues

.stream()

.filter(s->s.matches("\\d+"))

.map(Integer::valueOf)

.sorted(Comparator.reverseOrder())

.collect(Collectors.toList());

}

Note that the steps filter the elements that contains one or more digits and transform (map) the remaining Strings into Integers are identical. So I decided to extract the partial Stream into a method with the Extract Method refactoring support on IntelliJ IDEA:

Java

1

2

3

4

5

6

privateStream<Integer>integersWithOneOrMoreDigits(){

returnvalues

.stream()

.filter(s->s.matches("\\d+"))

.map(Integer::valueOf);

}

Then I refactored the the solution to use the new extracted method:

Java

1

2

3

4

5

publicList<Integer>getSortedIntegers(){

returnintegersWithOneOrMoreDigits()

.sorted()

.collect(Collectors.toList());

}

Java

1

2

3

4

5

publicList<Integer>getSortedDescendingIntegers(){

returnintegersWithOneOrMoreDigits()

.sorted(Comparator.reverseOrder())

.collect(Collectors.toList());

}

I re-run the tests and they all passed. What do you think about this solution? Do you suggest other ones?