Sono stato alle prese con questo problema per alcuni giorni. Sto cercando di creare la funzionalità di Pivot utilizzando i flussi Java. Devo solo implementare SUM, COUNT, MAX, MIN e MEDIA. Per l'input mi viene fornito un indice di colonna pivot, una matrice di indici di riga pivot e il valore da calcolare.Implementazione della tabella pivot Java utilizzando gli stream
Il catch è che i dati sono in un elenco < Elenco < Oggetto >>, dove l'oggetto può essere String, Integer o Double. ma non lo saprò fino al runtime. E devo restituire i risultati come Elenco < Elenco < Oggetto >>.
Ho problemi con MAX/MIN (Sto supponendo che MEDIA sarà simile a MAX e MIN)
Per ruotare su più valori di tabella, ho creato una classe di utilizzare la mia seconda groupingBy
Questo non verrà compilato, non sono sicuro su cosa confrontare, in cui convertire l'oggetto in int o se è necessario. Mi piacerebbe farlo tutto con un flusso, ma non sono sicuro che sia possibile. Cosa sto facendo di sbagliato, o potrei farlo in modo diverso. Grazie in anticipo.
package pivot.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class PivotTest {
List<List<Object>> rows = new ArrayList<List<Object>>();
public PivotTest() throws Exception {
rows.add(Arrays.asList(new Object[]{ "East", "Boy", "Tee", 10, 12.00}));
rows.add(Arrays.asList(new Object[]{ "East", "Boy", "Golf", 15, 20.00}));
rows.add(Arrays.asList(new Object[]{ "East", "Girl", "Tee", 8, 14.00}));
rows.add(Arrays.asList(new Object[]{ "East", "Girl", "Golf", 20, 24.00}));
rows.add(Arrays.asList(new Object[]{ "West", "Boy", "Tee", 5, 12.00}));
rows.add(Arrays.asList(new Object[]{ "West", "Boy", "Golf", 12, 20.00}));
rows.add(Arrays.asList(new Object[]{ "West", "Girl", "Tee", 15, 14.00}));
rows.add(Arrays.asList(new Object[]{ "West", "Girl", "Golf", 10, 24.00}));
}
// Dynamic Max based upon Column, Value to sum, and an array of pivot rows
public void MaxTable(int colIdx, int valueIdx, int... rowIdx) {
Map<Object, Map<Object, Integer>> myList = newRows.stream().collect(
Collectors.groupingBy(r -> ((List<Object>) r).get(colIdx),
Collectors.groupingBy(r -> new PivotColumns(r, rowIdx),
Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(???)),
r -> ((List<Object>) r).get(valueIdx)))));
System.out.println("Dynamic MAX PIVOT"); System.out.println(myList);
}
public static void main(String[] args) {
try {
PivotTest p = new PivotTest();
System.out.println("\n\nStreams PIVOT with index values inside a List\n");
p.MaxTable(0, 3, new int[] { 2 });
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class PivotColumns {
ArrayList<Object> columns;
public PivotColumns(
List<Object> objs, int... pRows) {
columns = new ArrayList<Object>();
for (int i = 0; i < pRows.length; i++) {
columns.add(objs.get(pRows[i]));
}
}
public void addObject(Object obj) {
columns.add(obj);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((columns == null) ? 0 : columns.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PivotColumns other = (PivotColumns) obj;
if (columns == null) {
if (other.columns != null)
return false;
} else if (!columns.equals(other.columns))
return false;
return true;
}
public String toString() {
String s = "";
for (Object obj : columns) {
s += obj + ",";
}
return s.substring(0, s.lastIndexOf(','));
}
}
Questa è una * enorme * domanda. Potresti voler leggere le domande * Minime *. Vedi http://stackoverflow.com/help/mcve --- Non sono andato molto lontano, ma hai detto che hai un 'Elenco' di oggetti, sia' String', 'Integer', o' Double' e che non lo saprai fino al runtime, ma poi continuerai a mostrare una classe 'Row' ben definita e completamente digitata. Quindi, qual è, lo sai o no? – Andreas
Un motivo particolare per cui * deve * essere flussi? – Andreas
No. Sono completamente aperto ad altre soluzioni che non coinvolgono flussi. –