学习 HashMap

发布于 2024-11-15 03:34:04 字数 430 浏览 2 评论 0原文

我正在学习如何使用 hashMaps,有人可以检查我编写的这段代码并告诉我它是否正确吗?我的想法是拥有一个在公司工作的员工列表,我想从 hashMap 中添加和删除员工。

public class Staff
{
    private HashMap<String, String>id;

    public Staff(String name, String number)
    {
        id = new HashMap<>(name,number);
    }

    public addStaff()
    {
       id.add("Joe","1234A2);   
    }

    public removeStaff()
    {
       id.remove("Joe","1234A2);
    }
}

I'm just learning myself how to use hashMaps, can someone check this piece of code I have written and tell me if it's correct? The idea is to have a list of staff working in a company, I want to add and remove staff from a hashMap.

public class Staff
{
    private HashMap<String, String>id;

    public Staff(String name, String number)
    {
        id = new HashMap<>(name,number);
    }

    public addStaff()
    {
       id.add("Joe","1234A2);   
    }

    public removeStaff()
    {
       id.remove("Joe","1234A2);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

心意如水 2024-11-22 03:34:04

在您的课程中,您需要更改/添加如下内容:

private HashMap<String, String> id;

public Staff(String name, String number)
{
    id = new HashMap<String, String>();
}

public addStaff()
{
   id.put("Joe","1234A2");   
}

public removeStaff()
{
   id.remove("Joe");
}

正确使用 HashMap

请注意,addStaff() 和 removeStaff() 对于大多数用途来说不太有用,因为它们仅添加和删除一名工作人员“Joe”。一种更有用的处理方式是

public void addStaff(StaffMember member) {
   id.put(member.getName(), member);
}

public StaffMember get(String name) {
   // this will return null if the member's name isn't a key in the map.
   return id.get(name);
}

Map 与其他数据结构的不同之处在于它有一个“键”,可以让您检索一个项目。如果您事先没有该项目的密钥,则无法以提高性能的方式使用 Map

请注意,除了返回相同 hashCode 的相等项之外,HashMap 还要求键正确遵循相等规则。基本上,如果一个对象 a 和另一个对象 b 被认为是相等的,那么

// Reflexive property
a.equals(a) must return true

// Symmetric property
if (a.equals(b)) then b.equals(a) must return true

// Transitive property
if (a.equals(b) and b.equals(c)) then a.equals(c) must return true

// Additional requirements to make hash related algorithms work properly
a.hashCode() == b.hashCode() // must be true

hashCode() 部分就超出了简单的相等性,并且是必需的使 HashMap 正常工作。

In your class, you need to change / add things to look like:

private HashMap<String, String> id;

public Staff(String name, String number)
{
    id = new HashMap<String, String>();
}

public addStaff()
{
   id.put("Joe","1234A2");   
}

public removeStaff()
{
   id.remove("Joe");
}

to use a HashMap properly.

Note that addStaff() and removeStaff() are not likely to be very useful for most purposes because they only add and remove the one staff member "Joe". A more useful way of doing things would be

public void addStaff(StaffMember member) {
   id.put(member.getName(), member);
}

public StaffMember get(String name) {
   // this will return null if the member's name isn't a key in the map.
   return id.get(name);
}

What makes a Map different from other data structures is that it has a "key" which allows you to possibly retrieve one item. You cannot use a Map in the ways that give you a boost in performance if you don't have the item's key beforehand.

Note that HashMaps require keys to follow the rules for equality correctly in addition to equal items returning the same hashCode. Basically if on object a and another object b are considered equal, then

// Reflexive property
a.equals(a) must return true

// Symmetric property
if (a.equals(b)) then b.equals(a) must return true

// Transitive property
if (a.equals(b) and b.equals(c)) then a.equals(c) must return true

// Additional requirements to make hash related algorithms work properly
a.hashCode() == b.hashCode() // must be true

The hashCode() part is above and beyond simple equality, and is required for the HashMap to work correctly.

怕倦 2024-11-22 03:34:04

对我来说看起来不错,但是我会创建一个 Employee 类并为每个员工使用唯一的 id。

class Employee {
  Integer id;
  String name;
}

Map<Integer, Employee> staff = new HashMap<Integer, Employee>();

这样您就可以随意扩展员工信息,而无需重新定义员工地图。也永远不要假设人类的名字是独一无二的。

Looks good to me, however I would have created a Employee class and use a unique id for each staff member.

class Employee {
  Integer id;
  String name;
}

Map<Integer, Employee> staff = new HashMap<Integer, Employee>();

This way you can extend the staff info at will without redefining your staff map. Also never assume that human names are unique.

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