You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Simon Chatelain <sc...@gmail.com> on 2011/03/25 11:59:42 UTC

Pivot Map with comparator issue.

Hello,

I observed a really weird behavior when using a Pivot Map with a comparator.
By executing the code bellow, we can see that from 0 to 12 the l_KeyCount ==
l_Map.getCount(). But when adding the 13th element in the map the l_KeyCount
become 26 when the l_Map.getCount() == 13. Then if I remove one of the entry
of the Map, the iterator on the map's key still return the key corresponding
to the removed entry, but the l_Map.containsKey() return false.

If I do not set a comparator on the map, I do not have this problem.

I looked in the user mailing list for similar issue, but didn't find
anything.

Sounds like a bug to me or am I missing something ?

Simon

Map<Integer, String> l_Map = new HashMap<Integer, String>();
l_Map.setComparator(new Comparator<Integer>() {

    @Override
    public int compare(Integer _arg0, Integer _arg1) {
        return _arg0.compareTo(_arg1);
    }
});

for (int i = 0; i < 100; i++) {
    l_Map.put(i, UUID.randomUUID().toString());

    int l_KeyCount = 0;
    for (Integer l_Key : l_Map) {
        l_KeyCount++;
    }
    if (l_KeyCount != l_Map.getCount()) {
        Logger.getLogger(this.getClass().getName()).log(Level.INFO,
                "l_KeyCount should be {0} but is {1}",
                new Object[] { l_Map.getCount(), l_KeyCount });
        l_Map.remove(0);
    }
    for (Integer l_Key : l_Map) {
        if (!l_Map.containsKey(l_Key)) {
            Logger.getLogger(this.getClass().getName()).log(Level.INFO,
                            "WTF : {0}",
            new Object[] { l_Key.toString()});
        }
    }
}

Re: Pivot Map with comparator issue.

Posted by Sandro Martini <sa...@gmail.com>.
Et voilĂ :
https://issues.apache.org/jira/browse/PIVOT-722

created and resolved.

Bye


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Pivot-Map-with-comparator-issue-tp2729873p2730395.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: Pivot Map with comparator issue.

Posted by Sandro Martini <sa...@gmail.com>.
Hi all,
I think it would be good to have a JIRA ticket for this, so we can put this
in release notes of 2.0.1 .
If possible I'll create and resolve it today.

Thanks to all.

Bye,
Sandro


--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/Pivot-Map-with-comparator-issue-tp2729873p2730353.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: Pivot Map with comparator issue.

Posted by Simon Chatelain <sc...@gmail.com>.
The exact code I added on my side ;-)

Thank you

On Fri, Mar 25, 2011 at 1:25 PM, Greg Brown <gk...@verizon.net> wrote:

> I think you are correct. Adding this code below "count = 0;" in
> HashMap#rehash() eliminates the errors:
>
> if (keys != null) {
>     keys.clear();
> }
>
> I'll check this in.
>
> On Mar 25, 2011, at 8:08 AM, Simon Chatelain wrote:
>
> Hello again,
>
> After some further investigation, I could make a guess : does the problem
> comes from the fact that in the HashMap#rehash() method the keys list is not
> cleared ?
>
> What do you think ?
>
> Simon
>
> On Fri, Mar 25, 2011 at 11:59 AM, Simon Chatelain <sc...@gmail.com>wrote:
>
>> Hello,
>>
>> I observed a really weird behavior when using a Pivot Map with a
>> comparator. By executing the code bellow, we can see that from 0 to 12 the
>> l_KeyCount == l_Map.getCount(). But when adding the 13th element in the map
>> the l_KeyCount become 26 when the l_Map.getCount() == 13. Then if I remove
>> one of the entry of the Map, the iterator on the map's key still return the
>> key corresponding to the removed entry, but the l_Map.containsKey() return
>> false.
>>
>> If I do not set a comparator on the map, I do not have this problem.
>>
>> I looked in the user mailing list for similar issue, but didn't find
>> anything.
>>
>> Sounds like a bug to me or am I missing something ?
>>
>> Simon
>>
>> Map<Integer, String> l_Map = new HashMap<Integer, String>();
>> l_Map.setComparator(new Comparator<Integer>() {
>>
>>     @Override
>>     public int compare(Integer _arg0, Integer _arg1) {
>>         return _arg0.compareTo(_arg1);
>>     }
>> });
>>
>> for (int i = 0; i < 100; i++) {
>>     l_Map.put(i, UUID.randomUUID().toString());
>>
>>     int l_KeyCount = 0;
>>     for (Integer l_Key : l_Map) {
>>         l_KeyCount++;
>>     }
>>     if (l_KeyCount != l_Map.getCount()) {
>>         Logger.getLogger(this.getClass().getName()).log(Level.INFO,
>>                 "l_KeyCount should be {0} but is {1}",
>>                 new Object[] { l_Map.getCount(), l_KeyCount });
>>         l_Map.remove(0);
>>     }
>>     for (Integer l_Key : l_Map) {
>>         if (!l_Map.containsKey(l_Key)) {
>>             Logger.getLogger(this.getClass().getName()).log(Level.INFO,
>>                             "WTF : {0}",
>>             new Object[] { l_Key.toString()});
>>         }
>>     }
>> }
>>
>
>
>

Re: Pivot Map with comparator issue.

Posted by Greg Brown <gk...@verizon.net>.
I think you are correct. Adding this code below "count = 0;" in HashMap#rehash() eliminates the errors:

if (keys != null) {
    keys.clear();
}

I'll check this in.

On Mar 25, 2011, at 8:08 AM, Simon Chatelain wrote:

> Hello again,
> 
> After some further investigation, I could make a guess : does the problem comes from the fact that in the HashMap#rehash() method the keys list is not cleared ?
> 
> What do you think ?
> 
> Simon
> 
> On Fri, Mar 25, 2011 at 11:59 AM, Simon Chatelain <sc...@gmail.com> wrote:
> Hello,
> 
> I observed a really weird behavior when using a Pivot Map with a comparator. By executing the code bellow, we can see that from 0 to 12 the l_KeyCount == l_Map.getCount(). But when adding the 13th element in the map the l_KeyCount become 26 when the l_Map.getCount() == 13. Then if I remove one of the entry of the Map, the iterator on the map's key still return the key corresponding to the removed entry, but the l_Map.containsKey() return false.
> 
> If I do not set a comparator on the map, I do not have this problem.
> 
> I looked in the user mailing list for similar issue, but didn't find anything. 
> 
> Sounds like a bug to me or am I missing something ?
> 
> Simon
> 
> Map<Integer, String> l_Map = new HashMap<Integer, String>();
> l_Map.setComparator(new Comparator<Integer>() {
> 
>     @Override
>     public int compare(Integer _arg0, Integer _arg1) {
>         return _arg0.compareTo(_arg1);
>     }
> });
> 
> for (int i = 0; i < 100; i++) {
>     l_Map.put(i, UUID.randomUUID().toString());
> 
>     int l_KeyCount = 0;
>     for (Integer l_Key : l_Map) {
>         l_KeyCount++;
>     }
>     if (l_KeyCount != l_Map.getCount()) {
>         Logger.getLogger(this.getClass().getName()).log(Level.INFO,
>                 "l_KeyCount should be {0} but is {1}",
>                 new Object[] { l_Map.getCount(), l_KeyCount });
>         l_Map.remove(0);
>     }
>     for (Integer l_Key : l_Map) {
>         if (!l_Map.containsKey(l_Key)) {
>             Logger.getLogger(this.getClass().getName()).log(Level.INFO,
>                             "WTF : {0}",
>             new Object[] { l_Key.toString()});
>         }
>     }
> }
> 


Re: Pivot Map with comparator issue.

Posted by Simon Chatelain <sc...@gmail.com>.
Hello again,

After some further investigation, I could make a guess : does the problem
comes from the fact that in the HashMap#rehash() method the keys list is not
cleared ?

What do you think ?

Simon

On Fri, Mar 25, 2011 at 11:59 AM, Simon Chatelain <sc...@gmail.com>wrote:

> Hello,
>
> I observed a really weird behavior when using a Pivot Map with a
> comparator. By executing the code bellow, we can see that from 0 to 12 the
> l_KeyCount == l_Map.getCount(). But when adding the 13th element in the map
> the l_KeyCount become 26 when the l_Map.getCount() == 13. Then if I remove
> one of the entry of the Map, the iterator on the map's key still return the
> key corresponding to the removed entry, but the l_Map.containsKey() return
> false.
>
> If I do not set a comparator on the map, I do not have this problem.
>
> I looked in the user mailing list for similar issue, but didn't find
> anything.
>
> Sounds like a bug to me or am I missing something ?
>
> Simon
>
> Map<Integer, String> l_Map = new HashMap<Integer, String>();
> l_Map.setComparator(new Comparator<Integer>() {
>
>     @Override
>     public int compare(Integer _arg0, Integer _arg1) {
>         return _arg0.compareTo(_arg1);
>     }
> });
>
> for (int i = 0; i < 100; i++) {
>     l_Map.put(i, UUID.randomUUID().toString());
>
>     int l_KeyCount = 0;
>     for (Integer l_Key : l_Map) {
>         l_KeyCount++;
>     }
>     if (l_KeyCount != l_Map.getCount()) {
>         Logger.getLogger(this.getClass().getName()).log(Level.INFO,
>                 "l_KeyCount should be {0} but is {1}",
>                 new Object[] { l_Map.getCount(), l_KeyCount });
>         l_Map.remove(0);
>     }
>     for (Integer l_Key : l_Map) {
>         if (!l_Map.containsKey(l_Key)) {
>             Logger.getLogger(this.getClass().getName()).log(Level.INFO,
>                             "WTF : {0}",
>             new Object[] { l_Key.toString()});
>         }
>     }
> }
>