Giriş
Şu satırı dahil ederiz.
Table nesnesinin Java 8 Stream ile kullanılabilmesini sağlar. Bir Collector döner.
Birinci parametre rowFunction, ikinci parametre columnFunction, üçüncü parametre valueFunction, dördünce parametre mergeFunction, beşinci parametre tableSupplier olarak kullanılır.
mergeFunction aynı row ve column değerlerine sahip satırlarda value değerini hesaplamak içindir.
toTable metodu - rowFunction + columFunction + valueFunction + tableSupplier
Örnek
Elimizde şöyle bir kod olsun.
Elimizde iki tane table olsun.
Örnek
Şöyle yaparız.
Şöyle yaparız.
Şu satırı dahil ederiz.
import com.google.common.collect.Tables;Table nesnesinin Java 8 Stream ile kullanılabilmesini sağlar. Bir Collector döner.
Birinci parametre rowFunction, ikinci parametre columnFunction, üçüncü parametre valueFunction, dördünce parametre mergeFunction, beşinci parametre tableSupplier olarak kullanılır.
mergeFunction aynı row ve column değerlerine sahip satırlarda value değerini hesaplamak içindir.
toTable metodu - rowFunction + columFunction + valueFunction + tableSupplier
Örnek
Elimizde şöyle bir kod olsun.
List<ApplicationUsage> appUsageFromDB =Table<String, String, ApplicationUsage> table2 = appUsageFromDB.stream()
  .collect(Tables.toTable(
    ApplicationUsage::getId,
    ApplicationUsage::getName,
    au -> au,
    HashBasedTable::create
));Elimizde iki tane table olsun.
Table<Long,String,Integer> tableOne
Table<Long,String,Integer> tableTwo1L Fruits     20
2L Fruits     30
2L Vegetables 15
3L Vegetables 102L Fruits     10
2L Vegetables 40
3L Fruits     15
4L Vegetables 351L Fruits     20
2L Fruits     30 + 10 = 40
2L Vegetables 15 + 40 = 55
3L Vegetables 10
3L Fruits     15
4L Vegetables 35HashBasedTable<Long, String, Integer> sumTable = Stream.concat(
  tableOne.cellSet().stream(), tableTwo.cellSet().stream())
    .collect(Tables.toTable(Table.Cell::getRowKey,
      Table.Cell::getColumnKey,
      Table.Cell::getValue,
      Integer::sum, HashBasedTable::create));Örnek
Şöyle yaparız.
Table<Region, Country, List<City>> table = RECORDS.stream()
        .collect(toTable(
                r -> r.getRegion(),
                r -> r.getCountry(),
                r -> Lists.newArrayList(r.getCity()),
                (l, l2) -> {
                    l.addAll(l2);
                    return l;
                },
                HashBasedTable::create
        ));Şöyle yaparız.
Map<Integer, Integer> source = ...;
// compiles
Table<Integer, Integer, Integer> broker = source.entrySet().stream()
  .collect( Tables.toTable( e -> 0, e -> 0, e -> 0,
                    ( e1, e2 ) -> 0, HashBasedTable::create ) );
Örnek
Şöyle yaparız. row değeri owner, column değeri status ve value 1 olur. aynı row ve column değerleri için value toplanır.List<Thing> listOfThings = ...;
Table<Owner, Status, Long> table = 
  listOfThings.stream().collect(
    Tables.toTable(
      Thing::getOwner,             // Row key extractor
      Thing::getStatus,            // Column key extractor
      thing -> 1,                  // Value converter (a single value counts '1')
      (count1, count2) -> count1 + count2, // Value merger (counts add up)
      HashBasedTable::create       // Table creator
  )
); 
Hiç yorum yok:
Yorum Gönder