Sie sind auf Seite 1von 4

Java 8

1. Make an array containing a few Strings. Sort it by


• length (i.e., shortest to longest)
• reverse length (i.e., longest to shortest)
• alphabetically by the first character only
(Hint: charAt(0) returns the numeric code for the first character)
• Strings that contain “e” first, everything else second. For now, put
the code directly in the lambda.
(Hint: remember that the body of a lambda is allowed to have curly
braces and a return statement.)
• Redo the previous problem, but use a static helper method so that
your lambda looks like this:
Arrays.sort(words, (s1,s2) -> Utils.yourMethod(s1,s2))

2. Make a method called betterString that takes two Strings and a lambda
that says whether the first of the two is “better”. The method should
return that better String; i.e., if the function given by the lambda
returns true, the betterString method should return the first String,
otherwise betterString should return the second String. Here are two
examples of how your code should work when it is finished (the first
lambda example returns whichever of string1 and string2 is longer, and
the second lambda example always returns string1).
• String string1 = ...;
• String string2 = ...;
• String longer = StringUtils.betterString(string1, string2,
(s1, s2) -> s1.length() > s2.length());
• String first = StringUtils.betterString(string1, string2,
(s1, s2) -> true);
Accomplishing all of this requires you to do three things:
• Define the TwoStringPredicate interface. It will specify a method
that takes 2 strings and returns a boolean.
• Define the static method betterString. That method will take 2
strings and an instance of your interface. It returns string1 if the
method in interface returns true, string2 otherwise.
• Call betterString.
3. Use generics to replace your String-specific solutions to problem 3
with generically typed solutions. That is, replace betterString with
betterEntry and TwoStringPredicate with TwoElementPredicate. Make
sure your previous examples still work when you only change
betterString to betterElement. But, now you should also be able to
supply two Cars and a Car predicate, two Employees and an Employee
predicate, etc. For example:
• ElementUtils.betterElement(string1, string2,
(s1, s2) -> s1.length() > s2.length())
• ElementUtils.betterElement(car1, car2,
(c1, c2) -> c1.getPrice() > c2.getPrice())
• ElementUtils.betterElement(employee1, employee2,
(e1, e2) -> e1.getSalary() > e2.getSalary())

4. Add the annotation to your two interfaces (TwoStringPredicate and


TwoElementPredicate). Does adding this annotation change the
behavior of your code? What happens if you try to add a second
abstract method to TwoStringPredicate?

5. Make a static method called allMatches. It should take a List of Strings


and a Predicate<String>, and return a new List of all the values that
passed the test. Test it with several examples. E.g.:
• List<String> shortWords = StringUtils.allMatches(words,
s -> s.length() < 4);
• List<String> wordsWithB = StringUtils.allMatches(words,
s -> s.contains("b"));
• List<String> evenLengthWords = StringUtils.allMatches(words,
s -> (s.length() % 2) == 0);

6. Redo allMatches so it works on any List and associated Predicate, not


just on Strings. Verify that your examples from #1 still work. But now,
you should be able to also do things like this:
• List<Integer> nums = Arrays.asList(1, 10, 100, 1000, 10000);
• List<Integer> bigNums = ElementUtils.allMatches(nums,
n -> n>500);

7. Make a static method called transformedList. It should take a List of


Strings and a Function<String,String> and return a new List that
contains the results of applying the Function to each element of the
original List. E.g.:
• List<String> excitingWords = StringUtils.transformedList(words,
s -> s + "!");
• List<String> eyeWords = StringUtils.transformedList(words,
s -> s.replace("i", "eye"));
• List<String> upperCaseWords =
StringUtils.transformedList(words, String::toUpperCase);

8. Redo transformedList so it works with generic types. Verify that your


examples from #3 still work. But now, you should also be able to also
do things like this:
• List<Integer> wordLengths = ElementUtils.transformedList(words,
String::length);
Notice above that I am passing in a List of Strings, but getting out a List
of Integer. Make sure your generic types support this idea.

For all the exercises, start with a List of Strings similar to this:
• List<String> words = Arrays.asList("hi", "hello", ...);

9. Loop down the words and print each on a separate line, with two
spaces in front of each word. Don’t use map.

10. Repeat the previous problem, but without the two spaces in front. This
is trivial if you use the same approach as in #9, so the point is to use a
method reference here, as opposed to an explicit lambda in problem 9.

11. In the previous exercise, we produced transformed lists like this:


• List<String> excitingWords = StringUtils.transformedList(words,
s -> s + "!");
• List<String> eyeWords = StringUtils.transformedList(words,
s -> s.replace("i", "eye"));
• List<String> upperCaseWords =
StringUtils.transformedList(words, String::toUpperCase);
Produce the same lists as above, but this time use streams and the builtin
“map” method.

12. In the previous exercise, we produced filtered lists like this:


• List<String> shortWords = StringUtils.allMatches(words,
s -> s.length() < 4);
• List<String> wordsWithB = StringUtils.allMatches(words,
s -> s.contains("b"));
• List<String> evenLengthWords = StringUtils.allMatches(words,
s -> (s.length() % 2) == 0);
Produce the same lists as above, but this time use “filter”.
13. Turn the strings into uppercase, keep only the ones that are shorter
than 4 characters, of what is remaining, keep only the ones that contain
“E”, and print the first result. Repeat the process, except checking for a
“Q” instead of an “E”. When checking for the “Q”, try to avoid
repeating all the code from when you checked for an “E”.

14. Produce a single String that is the result of concatenating the


uppercase versions of all of the Strings. E.g., the result should be
"HIHELLO...". Use a single reduce operation, without using map.

15. Produce the same String as above, but this time via a map operation
that turns the words into upper case, followed by a reduce operation
that concatenates them.

16. Produce a String that is all the words concatenated together, but with
commas in between. E.g., the result should be "hi,hello,...". Note that
there is no comma at the beginning, before “hi”, and also no comma at
the end, after the last word.

17. Find the total number of characters (i.e., sum of the lengths) of the
strings in the List.

18. Find the number of words that contain an “h”.

Das könnte Ihnen auch gefallen