Ver Mensaje Individual
  #5 (permalink)  
Antiguo 26/10/2013, 15:46
MarronsDispatcher
 
Fecha de Ingreso: noviembre-2012
Mensajes: 40
Antigüedad: 11 años, 6 meses
Puntos: 7
Respuesta: Metodos de Ordenamiento

He aquí un pequeño ejemplo de como ordenar implementando la interfaz comparable, he creado el pojo autobús, he determinado que el número de plazas es el atributo para determinar cual es mayor que cual, aunque podrían ser varios atributos, y he realizado una clase main pintando el antes y el después.

Pojo Autobus:
Código Java:
Ver original
  1. /**
  2.  *
  3.  */
  4. package main.collection.sort;
  5.  
  6. /**
  7.  * @author forosDelWeb
  8.  * @since 26-10-2013
  9.  *
  10.  */
  11. public class Autobus implements Comparable<Autobus> {
  12.  
  13.     private static final String SALTO_LINEA = "\n";
  14.     private static final String ESPACIO = " ";
  15.  
  16.     private int numPlazas;
  17.     private String matricula;
  18.     private String modelo;
  19.     private String marca;
  20.  
  21.     /**
  22.      * Constructor por defecto
  23.      */
  24.     public Autobus() {
  25.     }
  26.  
  27.     /**
  28.      * Que estupido me senti con este javaDoc.
  29.      *
  30.      * @param numPlazas numero de plazas del autobus
  31.      * @param matricula matricula del autobus
  32.      * @param modelo el modelo del autobus
  33.      * @param marca la marca del autobus
  34.      */
  35.     public Autobus(int numPlazas, String matricula, String modelo, String marca) {
  36.         this.numPlazas = numPlazas;
  37.         this.marca = marca;
  38.         this.matricula = matricula;
  39.         this.modelo = modelo;
  40.     }
  41.  
  42.     /**
  43.      * @return the numPlazas
  44.      */
  45.     public int getNumPlazas() {
  46.         return this.numPlazas;
  47.     }
  48.  
  49.     /**
  50.      * @param numPlazas the numPlazas to set
  51.      */
  52.     public void setNumPlazas(int numPlazas) {
  53.         this.numPlazas = numPlazas;
  54.     }
  55.  
  56.     /**
  57.      * @return the matricula
  58.      */
  59.     public String getMatricula() {
  60.         return this.matricula;
  61.     }
  62.  
  63.     /**
  64.      * @param matricula the matricula to set
  65.      */
  66.     public void setMatricula(String matricula) {
  67.         this.matricula = matricula;
  68.     }
  69.  
  70.     /**
  71.      * @return the modelo
  72.      */
  73.     public String getModelo() {
  74.         return this.modelo;
  75.     }
  76.  
  77.     /**
  78.      * @param modelo the modelo to set
  79.      */
  80.     public void setModelo(String modelo) {
  81.         this.modelo = modelo;
  82.     }
  83.  
  84.     /**
  85.      * @return the marca
  86.      */
  87.     public String getMarca() {
  88.         return this.marca;
  89.     }
  90.  
  91.     /**
  92.      * @param marca the marca to set
  93.      */
  94.     public void setMarca(String marca) {
  95.         this.marca = marca;
  96.     }
  97.  
  98.     /*
  99.      * (non-Javadoc)
  100.      *
  101.      * @see java.lang.Object#toString()
  102.      */
  103.     @Override
  104.     public String toString() {
  105.         StringBuilder strAutobus = new StringBuilder();
  106.  
  107.         strAutobus.append("Numero de plazas : ").append(this.numPlazas).append(Autobus.ESPACIO);
  108.         strAutobus.append("Marca : ").append(this.marca).append(Autobus.ESPACIO);
  109.         strAutobus.append("Modelo : ").append(this.modelo).append(Autobus.ESPACIO);
  110.         strAutobus.append("Matricula : ").append(this.matricula).append(Autobus.SALTO_LINEA);
  111.  
  112.         return strAutobus.toString();
  113.     }
  114.  
  115.     /**
  116.      * @param objAutobus objeto con el que comparar
  117.      * @return <code>resComparacion</code> si resComparacion = 0 los objetos son iguales, si resComparacion > 0 el objeto this es menor que el
  118.      *         especificado por argumento si resComparacion > 0 el objetos this es mayor que el especificado por argumento
  119.      */
  120.     @Override()
  121.     public int compareTo(final Autobus objAutobus) {
  122.         int resComparacion = 0;
  123.         if (this.numPlazas > objAutobus.getNumPlazas()) {
  124.             resComparacion = 1;
  125.         }
  126.         else if (this.numPlazas < objAutobus.getNumPlazas()) {
  127.             resComparacion = -1;
  128.         }
  129.         return resComparacion;
  130.     }
  131. }


Clase main :
Código Java:
Ver original
  1. package main.collection.sort;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. /**
  10.  * 1-Creamos una lita de objetos autobus 2-Utilizaremos el metodo sort de CollectionUtils para ordenar 3-Pintaremos el resultado por pantalla
  11.  * utilizando el metodo sobrescrito toString
  12.  *
  13.  *
  14.  * @author forosDelWeb
  15.  * @since 26-10-2013
  16.  *
  17.  */
  18. public class CollectionSort {
  19.  
  20.     private static final String CLASE = "CollecionSort";
  21.  
  22.     private static final int NUM_PLAZAS_3 = 3;
  23.     private static final int NUM_PLAZAS_5 = 5;
  24.     private static final int NUM_PLAZAS_7 = 7;
  25.     private static final int NUM_PLAZAS_10 = 10;
  26.  
  27.     private static final String MATRICULA_BASICA = "MATRICULA-XXX";
  28.     private static final String MODELO_BASICO = "MODELO-YYY";
  29.     private static final String MARCA_BASICA = "MARCA-ZZZ";
  30.  
  31.     public static void main(String[] args) {
  32.         List<Autobus> lstAutobus = new ArrayList<Autobus>();
  33.         lstAutobus.add(new Autobus(CollectionSort.NUM_PLAZAS_5, CollectionSort.MATRICULA_BASICA, CollectionSort.MODELO_BASICO,
  34.                 CollectionSort.MARCA_BASICA));
  35.         lstAutobus.add(new Autobus(CollectionSort.NUM_PLAZAS_3, CollectionSort.MATRICULA_BASICA, CollectionSort.MODELO_BASICO,
  36.                 CollectionSort.MARCA_BASICA));
  37.         lstAutobus.add(new Autobus(CollectionSort.NUM_PLAZAS_7, CollectionSort.MATRICULA_BASICA, CollectionSort.MODELO_BASICO,
  38.                 CollectionSort.MARCA_BASICA));
  39.         lstAutobus.add(new Autobus(CollectionSort.NUM_PLAZAS_10, CollectionSort.MATRICULA_BASICA, CollectionSort.MODELO_BASICO,
  40.                 CollectionSort.MARCA_BASICA));
  41.         Logger.getLogger(CollectionSort.CLASE).log(Level.INFO, "Lista desordenada");
  42.         Logger.getLogger(CollectionSort.CLASE).log(Level.INFO, lstAutobus.toString());
  43.         Collections.sort(lstAutobus);
  44.         Logger.getLogger(CollectionSort.CLASE).log(Level.INFO, "Lista ordenada");
  45.         Logger.getLogger(CollectionSort.CLASE).log(Level.INFO, lstAutobus.toString());
  46.     }
  47.  
  48.  
  49. }