mardi 7 février 2017

Split a list into a list

Suppose a list of objects (yourClass) that we have to divide into a list of lists and we do not want to cross the same to generate it.

We also want to reuse the class generically.

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class ListOfList {

    /**
     * Generates a list<list<yourClass>> from a list<YourClass>, made with
     * compare 1 field of your class
     * 
     * @param lista
     * @param clazz
     * @param field
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     */
    public static <T> List<List<T>> generateSuperList(List<T> lista, Class<T> clazz, String field)
            throws SecurityException, NoSuchMethodException {
        List<List<T>> superLista = new ArrayList<List<T>>();
        return generateSuperListPrivate(lista, superLista, clazz, field);
    }

    /**
     * recursive metod.
     * 
     * @param lista
     * @param superLista
     * @param clazz
     * @param field
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     */
    private static <T> List<List<T>> generateSuperListPrivate(List<T> lista, List<List<T>> superLista, Class<T> clazz,
            String field) throws SecurityException, NoSuchMethodException {

        superLista.add(filteredList(lista, clazz, field));

        if (lista.size() > 0)
            generateSuperListPrivate(lista, superLista, clazz, field);

        return superLista;
    }

    /**
     * Method that extract to aux list<T> the elements with condition ok and
     * erase from list.
     * 
     * @param lista
     * @param clazz
     * @param field
     * @return
     * @throws SecurityException
     * @throws NoSuchMethodException
     */
    private static <T> List<T> filteredList(List<T> lista, Class<T> clazz, String field)
            throws SecurityException, NoSuchMethodException {

        List<T> aux = new ArrayList<T>();
        if (lista.size() > 0) {
            try {
                Predicate<T> first = p -> GetPropValue(p, field).equals(GetPropValue(lista.get(0), field));
                aux = lista.stream().filter(first).collect(Collectors.toList());
                if (aux.size() > 0) {
                    lista.removeAll(aux);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return aux;
    }

    /**
     * get reflection property value
     * 
     * @param src
     * @param propName
     * @return
     */
    private static <T> Object GetPropValue(Object src, String propName) {
        Object o = null;
        try {
            Method method = src.getClass().getMethod("get" + propName, new Class[] {});
            o = method.invoke(src);

        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            e.printStackTrace();
        }

        return o;
    }

}

An example of how to use this class.

public class Main {

    public Main() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        List<Person> lista = new ArrayList<Person>();
        lista.add(new Person(1, "S1"));
        lista.add(new Person(1, "S2"));
        lista.add(new Person(1, "S3"));
        lista.add(new Person(1, "S4"));
        lista.add(new Person(2, "E3"));
        lista.add(new Person(2, "E4"));
        lista.add(new Person(2, "E5"));
        lista.add(new Person(3, "F1"));
        lista.add(new Person(1, "S5"));
        lista.add(new Person(2, "E1"));
        lista.add(new Person(2, "E2"));

        List<Person> lista2 = new ArrayList<Person>();
        lista2.addAll(lista);
        try {
            List<List<Person>> lista3 = ListOfList.generateSuperList(lista2, Person.class, "Id");
            System.out.println(lista3);
        } catch (SecurityException e) {
            // TODO Bloque catch generado automáticamente
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Bloque catch generado automáticamente
            e.printStackTrace();
        }
    }   
}

and the class Person

public class Person {

    private Integer id;
    private String nombre;

    public Person(Integer id, String nombre) {
        super();
        this.id = id;
        this.nombre = nombre;
    }

    public Integer getId() {
        return id;
    }

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

    public String getNombre() {
        return nombre;
    }

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

}





Aucun commentaire:

Enregistrer un commentaire