Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
How to print out an object if it's created with lambda?

Here is my interface example

public interface Card {
    String name();
}

I use it in ArrayList and create objects like this.

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

How can I print a number to console if I create an object like this? Here is the example of the printed text.

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

But I want 2 and 4 to be printed out.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try this.

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);
}

output:

[2, 4]

Note: You can't override toString() method.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda