Skip to content

Instantly share code, notes, and snippets.

@aaccioly
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaccioly/9d62f468a515ae1530de to your computer and use it in GitHub Desktop.
Save aaccioly/9d62f468a515ae1530de to your computer and use it in GitHub Desktop.
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* Created by Anthony on 11/10/2014.
*/
public class Teste {
public static void main(String[] args) {
final List<Object[]> lista = new ArrayList<>();
lista.add(new Object[] {new BigDecimal("45"), new BigDecimal("100"), new BigDecimal("200")});
lista.add(new Object[] {new BigDecimal("45"), new BigDecimal("200"), new BigDecimal("400")});
lista.add(new Object[] {new BigDecimal("50"), new BigDecimal("30"), new BigDecimal("60")});
final Map<BigDecimal, Object[]> total = calcularTotal(lista, 0, 1, 2);
total.forEach((key, values) -> {
System.out.print(key + ":");
Arrays.stream(values).forEach(value -> System.out.print(" " + value));
System.out.println();
});
}
public static class Holder {
/**
* Segundo passo da redução.
* @return soma colunas selecionadas
*/
public static Object[] combine(Object[] first, Object[] second) {
return IntStream.range(0, first.length).mapToObj(i -> {
final BigDecimal x = (BigDecimal) first[i];
final BigDecimal y = (BigDecimal) second[i];
return x.add(y);
}).toArray();
}
final BigDecimal key;
final Object[] selectedValues;
/**
* Passo do mapeamento.
*
* @param values entradas cruas
* @param keyIndex indice da chave
* @param valueIndexes indices selecionados
*/
public Holder(Object[] values, int keyIndex, int... valueIndexes) {
this.key = (BigDecimal) values[keyIndex];
this.selectedValues = Arrays
.stream(valueIndexes)
.mapToObj(i -> values[i])
.toArray();
}
/**
* Elemento neutro para a redução.
* @param size quantidade de colunas no elemento neutro
*/
public Holder(int size) {
this.key = null;
this.selectedValues = new Object[size];
Arrays.fill(selectedValues, BigDecimal.ZERO);
}
public Object[] getSelectedValues() {
return selectedValues;
}
public BigDecimal getKey() {
return key;
}
}
public static Map<BigDecimal, Object[]> calcularTotal(List<Object[]> lista, int chave, int... valores) {
final Map<BigDecimal, Object[]> results = lista
.stream()
.map(o -> new Holder(o,chave, valores))
.collect(
Collectors.groupingBy(
Holder::getKey,
Collectors.reducing(
new Holder(valores.length).getSelectedValues(),
Holder::getSelectedValues,
Holder::combine)));
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment