Removing a whole row/column from a Table

3,538 views
Skip to first unread message

Clément DENIS

unread,
Jun 29, 2011, 12:20:37 PM6/29/11
to guava-...@googlegroups.com
I recently had to remove a whole row/column from a Table. The Table API does not provide a method to do it.

It looks like clearing the map returned by the Table.row/column() method does the trick.

   public static <R, C, V> void removeRow(Table<R, C, V> table, R rowKey) {
        table.row(rowKey).clear();
    }

    public static <R, C, V> void removeColumn(Table<R, C, V> table, C columnKey) {
        table.column(columnKey).clear();
    }

I suppose it is the right way to do it ... 

Maybe you could add a hint in the Javadoc, or add removeColumn and removeRow to the API ?
Message has been deleted

Pete Gillin

unread,
Oct 21, 2015, 6:27:13 AM10/21/15
to matthewj...@gmail.com, guava-discuss
Matthew, could you give an example to show what you mean by "HashBasedTable supports this only for rows"? The following works fine for me.

    Table<String, Integer, Double> table = HashBasedTable.create();
    table.put("a", 1, 1.2);
    table.put("a", 2, 3.4);
    table.put("a", 3, 5.6);
    table.put("b", 2, 7.8);
    table.put("c", 2, 9.0);
    table.row("a").clear();
    table.column(2).clear();
    assertThat(table).isEmpty();

So the O.P.'s code should be fine. (It's true that the Map view you get from column() has iterators that don't support remove(), but mutation operations on the Map view itself should be fine. This is specified in the javadoc.)

Pete.

On Wed, 21 Oct 2015 at 06:56 <matthewj...@gmail.com> wrote:
In case anyone else stumbles upon this:
This behavior is dependent upon the particular implementation you are using. For example,  HashBasedTable supports this only for rows. NotSupportedException is thrown if an attempted modification is invalid.

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/31980c85-4b8c-44aa-b02a-a34b5344baec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages