Get all keys by the value of a specific table column

I have a view of tabular data in Java. So far I use Google Guava to store data in Table<R,C,V>.

Given this table

|----------------------------------------------|
| Artist          | Genre    | Type            |
|----------------------------------------------|
| Eddie Van Halen | Musician | Hard Rock       |
| David Bowie     | Musician | Art Pop         |
| David Lynch     | Director | Surrealist Film |
| George Lucas    | Director | Blockbuster     |
|----------------------------------------------|

I use this

Table<String, String, String> table = HashBasedTable.create();
table.put("Eddie Van Halen", "Genre", "Musician");
table.put("Eddie Van Halen", "Type",  "Hard Rock");

table.put("David Bowie", "Genre", "Musician");
table.put("David Bowie", "Type",  "Art Pop");

table.put("David Lynch", "Genre", "Director");
table.put("David Lynch", "Type",  "Surrealist Film");

table.put("George Lucas", "Genre", "Director");
table.put("George Lucas", "Type",  "Blockbuster");

to create a table object.

It is very easy to get data using the row or colum keys. So I could get the David Bowie genre:

table.get("David Bowie", "Genre"); // "Musician"

Also I can get all the data of a specific column:

table.column("Genre").values(); // [Musician, Director, Musician, Director]

My main interest is to get all the artists who are directors. In this case, it is a search by value. There are 2-dimensional Maps, such as BidiMapfrom the Apache collections or BiMapfrom Google Guava.

But are there implementations that help get a set of row keys by the value of tabular data?

I would like to find something like

Set<R> table.getRowsByValue( C columnKey, V value )

- :

table.getRowsByValue("Genre", "Musician"); // [David Bowie, Eddie Van Halen]

1

, Map, .
:

    BidiMap bidiMap = new DualHashBidiMap<String,String>(table.column("Genre")); 
    bidiMap.getKey("Musician"); // "David Bowie"

Apache Colletions - .

    BiMap biMap = HashBiMap.create(table.column("Genre"));
    biMap.inverse().get("Musician");

BiMap Google Guava () :

java.lang.IllegalArgumentException: value already present: Musician
+4
1

Map.entrySet(), Map.Entry.getKey().

, , cellSet() Set<Table.Cell<R,C,V>>

java 8 (, , ):

table.cellSet().stream()
   .filter(tc -> tc.getColumnKey().equals("Director"))
   .collect(Collectors.toList());
0

Source: https://habr.com/ru/post/1618581/


All Articles