2017-06-11 3 views
0

Je crée un tableau à partir d'un fichier CSV avec des données comprenant des notations des utilisateurs pour différents articles datasetRecherche Goyave Table par Valeur

J'utilise le code suivant pour remplir le tableau

Reader in = new FileReader(OriginalRatingDataPath); 
 
Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in); 
 

 
Table<Integer,Integer,Integer> ratings = HashBasedTable.create(); 
 
for (CSVRecord record : records) { 
 
    ratings.put(Integer.parseInt(record.get("userID")),Integer.parseInt(record.get("itemID")),Integer.parseInt(record.get("rating"))); 
 
}

Comment puis-je interroger la table pour obtenir les utilisateurs ayant noté les éléments 3 à 5?

Répondre

1

Option 1

Le point d'une table est de vous permettre d'accéder à des documents soit par ligne ou colonne. Donc, si vous avez besoin d'un accès par évaluation, la méthode la plus simple consiste à utiliser la notation en tant que colonne.

Reader in = new FileReader(OriginalRatingDataPath); 
Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in); 

Table<Integer,Integer,Integer> ratings = HashBasedTable.create(); 
for (CSVRecord record : records) { 
    ratings.put(Integer.parseInt(record.get("userID")), Integer.parseInt(record.get("rating")), Integer.parseInt(record.get("itemID"))); 
} 

// Copy column 3 to a new map to prevent editing the table itself 
// when adding columns 4 and 5 - in turn preventing a memory leak 
// from indirectly holding onto `ratings` through the map 
Map<Integer, Integer> usersToItemId = new HashMap<>(ratings.column(3)); 
usersToItemId.putAll(ratings.column(4)); 
usersToItemId.putAll(ratings.column(5)); 

// To get just User IDs 
Set<Integer> userIds = usersToItemId.keySet(); 

Option 2

Si la majorité de vos opérations vont être accéder à la table par itemID et userID vous ne voulez pas faire la colonne notation. Dans ce cas, vous pouvez utiliser la méthode cellSet et parcourir manuellement la table. Ses performances ne seront pas aussi bonnes mais cela fonctionnera.

// Your current code 

Set<Integer> userIds = new HashSet<>(); 
for (Table.Cell<Integer, Integer, Integer> cell : ratings.cellSet()) { 
    if (3 <= cell.getValue() && cell.getValue() <= 5) { 
     userIds.add(cell.getRowKey()); 
    } 
}