列表中的2个对象,并按最大值

时间:2016-11-18 21:36:33

标签: java spring indexing collections

我编辑了

我有一个Employee类和一个Sell类 卖有一个 @ManyToOne  与员工的关系。

所以我需要通过销售来获得员工清单,以便搜索最好的月销售商,但我也需要销售数量。

我创建了一个Service类来执行此操作。 我有正确的bean来创建列表,这不是问题。问题是我不知道如何从函数中获得2个不同的列表,或者可能使用向量或类似的东西。

在做这个之前我做了自己的研究,但是根本没有我所知道的知识。

我放了一些域名代码:

  

员工

package germanAcosta.electronicaDonPepe.dominio;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

@SuppressWarnings("serial")

@Entity
public class Empleado implements Serializable{

@Id
private Integer dni;
private String password;
private String nombre;
private String apellido;

@ManyToMany // 1 o +
private List <Comision> comision;

@OneToMany(cascade=CascadeType.ALL)
private List <Premio> premio;

private String tipo;

public Empleado(){

}

public Empleado(Integer dni, String password, String nombre, String apellido, List<Comision> comision,
        List<Premio> premio, String tipo) {
    super();
    this.dni = dni;
    this.password = password;
    this.nombre = nombre;
    this.apellido = apellido;
    this.comision = comision;
    this.premio = premio;
    this.tipo = tipo;
}

public Integer getDni() {
    return dni;
}

public void setDni(Integer dni) {
    this.dni = dni;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getApellido() {
    return apellido;
}

public void setApellido(String apellido) {
    this.apellido = apellido;
}

public List<Comision> getComision() {
    return comision;
}

public void setComision(List<Comision> comision) {
    this.comision = comision;
}

public List<Premio> getPremio() {
    return premio;
}

public void setPremio(List<Premio> premio) {
    this.premio = premio;
}

public String getTipo() {
    return tipo;
}

public void setTipo(String tipo) {
    this.tipo = tipo;
}
}
  

package germanAcosta.electronicaDonPepe.dominio;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@SuppressWarnings("serial")

@Entity
public class Venta implements Serializable {

@Id
@GeneratedValue
private Integer numero_factura;

@ManyToOne
private Empleado empleado;

@OneToMany(cascade = CascadeType.ALL)
private List<Producto> productos = new ArrayList<Producto>();

private Date fechaDeIngreso;

public Venta() {

}

public Venta(Integer numero_factura, Empleado empleado, List<Producto> productos, Date fechaDeIngreso) {
    super();
    this.numero_factura = numero_factura;
    this.empleado = empleado;
    this.productos = productos;
    this.fechaDeIngreso = fechaDeIngreso;
}

@Override
public boolean equals(Object objeto) {
    if (objeto == null) {
        return false;
    }

    if (this == objeto) {
        return true;
    }

    if (objeto instanceof Venta) {
        Venta otraVenta = (Venta) objeto;
        if (otraVenta.getNumero_factura() == this.numero_factura) {
            return true;
        }
    }

    return false;
}

public Integer getNumero_factura() {
    return numero_factura;
}

public void setNumero_factura(Integer numero_factura) {
    this.numero_factura = numero_factura;
}

public Empleado getEmpleado() {
    return empleado;
}

public void setEmpleado(Empleado empleado) {
    this.empleado = empleado;
}

public List<Producto> getProductos() {
    return productos;
}

public void setProductos(List<Producto> productos) {
    this.productos = productos;
}

public Date getFechaDeIngreso() {
    return fechaDeIngreso;
}

public void setFechaDeIngreso(Date fechaDeIngreso) {
    this.fechaDeIngreso = fechaDeIngreso;
}

}

提前致谢。

2 个答案:

答案 0 :(得分:1)

您不想对意见的大小进行排序。你想根据他们的意见大小对电影进行排序:

movies.sort(Comparator.comparingInt(movie -> movie.getOpinions().size());

答案 1 :(得分:0)

您还没有为域类发布实际代码,但我猜你想要的是什么 Employee emp = emList.stream()。max((a,b) - &gt; a.getSellList()。size() - b.getSellList()。size());

System.out.pritntf(&#34;畅销书:%s,数量:%d&#34;,emp.getName(),empty.getSellList()。size());

主要编辑1: 在我了解了域之后,我正在改变我的代码,如下所示。我使用了自己的域类,即POJO而不是JPA。我已经测试了这段代码并且工作正常。

package com.grs.stackOverFlow.pack02;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PrimitiveIterator.OfDouble;
import java.util.stream.Collectors;

public class Pack02Demo{

    public static void main(String []args){
        List<Sell> sells = Sell.createSampleSellList(10);

        Map<Employee, List<Sell>> map = sells.stream().collect(Collectors.groupingBy(Sell::getEmp));

        System.out.println(map);

        Employee som=null;

        for(Entry<Employee, List<Sell>> entry: map.entrySet()){

            if(som==null){
                som=entry.getKey();
            }else{
                som=map.get(som).size()>entry.getValue().size()?som:(Employee) entry.getKey();
            }

        }

        System.out.printf("Seller of month id : %d , number of sells : %s",som.getId(),map.get(som).size());

    }

}

class Sell{
    private int id;
    private Employee emp;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Employee getEmp() {
        return emp;
    }

    public void setEmp(Employee emp) {
        this.emp = emp;
    }

    public String toString(){
        return String.format("{sell.id : %d, emp.id: %d %n }", id,emp.getId());
    }

    public static List<Sell> createSampleSellList(int listSize){
        List<Employee> employees=Employee.createSampleEmpList(listSize);
        List<Sell> sells=new ArrayList<>(listSize);

        for(int i=1; i<=listSize; i++){
            int id= (int)(Math.random() * employees.size()-1);
            Sell sell=new Sell();
            sell.setId(i);
            sell.setEmp(employees.get(id));
            sells.add(sell);
        }

        return sells;
    }
}

class Employee {
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Employee(Integer id) {
        super();
        this.id = id;
    }

    public String toString(){
        return String.format("{employee, id : %d }", id);
    }

    public boolean equals(Object object){
        if(object instanceof Employee){
            Employee emp=(Employee)object;
            return this.id==emp.getId();
        }
        else
            return false;
    }


    public int hashCode(){
        return new Integer(id).toString().length();
    }

    public static List<Employee> createSampleEmpList(int size){
        List<Employee> employees=new ArrayList<>(size);

        for(int i=1; i<=size; i++){
            Employee employee=new Employee(i);
            employees.add(employee);
        }
        return employees;
    }

}