Advertisement
NLinker

Java quiz on lambdas+overloading

Jul 31st, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.stream.Collectors;
  2. import java.util.stream.Stream;
  3.  
  4. public class Test {
  5.     @FunctionalInterface
  6.     interface Varargs { String meth(int i, String... strings); }
  7.  
  8.     @FunctionalInterface
  9.     interface NoVarargs { String meth(int i); }
  10.  
  11.     @org.testng.annotations.Test
  12.     public void test() {
  13.         Varargs f1 = this::myMethod;
  14.         System.out.println(f1.meth(2, "hello", "world"));
  15.  
  16.         NoVarargs f2 = this::myMethod;
  17.         System.out.println(f2.meth(2));
  18.     }
  19.  
  20.     String myMethod(int i, String... strings) {
  21.         return i + ":" + Stream.of(strings).collect(Collectors.joining(":"));
  22.     }
  23.  
  24.     // String myMethod(Integer i) { return i + ":NO_STRINGS: Integer"; }
  25.     // String myMethod(Long i) { return i + ":NO_STRINGS: Long"; }
  26.     // String myMethod(byte i) { return i + ":NO_STRINGS: byte"; }
  27.     // String myMethod(Byte i) { return i + ":NO_STRINGS: Byte"; }
  28.     // String myMethod(Float i) { return i + ":NO_STRINGS: Float"; }
  29.     // String myMethod(float i) { return i + ":NO_STRINGS: float"; }
  30.     // String myMethod(Double i) { return i + ":NO_STRINGS: Double"; }
  31.     // String myMethod(double i) { return i + ":NO_STRINGS: double"; }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement