通过输入HashMap值无法按预期方式获取HashMap密钥

时间:2019-02-28 08:10:44

标签: java oop hashmap hashset keyset

问题出在方法getName()

public class Phonebook implements PhonebookInterface {

    private Map<String, Set<String>> phonebook;
    private Map<String, Address> address;

    public Phonebook() {
        this.phonebook = new HashMap<String, Set<String>>();
        this.address = new HashMap<String, Address>();
    }

    @Override
    public void addNumber(String person, String number) {
        if (!this.phonebook.containsKey(person)) {
            this.phonebook.put(person, new HashSet<String>());
        }

        this.phonebook.get(person).add(number);
    }

    @Override
    public void getName(String number) {
        for (String person : this.phonebook.keySet()) {
            if (this.phonebook.get(person).contains(number)) {
                System.out.println(" " + person);
            } else {
                System.out.println("  not found");
            }
        }
    }

    @Override
    public void getNumber(String person) {
        try {
            for (String n : this.phonebook.get(person)) {
                if (this.phonebook.get(person).size() > 1) {
                    System.out.println(" " + n);
                } else {
                    System.out.println("number: " + n);
                }
            }
        } catch (Exception e) {
            System.out.println("  not found");
        }
    }

    public void addAddress(String person, String street, String city) {
        this.address.put(person, new Address(street, city));
    }

    public void getInfo(String person) {
        if (this.address.containsKey(person)) {
            System.out.println("  address: " + this.address.get(person));

            if (this.phonebook.containsKey(person)) {
                System.out.println("  phone numbers:");
                for (String n : this.phonebook.get(person)) {
                    System.out.println("  " + n);
                }
            } else {
                System.out.println("  phone number not found");
            }
        } else {
            System.out.println("  not found");
        }
    }

    public void removeInfo(String person) {
        if (this.address.containsKey(person)) {
            this.address.remove(person);
        } else {
            System.out.println(" not found");
        }
    }
}

public class Address {

    private String street;
    private String city;

    public Address() {
    }

    Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String getStreet() {
        return this.street;
    }

    public String getCity() {
        return this.city;
    }

    public String toString() {
        return this.street + " " + this.city;
    }

}

class Main {

    public static void main(String[] args) {
        Phonebook p = new Phonebook();

        System.out.println("Search for number: ");

        System.out.println("Pekka :");
        p.addNumber("Pekka", "014-1234");
        p.addNumber("Pekka", "015-5344");
        p.getNumber("Pekka");

        System.out.println("Matti :");
        p.addNumber("Matti", "013-4321");
        p.getNumber("Matti");

        System.out.println("\nsearch for a person by phone number: ");

        p.getName("013-4321");
        p.getName("1234567");
    }
}

预期输出为:

搜索号码:
佩卡:
014-1234
015-5344
玛蒂:
编号:013-4321

通过电话号码搜索人:
 马蒂
 找不到

但是输出是:

搜索号码:
佩卡:
 014-1234
 015-5344
玛蒂:
编号:013-4321

通过电话号码搜索人:
  找不到
 马蒂
  找不到
  找不到

为什么输出的打印结果是“ 未找到” 3次而不是3次?

1 个答案:

答案 0 :(得分:1)

您遍历Map的所有键,然后将相应的值与所需的值进行比较。如果没有匹配项,则打印“找不到”。

您的Map有两个键,因此,如果您搜索Map中存在的值,则将打印匹配的键,但是对于该键,您还将打印“找不到”其他键。

如果要搜索Map中不存在的值,则将打印两次“未找到”(每个键一次)。

仅应在遍历所有键之后打印“找不到”:

public void getName(String number) {   
    for (String person : this.phonebook.keySet()) {   
        if (this.phonebook.get(person).contains(number)) {   
            System.out.println(" " + person);
            return;
        }    
    }
    System.out.println("  not found"); 
}