0

hello how do I call a method taking an interface as a parameter from the main ? The code in the main is an example of what I want to achieve but by calling the method map now

What do I write in my map method and how do I call it in the main ? Thank you

What I want to achieve :

StringTransformation addBlah = (e) -> {
    e += "boo";
     return e;
};
System.out.println(addBlah.transf("Hello")); // prints Helloboo

public class Main{

    public static void main(String[] args) {
        String a = hello;
        // How do I modify the string a by calling map ?


    }

    void map(StringTransformation t) {
        // What do I write ??
    }
}

public interface StringTransformation {
    String transf(String s);
}
9
  • With out the interface definition it's hard to say. Please include the code for the interface. Commented Nov 14, 2018 at 19:38
  • 1
    You pass an instance of a class that has implemented said interface. Commented Nov 14, 2018 at 19:38
  • @markspace Sorry I forgot, I added it now Commented Nov 14, 2018 at 19:39
  • 1
    Also, unless I missed something, map will need to be static to be called from main. Commented Nov 14, 2018 at 19:40
  • Aside: you can simply use e -> e += "boo" as your lambda declaration. No need for braces or return, since a += b is an expression. Commented Nov 14, 2018 at 19:42

2 Answers 2

1

You want to modify a String with a given StringTransformation so you need to pass both of them to the map method. Also you can turn addBlah in a more simple lambda :

public static void main(String[] args) {
    StringTransformation addBlah = (e) -> e + "boo";

    String str = "Hello";
    System.out.println(str);    // Hello
    str = map(addBlah, str);
    System.out.println(str);    // Helloboo
}

static String map(StringTransformation t, String argument) { 
    return t.transf(argument);     
}
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot call map inside the static main method. You must make map a static method as well if you want to do that. Also we can't help you with what to put inside your map function if you don't tell us what it should do.

public static void main(String[] args) {

    String string = "Hello";
    // you can call `mapBoo` like normal here
    string = mapBoo(string);
    System.out.println(string);

    List<String> strings = Arrays.asList("Hello", "this", "is", "a", "test");

    // or you can pass mapBoo into the stream.map method since map fits the method signature
    List<String> mappedStrings = strings.stream().map(Main::mapBoo)
            .collect(Collectors.toList());

    for (String mappedString : mappedStrings)
        System.out.println(mappedString);
}


static String mapBoo(String s) {
    return s + "boo";
}

1 Comment

I have a string a = "Hello" initialized in the main class and when I call map from the main I want it to modify string a to "Helloboo"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.