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

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


On Thu, 23 Mar 2023 21:44:56 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> Method `'java.awt.MenuBar#shortcuts` creates a 'Vector<MenuShortcut>', fills it and then returns its 'Enumeration elements()' as return value.
>> Instead of usage of legacy synchronized Vector here we can use ArrayList instead. Wrap it in Collections.enumeration to adapt to 'Enumeration' result type.
>
> 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

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

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



More information about the client-libs-dev mailing list