Java 8 List Get non-repeated items from a list

1

Case 1:

I have a List with a series of elements that can be repeated.

List<String> nombre;
nombre.add("Persona 1");
nombre.add("Persona 1");
nombre.add("Persona 1");
nombre.add("Persona 2");
nombre.add("Persona 3");
...

Is it possible to obtain elements without repeating Lambda expressions?

Exit:

Persona 1
Persona 2
Persona 3

( Edited )

Case 2:

public class Persona {
   private String nombre;
   private int edad;

   Persona(String nombre, int edad) {
      this.nombre=nombre;
      this.edad=edad;
   }
   ... getter y setter

}

List<Persona> p;
p.add(new Persona("Persona 1", 20));
p.add(new Persona("Persona 1", 15));
p.add(new Persona("Persona 1", 12));
p.add(new Persona("Persona 2", 50));
p.add(new Persona("Persona 3", 20));

Exit

Persona 1
Persona 2
Persona 3

In the case of Persona 1 I do not care what object I returned.

Edition 3

public class ConsultaBean {

    private static final long serialVersionUID = 6269862033023559153L;

    //Datos Cabecera
    private String negociador;
    private String descripcion;
    private String clave;
    //Detalle
    private Date fecha;
    private String hora;
    public String getNegociador() {
        return negociador;
    }
    public void setNegociador(String negociador) {
        this.negociador = negociador;
    }
    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
    public String getClave() {
        return clave;
    }
    public void setClave(String clave) {
        this.clave = clave;
    }
    public Date getFecha() {
        return fecha;
    }
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }
    public String getHora() {
        return hora;
    }
    public void setHora(String hora) {
        this.hora = hora;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((clave == null) ? 0 : clave.hashCode());
        result = prime * result + ((descripcion == null) ? 0 : descripcion.hashCode());
        result = prime * result + ((fecha == null) ? 0 : fecha.hashCode());
        result = prime * result + ((hora == null) ? 0 : hora.hashCode());
        result = prime * result + ((negociador == null) ? 0 : negociador.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;
        ConsultaBean other = (ConsultaBean) obj;
        if (clave == null) {
            if (other.clave != null)
                return false;
        } else if (!clave.equals(other.clave))
            return false;
        if (descripcion == null) {
            if (other.descripcion != null)
                return false;
        } else if (!descripcion.equals(other.descripcion))
            return false;
        if (fecha == null) {
            if (other.fecha != null)
                return false;
        } else if (!fecha.equals(other.fecha))
            return false;
        if (hora == null) {
            if (other.hora != null)
                return false;
        } else if (!hora.equals(other.hora))
            return false;
        if (negociador == null) {
            if (other.negociador != null)
                return false;
        } else if (!negociador.equals(other.negociador))
            return false;
        return true;
    }
}
    
asked by nachfren 12.04.2018 в 11:46
source

1 answer

4

It's easy with streams:

    List<String> nombre = new ArrayList<>();
    nombre.add("Persona 1");
    nombre.add("Persona 1");
    nombre.add("Persona 1");
    nombre.add("Persona 2");
    nombre.add("Persona 3");
    List<String> nombreSinDuplicados = nombre
       .stream()
       .distinct()
       .collect(Collectors.toList());

distinct () works based on the equals method of the class type in the list in question.

If I have understood your case, you want to display the non-repeated negotiator values on the screen:

List<ConsultaBean> listaConDuplicados;
List<String> listaSinDuplicados = listaConDuplicados.stream()
      .map(item->item.getNegociador())
      .distinct()
      .collect(Collectors.toList());
    
answered by 12.04.2018 / 11:57
source