gpt4 book ai didi

java - 在 Java 8 中使用 lambda 表达式打印列表(没有重复项)

转载 作者:行者123 更新时间:2023-11-30 06:53:31 26 4
gpt4 key购买 nike

我有什么

我有三个类(class)。 Main.javaProducto.javaSupermercado.java

  • Main.java 包含 main.
  • Producto.java 我要在列表中打印的产品结构。
  • Supermercado.java 是一个 Singleton Pattern,代表放置我的 List 的超市。

代码

我只会向大家展示我认为与问题相关的代码,如果您需要更多信息,请随时询问。

主.java

// Instanciar productos
Producto p1 = new Producto("Sal 100gr", LocalDate.now(), 0.5f, 1);
Producto p2 = new Producto("Pimienta 25gr", LocalDate.now(), 0.2f, 1);
Producto p3 = new Producto("Sal 1kg", LocalDate.now(), 2, 1);
Producto p4 = new Producto("Te limón", LocalDate.now(), 3.5f, 2);
Producto p5 = new Producto("Azucar 1kg", LocalDate.now(), 2, 1);
Producto p6 = new Producto("Azucar 5kg", LocalDate.now(), 10, 1);
Producto p7 = new Producto("Xilitol 1kg", LocalDate.now(), 15, 1);
Producto p8 = new Producto("Xilitol 1kg", LocalDate.now(), 15, 1);

// Añadir productos a la lista del super
Supermercado.getSupermercado().addProducto(p1);
Supermercado.getSupermercado().addProducto(p2);
Supermercado.getSupermercado().addProducto(p3);
Supermercado.getSupermercado().addProducto(p4);
Supermercado.getSupermercado().addProducto(p5);
Supermercado.getSupermercado().addProducto(p6);
Supermercado.getSupermercado().addProducto(p7);
Supermercado.getSupermercado().addProducto(p8);

// Printing the list
System.out.println("\nLista productos.");
Supermercado.getSupermercado().printListaProductos();

// Printing distinct items (do not repeat xilitol plz)
System.out.println("\nLista de productos sin repetir.");
Supermercado.getSupermercado().printListaProductosSinRepetir();

super 市场.java

// Printing the list
public void printListaProductos() {
listaProducto.stream()
.forEach(p -> System.out.println("Producto " +p.getNombre()
+ '\t' + "Precio " + p.getPrecio() + "€"
+ '\t' + "Caducidad " + p.getFechaCaducidad()));
}

// Printing distinct items (do not repeat xilitol plz)
public void printListaProductosSinRepetir() {
listaProducto.stream()
.distinct()
.forEach(p -> System.out.println("Producto " +p.getNombre()
+ '\t' + "Precio " + p.getPrecio() + "€"
+ '\t' + "Caducidad " + p.getFechaCaducidad()));
}

Producto.java

private String nombre;
private int seccion;
private LocalDate fechaCaducidad;
private float precio;

// constructor
public Producto(String pNombre, LocalDate pFechaCaducidad, float pPrecio, int pSeccion) {
this.nombre = pNombre;
this.fechaCaducidad = pFechaCaducidad;
this.precio = pPrecio;
this.seccion = pSeccion;
}

public LocalDate getFechaCaducidad() {
return this.fechaCaducidad;
}

public float getPrecio() {
return this.precio;
}

public int getSeccion() {
return this.seccion;
}

public String getNombre() {
return this.nombre;
}

输出

Lista productos.
Producto Sal 100gr Precio 0.5€ Caducidad 2016-05-27
Producto Pimienta 25gr Precio 0.2€ Caducidad 2016-05-27
Producto Sal 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Te limón Precio 3.5€ Caducidad 2016-05-27
Producto Azucar 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Azucar 5kg Precio 10.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27

Lista de productos sin repetir.
Producto Sal 100gr Precio 0.5€ Caducidad 2016-05-27
Producto Pimienta 25gr Precio 0.2€ Caducidad 2016-05-27
Producto Sal 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Te limón Precio 3.5€ Caducidad 2016-05-27
Producto Azucar 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Azucar 5kg Precio 10.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27

问题

显然,printListaProductosSinRepetir() 方法中的 .distinct() 代码对我不起作用。它应该显示这个:

Lista de productos sin repetir.
Producto Sal 100gr Precio 0.5€ Caducidad 2016-05-27
Producto Pimienta 25gr Precio 0.2€ Caducidad 2016-05-27
Producto Sal 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Te limón Precio 3.5€ Caducidad 2016-05-27
Producto Azucar 1kg Precio 2.0€ Caducidad 2016-05-27
Producto Azucar 5kg Precio 10.0€ Caducidad 2016-05-27
Producto Xilitol 1kg Precio 15.0€ Caducidad 2016-05-27

没有额外的Xilitol 1kg。这些项目不应重复。

为什么我需要这个

我正在尝试了解 Java 8 如何与 Lambda 表达式 一起工作。所以我正在为此建立一些例子。问题是 .distinct() 不工作。我在 SO 中找不到这个解决方案。提前致谢。

最佳答案

您应该覆盖 object 类中的 equalshashCode 方法,以使其正常工作,这里是一个示例。在您的 Producto.java 中添加以下代码。

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((nombre == null) ? 0 : nombre.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Producto other = (Producto) obj;
if (nombre == null) {
if (other.nombre != null)
return false;
} else if (!nombre.equals(other.nombre))
return false;
return true;
}

关于java - 在 Java 8 中使用 lambda 表达式打印列表(没有重复项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37480539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com