匹配 List 中的 id 并获取字符串 - Java
Match the id from List and get the String - Java
我必须获取 UserName -> firstname 和 lastname,其中 ID 与 UserIN 列表相匹配。
我不知道该怎么做,我认为 for 循环不好,我们可以通过 Collecions 来避免不好的实现。
有什么解决办法吗?
用户输入 class:
public class UserIN {
int id;
UserName name;
@Override
public String toString() {
return "UserIN{" +
"id=" + id +
", name=" + name +
'}';
}
public UserIN(int id, UserName name) {
this.id = id;
this.name = name;
}
// getters & setters here
}
用户名class:
public class UserName {
String firstname;
String lastname;
@Override
public String toString() {
return "UserName{" +
"firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
'}';
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public UserName() {}
public UserName(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
如果你想避免循环,你可以使用Map
import java.util.*;
...
// Maps user ids (integer) to UserName objects:
Map<Integer, UserName> map = new HashMap<>();
// Create two users and put them in the map:
UserIN user0 = new UserIN(0, new UserName("Peter", "Woodbridge"));
UserIN user1 = new UserIN(1, new UserName("Anne", "Miller"));
map.put(user0.id, user0.name);
map.put(user1.id, user1.name);
// Get user with id==1 from the map and print it to standard output
UserName user = map.get(1);
System.out.println(user.firstname + " " + user.lastname);
这会打印:
Anne Miller
我必须获取 UserName -> firstname 和 lastname,其中 ID 与 UserIN 列表相匹配。
我不知道该怎么做,我认为 for 循环不好,我们可以通过 Collecions 来避免不好的实现。
有什么解决办法吗?
用户输入 class:
public class UserIN {
int id;
UserName name;
@Override
public String toString() {
return "UserIN{" +
"id=" + id +
", name=" + name +
'}';
}
public UserIN(int id, UserName name) {
this.id = id;
this.name = name;
}
// getters & setters here
}
用户名class:
public class UserName {
String firstname;
String lastname;
@Override
public String toString() {
return "UserName{" +
"firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
'}';
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public UserName() {}
public UserName(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
如果你想避免循环,你可以使用Map
import java.util.*;
...
// Maps user ids (integer) to UserName objects:
Map<Integer, UserName> map = new HashMap<>();
// Create two users and put them in the map:
UserIN user0 = new UserIN(0, new UserName("Peter", "Woodbridge"));
UserIN user1 = new UserIN(1, new UserName("Anne", "Miller"));
map.put(user0.id, user0.name);
map.put(user1.id, user1.name);
// Get user with id==1 from the map and print it to standard output
UserName user = map.get(1);
System.out.println(user.firstname + " " + user.lastname);
这会打印:
Anne Miller