Dávid,

I don't understand the point of this test, both implementations of anonymous 
classes are broken because neither implement equals() or hashCode(), therefore 
violating the Map.Entry contract. See PairTest.testEqualsAnonynous().

> > Every instance of Map.Entry equals to itself, but not equals of something
> that is created the same way.

I don't understand what you mean. You can use equals() and hashCode() with a 
Java 8 AbstractMap.SimpleEntry and a Pair in either direction because 
AbstractMap.SimpleEntry implements the Map.Entry contract correctly, unlike ALL 
the tests in this thread. See PairTest.testPairOfAbstractMapSimpleEntry().

Gary

On 2024/09/26 12:39:31 Dávid Szigecsán wrote:
> Well, we both were a bit mistaken.
> I did the following test.
> ```
>     @Test
>     public void run() {
>         Pair pair = Pair.of("a", "b");
>         Map.Entry<String, String> entry = new Map.Entry<String, String>() {
>             public String getKey() { return "a"; }
>             public String getValue() { return "b"; }
>             public String setValue(String value) { return null; }
>         };
>         Map.Entry<String, String> entry2 = new Map.Entry<String, String>() {
>             public String getKey() { return "a"; }
>             public String getValue() { return "b"; }
>             public String setValue(String value) { return null; }
>         };
>         Assert.assertTrue(pair.equals(entry)); // true
>         Assert.assertTrue(pair.equals(entry2)); // true
>         Assert.assertTrue(entry.equals(entry)); // true
>         Assert.assertTrue(entry2.equals(entry2)); // true
>         Assert.assertTrue(entry.equals(entry2)); // false
>         Assert.assertTrue(entry.equals(pair)); // false
>     }
> ```
> You are right about that it is an interface and it seems it has no default
> implementation, but it does some magic.
> Every instance of Map.Entry equals to itself, but not equals of something
> that is created the same way.
> 
> 
> Gary D. Gregory <ggreg...@apache.org> ezt írta (időpont: 2024. szept. 26.,
> Cs, 14:26):
> 
> >
> >
> > On 2024/09/26 11:36:49 Dávid Szigecsán wrote:
> > ...
> > > Map.Entry.equals() howewer checks if the other is an instance of the same
> > > class.
> >
> > Dávid,
> >
> > It does not, in Java 8 and 23, the method is abstract and does not define
> > code, but the Javadoc does define expected behavior.
> >
> > I imagine that it was not changed to a default method for compatibility.
> >
> > Gary
> >
> > So if we want them to be equals, we should change the Map.Entry,
> > > that is out of our limit of power. To make it symmetric we could check
> > for
> > > the instances of the same class also (and return false in this case),
> > but I
> > > think we don't want this.
> > >
> > > Dávid
> > >
> > > Alex Tsvetkov <a.e.tsvet...@gmail.com> ezt írta (időpont: 2024. szept.
> > 26.,
> > > Cs 13:11):
> > >
> > > > Hi.
> > > >
> > > > I found a bug in the implementation of the method `equals` of class
> > `Pair`.
> > > >
> > > > Implementation must be symmetric. Current implementation is not.
> > > >
> > > > Her test showing the problem:
> > > >
> > > > ```
> > > >
> > > > @Test
> > > > void run() {
> > > >     var pair = Pair.of("a", "b");
> > > >     var entry = new Map.Entry<String, String>() {
> > > >         public String getKey() { return "a"; }
> > > >         public String getValue() { return "b"; }
> > > >         public String setValue(String value) { return null; }
> > > >     };
> > > >     assertTrue(pair.equals(entry)); // true
> > > >     assertTrue(entry.equals(pair)); // false
> > > > }
> > > > ```
> > > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
> > For additional commands, e-mail: user-h...@commons.apache.org
> >
> >
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@commons.apache.org
For additional commands, e-mail: user-h...@commons.apache.org

Reply via email to