groovy

A key class for java.util.Map that implements hashCode cause StackOverflowError during get()

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.5.4
  • Fix Version/s: 1.5.5
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    1

Description

The following script causes StackOverflowError:

Map map = new HashMap();
map.put(new MyKey("1"), "1");
map.get(new MyKey("1"));

public class MyKey
{
    private String m_key;

    public MyKey (String key)
    {
        m_key = key;
    }

    public String getKey ()
    {
       return m_key;
    }

    public int hashCode ()
    {
        return m_key.hashCode();
    }

    public boolean equals (Object o)
    {
        if (this == o) return true;
        if (!(o instanceof MyKey)) return false;
        MyKey key = (MyKey) o;
        return key.getKey().equals(m_key);
    }
}

Activity

Hide
Paul King added a comment -

add code tags

Show
Paul King added a comment - add code tags
Hide
Paul King added a comment -

Your key class is written in Groovy and hence the first line of the equals method calls itself. Change the first line to:

if (this.is(o)) return true
Show
Paul King added a comment - Your key class is written in Groovy and hence the first line of the equals method calls itself. Change the first line to:
if (this.is(o)) return true

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: