项目作者: Adhouma

项目描述 :
Java Lambda expressions challenge
高级语言: Java
项目地址: git://github.com/Adhouma/Lambda-expressions-challenge.git
创建时间: 2020-10-25T10:44:50Z
项目社区:https://github.com/Adhouma/Lambda-expressions-challenge

开源协议:

下载


Lambda-expressions-challenges

Java Lambda expressions challenges

  1. /**
  2. * Challenge 1:
  3. * Write the following anonymous class as a lambda expression
  4. */
  5. Runnable runnable = new Runnable() {
  6. @Override
  7. public void run() {
  8. String myString = "Let's split this up into an array";
  9. String[] parts = myString.split(" ");
  10. for (String part : parts) {
  11. System.out.println(part);
  12. }
  13. }
  14. };
  15. /**
  16. * Challenge 2:
  17. * Write the following method as a lambda expression, Don't worry about assigning it to anything
  18. */
  19. public static String everySecondChar(String source) {
  20. StringBuilder returnVal = new StringBuilder();
  21. for (int i = 0; i < source.length(); i++) {
  22. if (i % 2 == 1) {
  23. returnVal.append(source.charAt(i));
  24. }
  25. }
  26. return returnVal.toString();
  27. }
  28. /**
  29. * Challenge 3:
  30. * Right now the function in challenge 2 doesn't do anything.
  31. * Write the code that will execute the function with an argument of "1234567890"
  32. */
  33. /**
  34. * Challenge 4:
  35. * Instead of executing this function(challenge 2) directly,
  36. * Suppose we want to pass it to a method called everySecondCharacter that accept the function as a parameter
  37. * and executes it with the argument "1234567890"
  38. *
  39. * It should return the result of the function.
  40. * For bonus points, don't hard code the argument string within the method
  41. */
  42. /**
  43. * Challenge 5:
  44. * Using the bonus version on challenge 4, call the method and print the result returned from the method
  45. */
  46. /**
  47. * Challenge 6:
  48. * Write a lambda expression that maps to the java.util.Supplier interface
  49. * This lambda sould return the string "I love Java!", assign it to a variable called iLoveJava
  50. */
  51. /**
  52. * Challenge 7:
  53. * The supplier won't do anything until we use it,
  54. * Use the Supplier(Challenge 6) to assign the string "I love Java!" to a variable called supplierResult,
  55. * Then print the variable to the console
  56. */
  57. /**
  58. * Challenge 8:
  59. * There are many interfaces in the Java SDK, and sometimes we can use a lambda expression instead of creating
  60. * an instance that implements the interface we want to use
  61. *
  62. * Given a specific interface, how wan we tell whether we can map a lambda expression to it ?
  63. * What's the criteria that has to be met ?
  64. */
  65. /**
  66. * Challenge 9:
  67. * Suppose we have the following list of the top 5 male and female names for 2015,
  68. * Write code to print the items in the list in sorted order, and with the first letter in each name upper-cased
  69. * Use Lambda expressions and Stream operations wherever possible
  70. */
  71. /**
  72. * Challenge 10:
  73. * Instead of printing out the sorted names(challenge 9),
  74. * Print out how many names start with the letter 'A'
  75. */