可能没有正确比较地图?

时间:2017-10-24 01:24:23

标签: java hashmap

我有一些类似Facebook的朋友代码,其中有一些用于将某人添加到地图的周边,"非常友好"两个人,消除他们的友谊,检查两个人是否是朋友,并查找一个人的朋友。它从文本文件中输入。目前,我正在测试的是两个人是否是朋友。样本输出应该是:

INPUT           OUTPUT
p sa 8
p li 7
p he 9
p tu 5
f li 7 tu 5
f li 7 he 9
f tu 5 sa 8
l tu 5          (Output:    li 7 sa 8)
l sa 8          (Output:       tu 5)
u li 7 tu 5
l tu 5          (Output:       sa 8)
q li 7 he 9     yes
q he 9 li 7     yes
q he 9 li 23    no
q he 9 he 9     no

P正在向地图中添加一个人,F正在增加两者之间的友谊,U正在消除友谊,Q正在检查他们是否是朋友,而L正在寻找该人的朋友。但是,我的输出是:

INPUT           OUTPUT
P sa 8
P li 7
P he 9
P tu 5
F li 7 tu 5
F li 7 he 9
F tu 5 sa 8
L tu 5         (Output: li 7 sa 8) 
L sa 8         (Output: tu 5)
U li 7 tu 5
L tu 5         (Output: sa 8)
Q li 7 he 9    No
Q he 9 li 7    No
Q he 9 li 23   No
Q he 9 he 9    Yes

(我格式化了一下以便于阅读)。正如您所看到的,L和Q的输出在运行时是错误的,我似乎无法找出原因。我的相关代码是:

static class Person {
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return name + " " + age;
    }
    public int hashCode()
    {
        int ha = 31;
        ha = 31 * ha + ((Integer) age).hashCode();
        ha = 31 * ha + (null == name ? 0 : name.hashCode());
        return ha;
    }
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if ((o == null) || (o.getClass() !=

        this.getClass()))
            return false;
        Person t = (Person) o;
        return age == t.age && (name == t.name || (name != null && name.equals(t.name)));
    }
}

public void addPerson(Person p) {
    if (DEBUG) {
        StdOut.format("P %s\n", p);
    }
    if (map.containsValue(p)) {
        return;
    }
    if (!map.containsValue(p)) {
        HashSet<Person> set1 = new HashSet<Person>();
        set1.add(p);
        map.put(p, set1);
    }
}
// addFriendship does nothing if p1 and p2 are already friends or if one
// does not exist
public void addFriendship(Person p1, Person p2) {
    if (DEBUG) {
        StdOut.format("F %s %s\n", p1, p2);
    }
    if (p1.equals(p2)) {
        return;
    }
    if (!map.containsKey(p1) || !map.containsKey(p2)) {
        return;
    }
    if (map.containsKey(p1) && map.containsKey(p2)) {
        HashSet<Person> s1 = map.get(p1);
        HashSet<Person> s2 = map.get(p2);
        s1.add(p2);
        s2.add(p1);
        map.put(p1, s1);
        map.put(p2, s2);
    }
}

// removeFriendship does nothing if p1 and p2 are not friends or if one does
// not exist
public void removeFriendship(Person p1, Person p2) {
    if (DEBUG) {
        StdOut.format("U %s %s\n", p1, p2);
    }
    if (p1.equals(p2)) {
        return;
    }
    if (!map.containsKey(p1) || !map.containsKey(p2)) {
        return;
    }
    if (map.containsKey(p1) && map.containsKey(p2)) {
        HashSet<Person> s1 = map.get(p1);
        HashSet<Person> s2 = map.get(p2);
        s1.remove(p2);
        s2.remove(p1);
        //map.remove(p2, s2);
        map.remove(s1);
        map.remove(p1);
    }
}

// queryFriendship returns false if p1 and p2 are not friends or if one does
// not exist
public boolean queryFriendship(Person p1, Person p2) {
    if (DEBUG) {
        StdOut.format("Q %s %s\n", p1, p2);
    }

    if (!map.containsKey(p1) || !map.containsKey(p2)) {
        return false;
    } else {
        return true;
    }
}

// lookupFriends returns null or empty iterable if p does not exists
public Iterable<Person> lookupFriends(Person p) {
    if (DEBUG) {
        StdOut.format("L %s\n", p);
    }
    if (!map.containsKey(p))
        return null;
    else {
        // System.out.println(p);
        HashSet<Person> s1 = map.get(p);
        s1.remove(p);
        return s1;
    }
}

我为我提供了多少信息和格式错误道歉。如果有任何问题或者您可以看到任何问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

map包含的内容并不十分清楚。它似乎是Map<Person,Set<Person>>,但是对于添加一个人,请使用map.containsValue(p)进行检查,这将永远不会返回true ...

尝试这样的事情:

static class Person
{
   String name;
   int age;
   public Person(String name, int age)
   {
      this.name = name;
      this.age = age;
   }
   public String toString()
   {
      return name + " " + age;
   }
   public int hashCode()
   {
      int ha = 31;
      ha = 31 * ha + ((Integer) age).hashCode();
      ha = 31 * ha + (null == name ? 0 : name.hashCode());
      return ha;
   }
   public boolean equals(Object o)
   {
      if (this == o)
         return true;
      if ((o == null) || (o.getClass() != this.getClass()))
         return false;
      final Person t = (Person) o;
      return age == t.age && null != name ? name.equals(t.name) : null == t.name;
   }
}

// Map of persons and their friends.
private static final Map<Person,Set<Person>> map = new HashMap<>();

// Adds a person, without any friends, if not already added.
public void addPerson(Person p)
{
   if (DEBUG)
   {
      StdOut.format("P %s\n", p);
   }
   if (!map.containsKey(p))
   {
      map.put(p, new HashSet<Person>());
   }
}

// Adds two persons to each others "friends' list".
// addFriendship does nothing if p1 and p2 are already friends or if one
// does not exist
public void addFriendship(Person p1, Person p2)
{
   if (DEBUG)
   {
      StdOut.format("F %s %s\n", p1, p2);
   }
   if (!p1.equals(p2) && map.containsKey(p1) && map.containsKey(p2))
   {
      map.get(p1).add(p2);
      map.get(p2).add(p1);
   }
}

// Removes persons from each others "friends' list".
// removeFriendship does nothing if p1 and p2 are not friends or if one does
// not exist
public void removeFriendship(Person p1, Person p2)
{
   if (DEBUG)
   {
      StdOut.format("U %s %s\n", p1, p2);
   }
   if (!p1.equals(p2) && map.containsKey(p1) && map.containsKey(p2))
   {
      map.get(p1).remove(p2);
      map.get(p2).remove(p1);
   }
}

// Checks whether two persons are mutually friends.
// queryFriendship returns false if p1 and p2 are not friends or if one does
// not exist
public boolean queryFriendship(Person p1, Person p2)
{
   if (DEBUG)
   {
      StdOut.format("Q %s %s\n", p1, p2);
   }
   final Set<Person> s1 = map.get(p1);
   final Set<Person> s2 = map.get(p2);
   return null != s1 && null != s2 && s1.contains(p2) && s2.contains(p1);
}

// Lists a person's friends, if any.
// lookupFriends returns null or empty iterable if p does not exists
public Iterable<Person> lookupFriends(Person p)
{
   if (DEBUG)
   {
      StdOut.format("L %s\n", p);
   }
   return map.get(p);
}