fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. // Use an instance method reference with different objects.
  4.  
  5. // A functional interface that takes two reference arguments
  6. // and returns a boolean result.
  7. interface MyFunc<T> {
  8. boolean func(T v1, T v2);
  9. }
  10.  
  11. // A class that stores the temperature high for a day.
  12. class HighTemp {
  13. private int hTemp;
  14.  
  15. HighTemp(int ht) { hTemp = ht; }
  16.  
  17. // Return true if the invoking HighTemp object has the same
  18. // temperature as ht2.
  19. boolean sameTemp(HighTemp ht2) {
  20. return hTemp == ht2.hTemp;
  21. }
  22.  
  23. // Return true if the invoking HighTemp object has a temperature
  24. // that is less than ht2.
  25. boolean lessThanTemp(HighTemp ht2) {
  26. return hTemp < ht2.hTemp;
  27. }
  28. }
  29.  
  30.  
  31. /* Name of the class has to be "Main" only if the class is public. */
  32. class Ideone
  33. {
  34. // A method that returns the number of occurences
  35. // of an object for which some criteria, as specified by
  36. // the MyFunc parameter, is true.
  37. static <T> int counter(T[] vals, MyFunc<T> f, T v) {
  38. int count = 0;
  39.  
  40. for(int i=0; i < vals.length; i++)
  41. if(f.func(vals[i], v)) count++;
  42.  
  43. return count;
  44. }
  45.  
  46.  
  47.  
  48. public static void main (String[] args) throws java.lang.Exception
  49. {
  50. int count;
  51.  
  52. // Create an array of HighTemp objects.
  53. HighTemp[] weekDayHighs = { new HighTemp(89), new HighTemp(82),
  54. new HighTemp(90), new HighTemp(89),
  55. new HighTemp(89), new HighTemp(91),
  56. new HighTemp(84), new HighTemp(83) };
  57.  
  58. // Use counter() with arrays of the class HighTemp.
  59. // Notice that a reference to the instance method
  60. // sameTemp() is passed as the second argument.
  61. count = counter(weekDayHighs, HighTemp::sameTemp,
  62. new HighTemp(89));
  63. System.out.println(count + " days had a high of 89");
  64.  
  65. // Now, create and use another array of HighTemp objects.
  66. HighTemp[] weekDayHighs2 = { new HighTemp(32), new HighTemp(12),
  67. new HighTemp(24), new HighTemp(19),
  68. new HighTemp(18), new HighTemp(12),
  69. new HighTemp(-1), new HighTemp(13) };
  70.  
  71. count = counter(weekDayHighs2, HighTemp::sameTemp,
  72. new HighTemp(12));
  73. System.out.println(count + " days had a high of 12");
  74.  
  75.  
  76. // Now, use lessThanTemp() to find days when temperature was less
  77. // that a specified value.
  78. count = counter(weekDayHighs, HighTemp::lessThanTemp,
  79. new HighTemp(89));
  80. System.out.println(count + " days had a high less than 89");
  81.  
  82. count = counter(weekDayHighs2, HighTemp::lessThanTemp,
  83. new HighTemp(19));
  84. System.out.println(count + " days had a high of less than 19");
  85. }
  86. }
Success #stdin #stdout 0.16s 2184192KB
stdin
Standard input is empty
stdout
3 days had a high of 89
2 days had a high of 12
3 days had a high less than 89
5 days had a high of less than 19