1
The problem would be that I cannot multiply the variables that return from Listing using lambda (e*e), it keeps asking to create a Listing method using Object, but using Generics. I appreciate any contribution.
public Element<T> listing() {
Element<T> e = head;
System.out.println("");
System.out.println("Start listing... ");
while (e != null) {
System.out.println("Value: " + e.getValue());
e = e.getNext();
}
System.out.println("End listing");
System.out.println("");
return e;
}
Calling for
Deque<String> deque = new Deque<String>();
deque.addFirst("zero");
deque.addFirst("first");
deque.addFirst("secound");
deque.addFirst("third");
deque.addLast("fifth");
deque.listing((e)-> System.out.println(e*e));
I didn’t tell you what the problem is.
– Piovezan
The method
listing
presented does not receive argument, but the use of it inDeque<T>
gets aConsumer<T>
. What problem are you really facing?– Jefferson Quesado
@Piovezan: The problem would be that I cannot multiply the variables that return from Listing using lambda (e*e), it keeps asking to create a Listing method using Object, but using Generics. I appreciate any contribution
– Sabrina
The class
Deque
it was you who defined it? With that methodlisting
?– Jefferson Quesado
@Jeffersonquesado Yes, she who defined, in another question here on the site.
– Piovezan
Related: https://answall.com/q/308221/64969
– Jefferson Quesado
The problem of wanting to print
e*e
is that this does not work with Generics. In this case strings are being used, but Java does not"first" * "third"
, for example. I think you need to explain better what you want to do there we try to find a solution.– Piovezan
In Java the multiplication operator only works with primitive types (integer or floating point numbers), and I believe also with classes Wrappers as
Integer
orDouble
(thanks to autoboxing).– Piovezan
ah I understood the mess. So if I switched to Deque<Integer> deque = new Deque<Integer>(); and added instead of string deque.addFirst(1); deque.addFirst(2); deque.addFirst(3); deque.addFirst(4); deque.addLast(5); would have no trouble using.Listing((e)->
– Sabrina
The method
listing
does not yet receive a parameter, such as aConsumer<T>
– Jefferson Quesado
Thank you! It worked.
– Sabrina