Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
¿Cómo imprimir un objeto si se crea con lambda?

Aquí está mi ejemplo de interfaz

 public interface Card { String name(); }

Lo uso en ArrayList y creo objetos como este.

 public static void main(String[] args) { List<Card> cards = new ArrayList<>(); cards.add(() -> "2"); cards.add(() -> "4"); System.out.println(cards); }

¿Cómo puedo imprimir un número en la consola si creo un objeto como este? Aquí está el ejemplo del texto impreso.

 [d$$Lambda$14/0x0000000800bb1438@2d98a335]

Pero quiero que se impriman 2 y 4.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Prueba esto.

 public interface Card { String name(); static Card of(Supplier<String> supplier) { return new Card() { @Override public String name() { return supplier.get(); } @Override public String toString() { return name(); } }; } } public static void main(String[] args) { List<Card> cards = new ArrayList<>(); cards.add(Card.of(() -> "2")); cards.add(Card.of(() -> "4")); System.out.println(cards); }

producción:

 [2, 4]

Nota: no puede anular el método toString() .

 public interface Card { String name(); public default String toString() { // compile error // "A default method cannot override a method from java.lang.Object " return name(); } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!