匹配列表的ID并获取字符串-Java
我必须获取用户名 - > firstName和lastname ID与用户列表匹配。
我不知道该如何确切地做到这一点,我认为对于循环来说是不好的,我们可以通过集合来避免实施不良的实现。
有解决方案吗?
USERIN类:
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
}
用户名类:
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;
}
}
I have to get UserName -> firstname and lastname where the id matches to UserIN list.
I dont know how to exactly do this, i think that for loop is bad, and we could do this by Collecions to avoid bad implementations.
Any solutions?
UserIN 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
}
UserName 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要避免循环,则可以使用
MAP
此打印:
If you want to avoid a loop, you can use a
Map
This prints: