24 Nisan 2020 Cuma

Table Arayüzü

Giriş
Şu satırı dahil ederiz.
import com.google.common.collect.Table;
Açıklaması şöyle. Bir çeşit MultiKeyMap gerçekleştirimidir.
We have a very nice implementation of a two-tiered map , which we call a "table" (K1 is the "row key" and K2 is the "column key"),
Açıklaması şöyle.
If a row-column-combination can't be found, it returns null...
Bu arayüzü gerçekleştiren sınıflar şöyle
HashBasedTable
TreeBasedTable

Tanımlama
Şöyle yaparız.
Table<Integer, Integer, Double> pTable;
constructor
Constructor olarak Tables sınıfı da kullanılabilir.
Örnek
Şöyle yaparız.
Table<Integer, Integer, Integer> destination = HashBasedTable.create();
Örnek
Şöyle yaparız.
Table<Row, Column, Double> rowWeightedColumnWeights = HashBasedTable.create();
columnMap metodu
Açıklaması şöyle. Yani 2. ve 3. sütunları birleştiren bir view döner.
Returns a view that associates each column key with the corresponding map from row keys to values.
put metodu
İmzası şöyle.
public V put(R rowKey, C columnKey, V value);
putAll metodu
Örnek
Şöyle yaparız.
Table<Integer, Integer, Integer> destination = ...
// compiles
Table<Integer, Integer, Integer> broker = ...;
destination.putAll(broker);
rowMap metodu
Açıklaması şöyle. Yani 1. ve 3. sütunları birleştiren bir view döner.
Returns a view that associates each row key with the corresponding map from column keys to values.
Genellikle rowMap().entrySet().stream() şeklinde kullanılır. Bu şekliyle aynı değere sahip tüm satırlardaki değerlere aynı anda erişiriz.
Örnek
Şöyle yaparız. Tüm satırları dolaşırız.
table.rowMap().forEach((rowKey, row) -> {
  row.forEach((colKey, value) -> {
    // Do something for each row key/col key/value.
  });
});
Örnek
Şöyle yaparız.
Table<Row, Column, Double> columnWeightsPerRow = ...;

return columnWeightsPerRow
        .rowMap()
        .entrySet()
        .stream()
        .collect(...);
Örnek
Şöyle yaparız. Tüm rowMap().entrySet().stream() ile aynı key değerine sahip tüm satırları dolaşırız.
e.getValue().values() ile aynı key değerine sahip tüm value nesnelerine erişiriz.
final Table<String, String, Double> graph = HashBasedTable.create();

graph.put("A", "FirstCol", 0.0);
graph.put("A", "SecondCol", 1.0);
graph.put("B", "FirstCol", 0.1);
graph.put("B", "SecondCol", 1.1);

final Appendable out = new StringBuilder();
try {
    final CSVPrinter printer = CSVFormat.DEFAULT.print(out);

    printer.printRecords(graph.rowMap().entrySet()
      .stream()
      .map(entry -> ImmutableList.builder()
            .add(entry.getKey())
            .addAll(entry.getValue().values())
            .build())
      .collect(Collectors.toList()));

} catch (final IOException e) {
    e.printStackTrace();
}

System.out.println(out);
Çıktı olarak şunu alırız
A,0.0,1.0
B,0.1,1.1