RFR: 8304947: Unnecessary Vector usage in java.awt.MenuBar.shortcuts

Andrey Turbanov aturbanov at openjdk.org
Tue Mar 28 19:53:46 UTC 2023


On Fri, 24 Mar 2023 07:21:27 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> Behavior is the same.
>> `Enumeration` doesn't have `remove` method. Only `hasMoreElements` and `nextElement`.
>> Method creates a new collection for result each time, hence no updates of underlying collection are possible.
>> Iteration order is the same too.
>> 
>>         Vector<String> v = new Vector<>();
>>         v.add("123");
>>         v.add("444");
>>         v.add("555");
>>         v.add("666");
>> 
>>         Enumeration<String> elements = v.elements();
>>         while (elements.hasMoreElements()) {
>>             String s = elements.nextElement();
>>             System.out.println(s);
>>         }
>>         System.out.println();
>> 
>>         ArrayList<String> a = new ArrayList<>(v);
>>         Enumeration<String> e = Collections.enumeration(a);
>>         while (e.hasMoreElements()) {
>>             String s = e.nextElement();
>>             System.out.println(s);
>>         }
>> 
>> gives
>> 
>> 123
>> 444
>> 555
>> 666
>> 
>> 123
>> 444
>> 555
>> 666
>
> but there is an asIterator method that may support remove

`Vector.elements()` and `Collections.enumeration(ArrayList)` returns `Enumeration` implementations that don't override `asIterator` method. Default implementation `java.util.Enumeration#asIterator` doesn't support remove too.
So, we are safe here.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/13149#discussion_r1147979096



More information about the client-libs-dev mailing list