Page 1 of 1

About Question enthuware.ocpjp.v11.2.1768 :

Posted: Tue Feb 09, 2021 2:57 pm
by OliviaJohnson
I don't understand the order of the enumeration.

Code: Select all

noLabel : NO
okLabel : YES
cancelLabel : Cancel 
Is ResourceBundle returning the enumeration in reverse? I thought it would be like this

Code: Select all

okLabel=YES
noLabel=NO
cancelLabel=Cancel  
because okLabel is listed first in the highest priority properties file

Code: Select all

msgs.getKeys().asIterator().forEachRemaining(System.out::println);

noLabel
okLabel
cancelLabel


Re: About Question enthuware.ocpjp.v11.2.1768 :

Posted: Tue Feb 09, 2021 3:55 pm
by OliviaJohnson
Never mind, it's not by ordering in the property file, hashCode, or Comparable.

Code: Select all

c=1
a=1
d=1
b=1
e=1
aa=1
""=1

Code: Select all

msgs.getKeys().asIterator().forEachRemaining(x -> System.out.println(x + ": " + x.hashCode()));

Code: Select all

aa: 3104
"": 1088
a: 97
b: 98
c: 99
d: 100
e: 101
PropertyResourceBundle constructor creates a private "lookup" HashMap wrapping over a Hashtable when constructed. I thought HashMap's iterators do not guarantee ordering, but its interesting how the getKeys() method always return the same output.

I guess knowing the algo for the iterator do not matter to answer this question though.

Re: About Question enthuware.ocpjp.v11.2.1768 :

Posted: Sat Apr 17, 2021 6:19 am
by Sieusc
I don't understand why okLabel=YES in "mymsgs_en_UK.properties" is not printed (as the first line) ?

Re: About Question enthuware.ocpjp.v11.2.1768 :

Posted: Sat Apr 17, 2021 8:19 am
by admin
It is just how the values are returned by the enumeration when you do msgs.getKeys();. The order is not guaranteed anyway.

Re: About Question enthuware.ocpjp.v11.2.1768 :

Posted: Sat Apr 17, 2021 2:27 pm
by Sieusc
Can you please explain in more detail what Enumeration does and what the flow of the iteration is?

If it is iterating through 1.mymsgs.properties as well as 2.mymsgs_en_UK.properties, then why is is the "okLabel=YES" in 2. not printed?

Re: About Question enthuware.ocpjp.v11.2.1768 :

Posted: Thu Feb 02, 2023 4:01 pm
by asi-aal
public abstract Enumeration<String> getKeys();

return an Enumeration of the keys contained in this ResourceBundle and its parent bundles.
Could you tell me what It meant by parent bundles? Is It the base bundle (e.i mymsgs.properties) or mymsgs_en.properties (If existed)?

Re: About Question enthuware.ocpjp.v11.2.1768 :

Posted: Fri Feb 03, 2023 12:52 am
by admin
The parent bundle is the bundle loaded just before this bundle. For example, if the chain of bundles that are loaded is a -> b -> c -> d, then c is the parent of d, b is the parent of c, and so on.

Values present in the child are superimposed on the values present in the parent. So, if the bundle c contains key1=value1 but bundle d contains key1=valueX, then key1 will be associated with valueX (instead of value1).

So, now, in this question, as the explanation to option 5 clearly notes:
Remember that if there were another properties file named mymsgs_en.properties also present, then the values of this file would be superimposed on the values mymsgs.properties and then values of mymsgs_en_UK.properties would be superimposed on the resulting union.
Thus, the chain is:

mymsgs.properties -> mymsgs_en.properties -> mymsgs_en_UK.properties