Java Lambda Expression reverse the characters in string

Description

Java Lambda Expression reverse the characters in string

interface StringFunc {
  String func(String n);//from  w  w  w . j av a 2  s  .c  o  m
}

public class Main {
  public static void main(String args[]) {

    // This block lambda that reverses the characters in a string.
    StringFunc reverse = (str) -> {
      String result = "";
      int i;

      for (i = str.length() - 1; i >= 0; i--)
        result += str.charAt(i);

      return result;
    };

    System.out.println("Lambda reversed is " + reverse.func("Lambda"));
    System.out.println("Expression reversed is " + reverse.func("Expression"));
  }
}



PreviousNext

Related