有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的Hashmaps帮助

我不确定如何使用get()获取信息。看着我的书,他们把钥匙递给我。我认为get()返回与该键相关的对象,查看文档。但我一定是做错了什么。。。。有什么想法吗

import java.util.*;

public class OrganizeThis 
{
    /** 
    Add a person to the organizer

    @param p A person object
    */
    public void add(Person p)
    {   
        staff.put(p, p.getEmail());
        System.out.println("Person " + p + "added");
    }

    /**
    * Find the person stored in the organizer with the email address.
    * Note, each person will have a unique email address.
    * 
    * @param email The person email address you are looking for.
    *
    */
    public Person findByEmail(String email)
    {
        Person aPerson = staff.get(email);
        return aPerson;
    }

    private Map<Person, String> staff = new HashMap<Person, String>();

    public static void main(String[] args)
    {
        OrganizeThis testObj = new OrganizeThis();
        Person person1 = new Person("J", "W", "111-222-3333", "JW@ucsd.edu");
        testObj.add(person1);

        System.out.println(testObj.findByEmail("JW@ucsd.edu"));
    }
}

共 (2) 个答案

  1. # 1 楼答案

    你做错的事情是,你正在以相反的顺序插入密钥和值(假设你希望电子邮件成为密钥)。您可以在docs中看到put的签名采用(key, value)

    改变

    staff.put(p, p.getEmail());
    

    staff.put(p.getEmail(), p);
    

    private Map<Person, String> staff = new HashMap<Person, String>();
    

    private Map<String, Person> staff = new HashMap<String, Person>();
    

    现在,您可以通过电子邮件地址查找Person

  2. # 2 楼答案

    下面是一个片段,展示了大多数Map功能:

    import java.util.*;
    public class MapExample {
        public static void main(String[] args) {
            Map<String,Integer> map = new HashMap<String,Integer>();
            map.put("One", 1);
            map.put("Two", 2);
            map.put("Three", 3);
    
            System.out.println(map.size()); // prints "3"
            System.out.println(map);
            // prints "{Three=3, One=1, Two=2}"
    
            // HashMap allows null keys and values
            // Map also allows several keys to map to the same values
            map.put(null, 1);
            map.put("?", null);
    
            System.out.println(map.size()); // prints "5"
            System.out.println(map);
            // prints "{null=1, Three=3, ?=null, One=1, Two=2}"
    
            // get values mapped by key
            System.out.println(map.get("One")); // prints "1"
            System.out.println(map.get("Two")); // prints "2"
            System.out.println(map.get("Three")); // prints "3"
    
            // get returns null if
            //   (i) there's no such key, or
            //   (ii) there's such key, and it's mapped to null
            System.out.println(map.get("Four") == null); // prints "true"
            System.out.println(map.get("?") == null); // prints "true"
    
            // use containsKey to check if map contains key
            System.out.println(map.containsKey("Four")); // prints "false"
            System.out.println(map.containsKey("?")); // prints "true"
    
            // use keySet() to get view of keys
            Set<String> keys = map.keySet();
            System.out.println(keys);
            // prints "[null, Three, ?, One, Two]"
    
            // the view supports removal
            keys.remove("Three");
            System.out.println(map);
            // prints "{null=1, ?=null, One=1, Two=2}"
    
            // use values() to get view of values
            Collection<Integer> values = map.values();
            System.out.println(values);
            // prints "[1, null, 1, 2]"
    
            // the view supports removal
            values.remove(null);
            System.out.println(map);
            // prints "{null=1, One=1, Two=2}"
    
            values.remove(1); // removes at most one mapping
            System.out.println(map);
            // prints "{One=1, Two=2}"
    
            // iterating all entries using for-each
            for (Map.Entry<String,Integer> entry : map.entrySet()) {
                System.out.println(entry.getKey() + "->" + entry.getValue());
            }
            // prints "One->1", "Two->2"
    
            map.clear();
            System.out.println(map.isEmpty()); // prints "true"
        }
    }