这是我的第一个问题,请耐心等待。我有一个看起来像这样的文本文件;
用户id,的itemId,评级
1,101,2.5
4,103,3.0
1,103,3.0
6,104,3.5
2,101,3.0
4,106,4.5
2,103,1.5
5,102,4.0
2,105,3.5
7,106,3.0
3,101,2.5
3,102,3.0
我想要完成的是每行读取文件行并分隔每个值。完成之后,将所有唯一的userIds放在此地图的关键部分; Map<Integer, Map<Integer, Double>>
。并将itemIds与第二个地图中的等级放在第一个地图的键对应的等级中。你还能跟着我吗?这是我到目前为止制作的代码而不是我想要的结果。
这是我的用户类:
package com.example;
import java.util.HashMap;
import java.util.Map;
public class User {
private int mUserId;
private Map<Integer, Double> mRatedItems;
public User(int userId) {
mUserId = userId;
mRatedItems = new HashMap<>();
}
public int getUserId() {
return mUserId;
}
public void setUserId(int userId) {
mUserId = userId;
}
public Map<Integer, Double> getRatedItems() {
return mRatedItems;
}
public void addRatedItems(int itemId, double rating) {
mRatedItems.put(itemId, rating);
}
}
这是我需要修改的文件:
package com.example;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class TextFile {
List<User> mUserIdList;
private User mUser;
public Map<Integer, Map<Integer, Double>> readFile(String fileName) {
mUserIdList = new ArrayList<>();
Map<Integer, Map<Integer, Double>> userItemData = new HashMap<>();
try (
FileInputStream fis = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis))
) {
String line;
while ((line = reader.readLine()) != null) {
String[] args = line.split(",");
int userId = Integer.parseInt(args[0]);
int itemId = Integer.parseInt(args[1]);
double rating = Double.parseDouble(args[2]);
if (!mUserIdList.contains(userId)) {
mUser = new User(userId);
mUserIdList.add(mUser);
}
if (!mUserIdList.isEmpty()) {
addItemRatings(userId, itemId, rating);
}
if (!checkDuplicates()) {
userItemData.put(mUser.getUserId(), mUser.getRatedItems());
}
}
} catch (IOException e) {
System.out.printf("Problem loading: %s %n", fileName);
e.printStackTrace();
}
return userItemData;
}
private boolean checkDuplicates() {
Set<Integer> set = new HashSet<>();
for (User user : mUserIdList) {
if (!set.add(user.getUserId())) {
return true;
}
}
return false;
}
public void addItemRatings(int userId, int itemId, double rating) {
for (User user : mUserIdList) {
if (user.getUserId() == userId) {
user.addRatedItems(itemId, rating);
}
}
}
public int showTextLines(String fileName) {
int textLines = 0;
try (
FileInputStream fis = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis))
) {
while (reader.readLine() != null) {
textLines++;
}
reader.close();
} catch (IOException e) {
System.out.printf("Problem showing total lines from: %s %n", fileName);
e.printStackTrace();
}
return textLines;
}
}
这是我的主要课程:
package com.example;
import java.util.Map;
public class Main {
public static void main(String[] args) {
TextFile textFile = new TextFile();
String location = "D:\\IdeaProjects\\KMeans\\src\\com\\quincy\\userItem1.data";
Map<Integer, Map<Integer, Double>> readFile = textFile.readFile(location);
int textLines = textFile.showTextLines(location);
System.out.printf("File: %s %n", readFile);
System.out.printf("Number of lines in file: %s %n", textLines);
}
}
我的输出是:
File: {1={101=2.5, 103=3.0}, 4={103=3.0, 106=4.5}}
Number of lines in file: 12
我的代码只添加前两个userIds以及相应的itemIds和rating。有人可以向我解释一下我的情况。我做错了?也许可以说明如何改进这段代码才能正常工作?
提前致谢。
答案 0 :(得分:0)
问题出在您的checkDuplicates()
方法中。我不知道你在该功能上想要实现什么,但是如果你想检查重复。那么,我建议你只使用这个函数containsKey()
。
if (!userItemData.containsKey(mUser.getUserId())) {
userItemData.put(mUser.getUserId(), mUser.getRatedItems());
}