Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-dimensional Map Java

I need a data structure that I could store my information in a two dimensional way. For example imagine a table that contains user-item ratings. I need to store all ratings for all users. let's say, user u1. I need to store ratings for user u1 and u2 and u3 and all other users. But the problem is I also need too store all ratings for all items. For example I need to store ratings provided by all users for each item. So I need something like a map that for users the key is the user ID and the value is the set of ratings. I can do that easily. But my problem is how I can also store ratings for Items. for example a map that the key is the item ID and the value is the set of ratings provided be users for that Item. I wanted to upload a table but since I didn't have enough reputation I couldn't do that.So just imagine a table like a two dimensional matrix that rows are users and columns are items. Is there a data structure that can do that? Or I should build two different maps? maybe there is a better option than Map but Since I had to choose a title for my question I wrote map.

Thanks

like image 354
HimanAB Avatar asked Jan 29 '26 21:01

HimanAB


1 Answers

You can use the Table class from the free Guava library

Table<Integer, String, Double> table = HashBasedTable.create();

table.put(1, "a", 2.0);
double v = table.get(1, "a"); // getting 2.0
like image 137
Alexander Kulyakhtin Avatar answered Feb 01 '26 14:02

Alexander Kulyakhtin