匹配列表的ID并获取字符串-Java

发布于 2025-01-23 20:04:05 字数 1303 浏览 0 评论 0原文

我必须获取用户名 - > 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2025-01-30 20:04:05

如果要避免循环,则可以使用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

If you want to avoid a loop, you can use a 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); 

This prints:

Anne Miller
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文