Method containing interface as parameter
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);
}
java interface
|
show 3 more comments
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);
}
java interface
With out the interface definition it's hard to say. Please include the code for the interface.
– markspace
Nov 14 '18 at 19:38
1
You pass an instance of a class that has implemented said interface.
– KevinO
Nov 14 '18 at 19:38
@markspace Sorry I forgot, I added it now
– Joon1
Nov 14 '18 at 19:39
1
Also, unless I missed something,map
will need to bestatic
to be called frommain
.
– KevinO
Nov 14 '18 at 19:40
Aside: you can simply usee -> e += "boo"
as your lambda declaration. No need for braces or return, sincea += b
is an expression.
– Andy Turner
Nov 14 '18 at 19:42
|
show 3 more comments
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);
}
java interface
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);
}
java interface
java interface
edited Nov 14 '18 at 19:57
Joon1
asked Nov 14 '18 at 19:36
Joon1Joon1
32
32
With out the interface definition it's hard to say. Please include the code for the interface.
– markspace
Nov 14 '18 at 19:38
1
You pass an instance of a class that has implemented said interface.
– KevinO
Nov 14 '18 at 19:38
@markspace Sorry I forgot, I added it now
– Joon1
Nov 14 '18 at 19:39
1
Also, unless I missed something,map
will need to bestatic
to be called frommain
.
– KevinO
Nov 14 '18 at 19:40
Aside: you can simply usee -> e += "boo"
as your lambda declaration. No need for braces or return, sincea += b
is an expression.
– Andy Turner
Nov 14 '18 at 19:42
|
show 3 more comments
With out the interface definition it's hard to say. Please include the code for the interface.
– markspace
Nov 14 '18 at 19:38
1
You pass an instance of a class that has implemented said interface.
– KevinO
Nov 14 '18 at 19:38
@markspace Sorry I forgot, I added it now
– Joon1
Nov 14 '18 at 19:39
1
Also, unless I missed something,map
will need to bestatic
to be called frommain
.
– KevinO
Nov 14 '18 at 19:40
Aside: you can simply usee -> e += "boo"
as your lambda declaration. No need for braces or return, sincea += b
is an expression.
– Andy Turner
Nov 14 '18 at 19:42
With out the interface definition it's hard to say. Please include the code for the interface.
– markspace
Nov 14 '18 at 19:38
With out the interface definition it's hard to say. Please include the code for the interface.
– markspace
Nov 14 '18 at 19:38
1
1
You pass an instance of a class that has implemented said interface.
– KevinO
Nov 14 '18 at 19:38
You pass an instance of a class that has implemented said interface.
– KevinO
Nov 14 '18 at 19:38
@markspace Sorry I forgot, I added it now
– Joon1
Nov 14 '18 at 19:39
@markspace Sorry I forgot, I added it now
– Joon1
Nov 14 '18 at 19:39
1
1
Also, unless I missed something,
map
will need to be static
to be called from main
.– KevinO
Nov 14 '18 at 19:40
Also, unless I missed something,
map
will need to be static
to be called from main
.– KevinO
Nov 14 '18 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.– Andy Turner
Nov 14 '18 at 19:42
Aside: you can simply use
e -> e += "boo"
as your lambda declaration. No need for braces or return, since a += b
is an expression.– Andy Turner
Nov 14 '18 at 19:42
|
show 3 more comments
2 Answers
2
active
oldest
votes
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";
}
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"
– Joon1
Nov 14 '18 at 20:00
add a comment |
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);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307602%2fmethod-containing-interface-as-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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";
}
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"
– Joon1
Nov 14 '18 at 20:00
add a comment |
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";
}
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"
– Joon1
Nov 14 '18 at 20:00
add a comment |
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";
}
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";
}
edited Nov 14 '18 at 20:05
answered Nov 14 '18 at 19:55
xtraticxtratic
2,4971822
2,4971822
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"
– Joon1
Nov 14 '18 at 20:00
add a 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"
– Joon1
Nov 14 '18 at 20:00
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"
– Joon1
Nov 14 '18 at 20:00
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"
– Joon1
Nov 14 '18 at 20:00
add a comment |
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);
}
add a comment |
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);
}
add a comment |
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);
}
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);
}
answered Nov 14 '18 at 20:06
azroazro
11.3k41638
11.3k41638
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307602%2fmethod-containing-interface-as-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
With out the interface definition it's hard to say. Please include the code for the interface.
– markspace
Nov 14 '18 at 19:38
1
You pass an instance of a class that has implemented said interface.
– KevinO
Nov 14 '18 at 19:38
@markspace Sorry I forgot, I added it now
– Joon1
Nov 14 '18 at 19:39
1
Also, unless I missed something,
map
will need to bestatic
to be called frommain
.– KevinO
Nov 14 '18 at 19:40
Aside: you can simply use
e -> e += "boo"
as your lambda declaration. No need for braces or return, sincea += b
is an expression.– Andy Turner
Nov 14 '18 at 19:42