Is there a way to use each construct for a Guava table?

One of the purest benefits of coding modern collections is the ability to use each design. I have below a simple general method for printing a table, followed by a test load method. While this works, some will be much cleaner for everyone. Any ideas?

public void printTable(Table table) { int numRows = table.rowKeySet().size(); int numCols = table.columnKeySet().size(); for (int i=0; i<numRows; i++) { for (int j=0; j<numCols; j++) { System.out.print( table.get(i,j) + " " ); } System.out.println(); } } Table<Integer, Integer, Integer> table = HashBasedTable.create(); void makeTable() { for (int i=0; i<4; i++) for (int j=0; j<6; j++) table.put(i, j, i*j+2); } 
+6
source share
1 answer

Why don't you just call Map<R,Map<C,V>> rowMap() and Map<R,Map<C,V>> rowMap() over it?

Also, I think you might prefer TreeBasedTable , which takes into account the order of rows and columns, since you use integers for rows and columns, and it looks like you want iteration in natural order.

+5
source

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


All Articles