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

Sergey Bylokhov serb at openjdk.org
Tue Mar 28 19:53:45 UTC 2023


On Fri, 24 Mar 2023 06:37:35 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:

>> src/java.desktop/share/classes/java/awt/MenuBar.java line 350:
>> 
>>> 348:             }
>>> 349:         }
>>> 350:         return shortcuts.elements();
>> 
>> Can you please confirm the old and new enumerations have the same behavior during iteration/adding/removing/etc elements?
>
> 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

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

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



More information about the client-libs-dev mailing list