从数据集中读取并放入对象
Read from dataset and place into objects
我有一个外部数据集,其中填充了以下数据:userid、itemid、rating。我想将此数据读入每个用户的用户首选项对象中。 userpreference 对象包含一个 userid、一个 itemid 数组和一个 ratings 数组。我选择了一个数组,因为性能需要尽可能快。我试图在没有对象的情况下做到这一点,而是使用了二维数组。这可行,但我希望它是 OOP 并使用对象。我被困在将数据放入对象中。
数据集的一个例子是:
1,101,2.5
1,106,3.0
2,101,3.0
4,106,4.5
4,103,3.0
1,103,3.0
1,104,3.5
5,102,4.0
我的代码是什么样的
public class Main {
public static void main(String[] args) throws IOException{
//UserPreference bestaat uit userid, itemid[] en preferences[].
UserPreference [][] rating = new UserPreference[8][107];
int[][] userPref = new int[8][107]; //2d array test
BufferedReader br = new BufferedReader(new FileReader("userinfo.data"));
String line = null;
while ((line = br.readLine()) != null) {
String[] userratings = line.split(",");
int userColumn = Integer.parseInt(userratings[0]);
int itemColumn = Integer.parseInt(userratings[1]);
double ratingColumn = Double.parseDouble(userratings[2]);
System.out.println(userColumn + " " + itemColumn + " " + ratingColumn);
rating[userColumn][itemColumn] = ratingColumn;
userPref[userColumn][itemColumn] = (int) ratingColumn; //2d array test
}
br.close();
System.out.println("Rating: " + userPref[1][101]); // this works as an example, but not OO and reads out as an int instead of a double
System.out.println("Rating: " + rating[1][101]); //this does not work, because ratingcolumn is no object.
} }
评级列未被识别为对象。我试着像我以前使用过的二维数组那样做,但它的工作方式不同。我希望有人能帮助我朝着正确的方向前进。
你好,
阿尔弗雷德
面向对象的解决方案可以是:
public static void main(final String[] args) throws IOException {
HashMap<Integer, User> users = new HashMap<Integer, User>();
BufferedReader br = new BufferedReader(new FileReader("userinfo.data"));
String line = null;
while ((line = br.readLine()) != null) {
String[] userratings = line.split(",");
int userColumn = Integer.parseInt(userratings[0]);
int itemColumn = Integer.parseInt(userratings[1]);
double ratingColumn = Double.parseDouble(userratings[2]);
User user = users.get(userColumn);
if (user == null) {
user = new User(userColumn);
users.put(userColumn, user);
}
user.add(new Item(itemColumn, ratingColumn));
}
br.close();
for (User user : users.values()) {
for (Item item : user.getItems()) {
System.out.println(user.getId() + "\t" + item.getId() + "\t" + item.getRating());
}
}
}
有了这两个类:
public class User {
int id;
ArrayList<Item> items;
public User(final int id) {
this.id = id;
items = new ArrayList<Item>();
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public void add(final Item item) {
items.add(item);
}
public ArrayList<Item> getItems() {
return items;
}
}
public class Item {
int id;
double rating;
public Item(final int id, final double rating) {
this.id = id;
this.rating = rating;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public double getRating() {
return rating;
}
public void setRating(final double rating) {
this.rating = rating;
}
}
我有一个外部数据集,其中填充了以下数据:userid、itemid、rating。我想将此数据读入每个用户的用户首选项对象中。 userpreference 对象包含一个 userid、一个 itemid 数组和一个 ratings 数组。我选择了一个数组,因为性能需要尽可能快。我试图在没有对象的情况下做到这一点,而是使用了二维数组。这可行,但我希望它是 OOP 并使用对象。我被困在将数据放入对象中。
数据集的一个例子是:
1,101,2.5
1,106,3.0
2,101,3.0
4,106,4.5
4,103,3.0
1,103,3.0
1,104,3.5
5,102,4.0
我的代码是什么样的
public class Main {
public static void main(String[] args) throws IOException{
//UserPreference bestaat uit userid, itemid[] en preferences[].
UserPreference [][] rating = new UserPreference[8][107];
int[][] userPref = new int[8][107]; //2d array test
BufferedReader br = new BufferedReader(new FileReader("userinfo.data"));
String line = null;
while ((line = br.readLine()) != null) {
String[] userratings = line.split(",");
int userColumn = Integer.parseInt(userratings[0]);
int itemColumn = Integer.parseInt(userratings[1]);
double ratingColumn = Double.parseDouble(userratings[2]);
System.out.println(userColumn + " " + itemColumn + " " + ratingColumn);
rating[userColumn][itemColumn] = ratingColumn;
userPref[userColumn][itemColumn] = (int) ratingColumn; //2d array test
}
br.close();
System.out.println("Rating: " + userPref[1][101]); // this works as an example, but not OO and reads out as an int instead of a double
System.out.println("Rating: " + rating[1][101]); //this does not work, because ratingcolumn is no object.
} }
评级列未被识别为对象。我试着像我以前使用过的二维数组那样做,但它的工作方式不同。我希望有人能帮助我朝着正确的方向前进。
你好,
阿尔弗雷德
面向对象的解决方案可以是:
public static void main(final String[] args) throws IOException {
HashMap<Integer, User> users = new HashMap<Integer, User>();
BufferedReader br = new BufferedReader(new FileReader("userinfo.data"));
String line = null;
while ((line = br.readLine()) != null) {
String[] userratings = line.split(",");
int userColumn = Integer.parseInt(userratings[0]);
int itemColumn = Integer.parseInt(userratings[1]);
double ratingColumn = Double.parseDouble(userratings[2]);
User user = users.get(userColumn);
if (user == null) {
user = new User(userColumn);
users.put(userColumn, user);
}
user.add(new Item(itemColumn, ratingColumn));
}
br.close();
for (User user : users.values()) {
for (Item item : user.getItems()) {
System.out.println(user.getId() + "\t" + item.getId() + "\t" + item.getRating());
}
}
}
有了这两个类:
public class User {
int id;
ArrayList<Item> items;
public User(final int id) {
this.id = id;
items = new ArrayList<Item>();
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public void add(final Item item) {
items.add(item);
}
public ArrayList<Item> getItems() {
return items;
}
}
public class Item {
int id;
double rating;
public Item(final int id, final double rating) {
this.id = id;
this.rating = rating;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public double getRating() {
return rating;
}
public void setRating(final double rating) {
this.rating = rating;
}
}