28/10/2014, 18:44
|
| | Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 14 años, 6 meses Puntos: 5 | |
Respuesta: ordenar y filtrar hashmap multidimensional Wenas puedes usar de esta forma la clase producto
Código:
import java.util.Comparator;
import com.test.Person;
public class Producto {
private long id;
private String marca;
private String rubro;
private String descripcion;
private float precio;
public Producto(){
}
public Producto(long id,String marca,String rubro,String desc,float price){
this.id=id;
this.marca=marca;
this.rubro=rubro;
this.descripcion=desc;
this.precio=price;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMarca() {
return marca;
}
public void setMarca(String marca) {
this.marca = marca;
}
public float getPrecio() {
return precio;
}
public void setPrecio(float precio) {
this.precio = precio;
}
public String getRubro() {
return rubro;
}
public void setRubro(String rubro) {
this.rubro = rubro;
}
public String toString() {
//return id + " " + marca + " " + rubro + " " +descripcion + " " +precio;
return String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s",id,marca,rubro,descripcion,precio);
}
// Comparator
public static class CompId implements Comparator<Producto> {
public int compare(Producto arg0, Producto arg1) {
return (int) (arg0.id - arg1.id);
}
}
public static class CompMarca implements Comparator<Producto> {
public int compare(Producto arg0,Producto arg1) {
// TODO Auto-generated method stub
return arg0.marca.compareTo(arg1.getMarca());
}
}
public static class CompRubro implements Comparator<Producto> {
private boolean orderby=false;
public CompRubro(boolean desc){
this.orderby=desc;
}
public int compare(Producto arg0,Producto arg1) {
// TODO Auto-generated method stub
int i=0;
if(orderby){
i=arg0.rubro.compareTo(arg1.getRubro());
}else{
i=arg1.rubro.compareTo(arg0.getRubro());
}
return i;//arg0.rubro.compareTo(arg1.getRubro());
}
}
public static class CompPrice implements Comparator<Producto> {
private float mod = 1;
public CompPrice(boolean desc) {
if (desc) mod =-1;
}
public int compare(Producto arg0,Producto arg1) {
// TODO Auto-generated method stub
return (int) ((int) mod*(arg0.precio - arg1.precio));
}
}
}
Y en el main usar esto
Código:
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import com.test.Person;
public class testProductosort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Producto p = null;
Producto p1 = new Producto();
List<Producto> lstpro = new ArrayList<Producto>();
lstpro.add(new Producto(4,"palmolive","jabon","jabon bano",5.80f));
lstpro.add(new Producto(9,"dove","jabon","jabon bano",7.80f));
lstpro.add(new Producto(1,"escudo","jabon","jabon bano",8.50f));
lstpro.add(new Producto(5,"palma","jabon","jabon bano",10.50f));
lstpro.add(new Producto(2,"palma","shampo","jabon bano",10.50f));
lstpro.add(new Producto(6,"acros","lavadora","lavadora 11kgs",89.50f));
lstpro.add(new Producto(8,"whirpol","lavadora","lavadora 15kgs",150.50f));
lstpro.add(new Producto(3,"samsung","lavadora","lavadora 15kgs",148.50f));
lstpro.add(new Producto(3,"acros","estufa","estufa 4 quemadores",248.50f));
System.out.println("-----ANTES DE ORDENAR----");
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
System.out.println("-----DESPUES DE ORDENAR BY ID----");
Collections.sort(lstpro,new Producto.CompId());
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
System.out.println("-----DESPUES DE ORDENAR BY MARCA----");
Collections.sort(lstpro,new Producto.CompMarca());
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
System.out.println("-----DESPUES DE ORDENAR BY PRECIO ASC----");
Collections.sort(lstpro,new Producto.CompPrice(false));
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
System.out.println("-----DESPUES DE ORDENAR BY PRECIO DESC----");
Collections.sort(lstpro,new Producto.CompPrice(true));
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
System.out.println("-----DESPUES DE ORDENAR BY RUBRO ASC----");
Collections.sort(lstpro,new Producto.CompRubro(false));
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
System.out.println("-----DESPUES DE ORDENAR BY RUBRO DESC----");
Collections.sort(lstpro,new Producto.CompRubro(true));
System.out.println(String.format("%-16.20s %-30.30s %-20.30s %-25.30s %-20.30s","ID","MARCA","RUBRO","DESCRIPCION","PRECIO")+"\n");
for (Producto pr : lstpro) {
System.out.println(pr.toString());
}
}
}
Saludos, espero te oriente como dijo el Profe |