How to write system.out.println as a method reference? - java-8

I am iterating a Collection using foreach, I am using intelliJ IDE.
queue.forEach(s->{
System.out.println(s);
});
I am getting warning here that
"Can be replaced with method reference.."
can anyone suggest how can I use method reference here?

System.out::println
This should do
queue.forEach(System.out::println);

Related

is there any way to print a value of the object?

I am using java spring boot and I got an errorrelated to the Aggregation.group I want to print the contents of the object, but instead I get the result which looks like object itself
org.springframework.data.mongodb.core.aggregation.GroupOperation#5ef26c0d
What I want to get the value of the object. Is there any way to print it out?
I have tried these commands so far:
System.out.println(Aggregation.group());
System.out.println(Aggregation.group("a").addToSet("b").as("references"));
Please help me how to print the values.
In general, printing an object results in calling its toString() method. To modify how the print of an object looks like, just override the toString() method in the relevant class.
In your case, it is a foreign class of another library you use. The easier way how to find the issue is to use the debugger of your IDE. Set some breakpoints where the debugger should stop the programme to investigate variables values at this point in time. It is often quite cumbersome to print variable values at the console.
Depending on the IDE you use: the usage of the debugging function in Eclipse, in JetBrains' IntelliJ and in Visual Studio Code.

Get method object with method references

Is it possible to get an instance of java.lang.reflect.Method by using the new method reference feature of Java 8?
That way I would have a compile time check and refactoring would be also easier. Also, I wouldn't need to catch the exceptions (which shouldn't been thrown after all).
Short answer: No.
You will get a lambda of that method, not a java.lang.reflect.Method. You do not know the name of the method. Just as you can not have a reference to a "property" of a java bean.
You can have a reference to the getter or setter but that is also a lambda and you do not know the actual name.
In any case you'd have to provide the name as a String and that can't be checked by the compiler. I also tried this but failed. It simply can't be done unless you write something that checks the javacode/bytecode. But there are tools that do that.
Maybe the Criteria API could be used for that, but it depends on the requirements.
http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html
There you'd have a SingularAttribute or similar field on a "metamodel" and then the regular java compiler can check the (generic) type of it.

How to use a method as an argument of another method when they have different types in java?

I need to add a method inside this add(null) method but the method has a string argument like the following
private double mymethod(String writing) // This is the method which must replace
// the null value inside the add(null) method
but since it has a String argument this gives an error! what could i do in order to fix this problem?
From what you have I think that features is a Collection of Doubles - is that right?
And what you want to do is the following:
features.add(mymethod("some string"));
If thats the case I dont see the problem I'm afraid - where is the error being thrown?
Provided your mymethod returns a valid double I cant see what else would cause you a problem here.

How to programmatically setObjectClass for NSArrayController

I'm having a problem with what should be a very simple thing. I want to create an NSArrayController and specify the class it manages. Problem is, I can't figure out the correct way to specify the Class in the setObjectClass method. I want to do the following:
[projectArrayController setObjectClass:SKHProject];
SKHProject is a class that I've imported in the implementation file. I keep getting the "Expected expression before 'SKHProject'" error, but I can't figure out the correct expression. Where am I going wrong?
Do
[projectArrayController setObjectClass:[SKHProject class]];
!
Just found it
[projectArrayController setObjectClass:[SKHProject class]];
Thanks anyway
You can only use a class name as the receiver of a message; you can't use it in any other context. So, to pass the Class somewhere, send it a message asking it for itself: [SKHProjectClass class].

what does "generate method stub" mean in c#?

I'm trying to call a function, and VS gives me an error (red underline), and i have the option to "generate method stub". What is this?
The generate method stub will generate you a method which looks exactly like you've written it, with the same parameters. Probably are getting this error because you've misspelled the method or because it is in a different namespace.
It means that you're trying to call the function incorrectly; check to make sure you've spelled the method name correctly, and that you're passing it the proper number and types of arguments.
It means you typed a wrong signature, so VS assumes this method doesn't exist. By using the shortcut VS can help you create the method as a stub (i.e. the signature, then you have to fill out the implementation).
ahh I had
method(button.Tag);
and a declaration of
void method(int tag)
so i fixed it with
method(int.Parse(button.Tag.toString()));
i tried that before, but I forgot to put "toString", since I thought it was already an int... stupid little mistake. thx guys

Resources