Order and ways to group elements in linked lists

1

Hello, can someone help me with this problem? I would like to fully understand the handling of the linked lists and I stayed with this example. Make an application that allows to generate different lists related to a specific community of neighbors. For this we need to store the data of the different neighbors: name, surnames, block, door, monthly fee and the total amount owed to the community. The application will allow the following listings:  - Alphabetically organized list of all the neighbors of the community.  - List of neighbors ordered decreasingly by amount they owe.  - List of neighbors belonging to a certain block.  - List of neighbors ordered by the amount of the monthly fee they must pay.

I have done the first section but I do not know if I am doing it the right way. I appreciate the help.

Vecino.java

public class Vecino implements Comparable <Vecino> {
    private String nombre;
    private String apellidos;
    private int bloque;
    private char puerta;
    private double cuotaMensual;
    private double importeDebe;

    Vecino(String nombre, String apellidos, int bloque, char puerta, double cuotaMensual, double importeDebe){
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.bloque = bloque;
        this.puerta = puerta;
        this.cuotaMensual = cuotaMensual;
        this.importeDebe = importeDebe;
    }

    Vecino(){

    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @param nombre the nombre to set
     */
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * @return the apellidos
     */
    public String getApellidos() {
        return apellidos;
    }

    /**
     * @param apellidos the apellidos to set
     */
    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    /**
     * @return the bloque
     */
    public int getBloque() {
        return bloque;
    }

    /**
     * @param bloque the bloque to set
     */
    public void setBloque(int bloque) {
        this.bloque = bloque;
    }

    /**
     * @return the puerta
     */
    public char getPuerta() {
        return puerta;
    }

    /**
     * @param puerta the puerta to set
     */
    public void setPuerta(char puerta) {
        this.puerta = puerta;
    }

    /**
     * @return the cuotaMensual
     */
    public double getCuotaMensual() {
        return cuotaMensual;
    }

    /**
     * @param cuotaMensual the cuotaMensual to set
     */
    public void setCuotaMensual(double cuotaMensual) {
        this.cuotaMensual = cuotaMensual;
    }

    /**
     * @return the importeDebe
     */
    public double getImporteDebe() {
        return importeDebe;
    }

    /**
     * @param importeDebe the importeDebe to set
     */
    public void setImporteDebe(double importeDebe) {
        this.importeDebe = importeDebe;
    }

    @Override
    public int compareTo(Vecino o) {
        return this.nombre.compareTo(o.getNombre()); //To change body of generated methods, choose Tools | Templates.
    }

}

Main.java

public class Main {

    LinkedList<Vecino> vecinos = new LinkedList<>();

    public void mostrarTodosVecinos(LinkedList<Vecino> vecinos){
        System.out.println("Listado de vecinos ordenados alfabéticamente: ");

        Collections.sort(vecinos);

            for (Vecino vecino : vecinos) {
                System.out.println("Nombre: "+vecino.getNombre()+" "+vecino.getApellidos()+"\n"+
                                    "Bloque: "+vecino.getBloque()+"\n"+
                                    "Puerta: "+vecino.getPuerta()+"\n"+
                                    "Cuota mensual: "+vecino.getCuotaMensual()+"\n"+
                                    "Importe que debe: "+vecino.getImporteDebe()+"\n");
            }

    }


    public static void main(String[] args) {
        Main prueba = new Main();
        LinkedList<Vecino> vecinos = new LinkedList<>();
        Vecino veci = new Vecino("Jaime","Weirk",2,'A',20,40);
        Vecino veci1 = new Vecino("Laura","Gil",1,'C',20,20);
        Vecino veci2 = new Vecino("Mike","Aton",4,'B',20,60);
        Vecino veci3 = new Vecino("Billy","Willy",5,'D',20,10);
        vecinos.add(veci);
        vecinos.add(veci1);
        vecinos.add(veci2);
        vecinos.add(veci3);
        prueba.mostrarTodosVecinos(vecinos);

    }

}
    
asked by Nando 30.09.2016 в 03:16
source

1 answer

1

To order, the method you perform is a valid and correct option ... but I would like to propose another option that is the use of stream , for this you have to create the toString() method in your Neighbor class (You can modify it and format it as you wish)

public String toString() {
    return "Vecino{" + "nombre=" + nombre + ", apellidos=" + apellidos + ", bloque=" + bloque + ", puerta=" + puerta + ", cuotaMensual=" + cuotaMensual + ", importeDebe=" + importeDebe + '}'+ "\n";
}

To do the ordering and grouping, the code would be left

LinkedList<Vecino> vecinos = new LinkedList<>();
 ......

    /* Ordenados por Nombre */
    vecinos.sort((p1,p2) -> p1.getNombre().compareTo(p2.getNombre()));
    vecinos.forEach(p -> System.out.println(p.toString()));

    /* Ordenados Importe Debe */
    System.out.println("");
    vecinos.sort((p1,p2) -> Double.valueOf(p2.getImporteDebe()).compareTo(p1.getImporteDebe()));
    vecinos.forEach(p -> System.out.println(p.toString()));

    /* Ordenados Importe Mensual */
    System.out.println("");
    vecinos.sort((p1,p2) -> Double.valueOf(p2.getCuotaMensual()).compareTo(p1.getCuotaMensual()));
    vecinos.forEach(p -> System.out.println(p.toString()));

    /* Por Grupos */

     vecinos.stream()
    .collect(Collectors.groupingBy(foo -> foo.getBloque(),Collectors.toList()))
    .forEach((id,lista)->
            System.out.println("Grupo " +id+ "\n"+ lista.toString()));
  

As an observation, unless it is exclusive use it is better to use ArrayList instead of LinkedList , for better reference read this question in English but very interesting    link

    
answered by 30.09.2016 / 06:52
source