Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

EntrySet not working as expected

camickrOct 6 2008 — edited Oct 6 2008
I've been updating some old code that uses Enumerations, to use an EntrySet instead. However, I get different results and I'm not sure why.

Several properties return different values when using the EntrySet. I choose the "Button.font" property as a simple example.

So the question is why is the value different when using an EntrySet?
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class UIManagerTest
{
    public static void main(String[] args)
    {
        UIDefaults defaults = UIManager.getLookAndFeelDefaults();

        String property = "Button.font";

        //  This works the way I want it to

        useEnumeration(defaults, property);

        //  This doesn't work the way I want it to

        useEntrySet(defaults, property);
    }

    public static void useEnumeration(UIDefaults defaults, String property)
    {
        for ( Enumeration enumm = defaults.keys(); enumm.hasMoreElements(); )
        {
            Object key = enumm.nextElement();
            Object value = defaults.get( key );

            if (key.toString().equals(property))
            {
                System.out.println("Enumeration: " + property + " : " + value);
            }
        }
    }

    public static void useEntrySet(UIDefaults defaults, String property)
    {
        for (Map.Entry e: defaults.entrySet())
        {
            Object key = e.getKey();
            Object value = e.getValue();

            if (key.toString().equals(property))
            {
                System.out.println("EntrySet   : " + property + " : " + value);
            }
        }
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2008
Added on Oct 6 2008
7 comments
348 views