【问题标题】:How do I use hash map with arrayList as value如何使用带有 arrayList 作为值的哈希映射
【发布时间】:2013-11-12 12:40:15
【问题描述】:

我正在使用HashMap<Integer, ArrayList>。在我的ArrayList, 第二个元素是int,我需要将其加一。我怎样才能做到这一点?

public class IncrementAge {

    /** integer is key; value is ArrayList. */
    Map<Integer, ArrayList> idNumber = new HashMap<Integer, ArrayList>();

    /** array that contains name and age (only two elements). */
    ArrayList<> person = new ArrayList();

    /** name is 1st(0) element in array while age is second(1) element. */
    this.person.add(0, "jose");
    this.person.add(1, 32);

    /** place this object in hashMap with key=0; obj=person. */

    idNumber.put(0, this.person);
}

我应该如何增加这个idNumber 对象的年龄, 基于HashSet的键值?

【问题讨论】:

  • 您需要将对象名称赋予 ArrayList 模板。
  • 这是一个糟糕的设计。请创建一个 Person 类,并让 Map 的类型为 。然后可以在 Map 中查找一个人,并使用 Person 的方法来更改属性。
  • 你需要先从Map中获取ArrayList,然后通过索引获取元素。
  • 当你解决这个问题时,你会发现它需要一大堆代码,而且还有非类型安全的代码——这就是你的设计很糟糕的原因。
  • 不是硬件。试图了解java的内置数据结构。只是玩弄它

标签: java arraylist hashmap


【解决方案1】:

遵循sn-p的代码将帮助您增加年龄。

Integer age = (Integer)idNumber.get(0).remove(1);
age++;
idNumber.get(0).add(1, age);

说明:
- idNumber.get(0):这里的0代表需要增加年龄的人的key
- remove(1):将从 ArrayList 人(即年龄)中移除并返回索引为 1 的对象。
- age++:将年龄增加 1
- add(1, age):将年龄对象添加到 ArrayList 索引 1 处。

但正如已经建议的那样,这种编写代码的方式不是好的做法,而不是使用 ArrayList preson,更好的方法是设计 Person 类并将其对象作为值存储在 HashMap 中。

【讨论】:

  • 这是有道理的。谢谢你。 ...即使是动态伪代码也在这里受到批评...大声笑
【解决方案2】:

没有必要向寻求你帮助的人吐口水。

@user2943394,是的,你的设计在这里不好。 OO 的存在就是为了简化您的编码。

这里有一个简单的例子,你可以如何做你需要做的事情,让它看起来很漂亮。但请注意,这只是 2 分钟的代码废品,而不是最先进的设计。

public class Person {
    private String firstName;
    private String lastName;
    private int age;

    // Constructor
    public Person(...) {
        ...
    }


    // Getters & Setters
    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        // you dont want to pass the wrong value, so you might want to check for that also
        this.age = age;
    }

    ...
}

public class PersonRepo {
    private Map<Integer, Person> personMap;

    public PersonRepo() {
        this.personMap = new HashMap<Integer, Person>();

        loadPersons();
    }

    private void loadPersons() {
        // load them from text file, or whicever source you have
        // or simply add them manually
    }

    public Person getPersonById(int id) {
        return this.personMap.get(id);
    }
}

public class PersonService {
    private PersonRepo personRepo;

    public PersonService() {
        this.personRepo = new PersonRepo();
    }

    public void incrementPersonAge(int personId, int step) {
        Person person = this.personRepo.getPersonById(personId);
        int oldAge = person.getAge();
        int newAge = oldAge + step;
        person.setAge(newAge);
    }
}

public class PersonTester() {
    public static void main(String[] args) {
        PersonService personService = new PersonService();

        personService.incrementPersonAge(12, 1);
    }
}

如果你真的坚持自己的方式,你可以这样做:

int oldAge = (Integer) idNmber.get(0).get(1);
int newAge = oldAge + 1;
idNumber.get(0).set(1, newAge);

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多