Java Code Examples for java.util.Locale.hashCode()

The following are Jave code examples for showing how to use hashCode() of the java.util.Locale class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jdk8u-jdk   File: LocaleTest.java   View Source Code Vote up 6 votes
public void TestSimpleObjectStuff() {
    Locale  test1 = new Locale("aa", "AA");
    Locale  test2 = new Locale("aa", "AA");
    Locale  test3 = (Locale)test1.clone();
    Locale  test4 = new Locale("zz", "ZZ");

    if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3)
        errln("Some of the test variables point to the same locale!");

    if (test3 == null)
        errln("clone() failed to produce a valid object!");

    if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3))
        errln("clone() or equals() failed: objects that should compare equal don't");

    if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4))
        errln("equals() failed: objects that shouldn't compare equal do");

    int hash1 = test1.hashCode();
    int hash2 = test2.hashCode();
    int hash3 = test3.hashCode();

    if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3)
        errln("hashCode() failed: objects that should have the same hash code don't");
}
 
Example 2
Project: jdk8u-jdk   File: LocaleTest.java   View Source Code Vote up 6 votes
/**
 * @bug 4110613
 */
public void TestSerialization() throws ClassNotFoundException, OptionalDataException,
                IOException, StreamCorruptedException
{
    ObjectOutputStream ostream;
    ByteArrayOutputStream obstream;
    byte[] bytes = null;

    obstream = new ByteArrayOutputStream();
    ostream = new ObjectOutputStream(obstream);

    Locale test1 = new Locale("zh", "TW", "");
    int dummy = test1.hashCode();   // fill in the cached hash-code value
    ostream.writeObject(test1);

    bytes = obstream.toByteArray();

    ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));

    Locale test2 = (Locale)(istream.readObject());

    if (!test1.equals(test2) || test1.hashCode() != test2.hashCode())
        errln("Locale failed to deserialize correctly.");
}
 
Example 3
Project: openjdk-jdk10   File: LocaleTest.java   View Source Code Vote up 5 votes
public void TestSimpleObjectStuff() {
    Locale test1 = new Locale("aa", "AA");
    Locale test2 = new Locale("aa", "AA");
    Locale test3 = (Locale) test1.clone();
    Locale test4 = new Locale("zz", "ZZ");

    if (test1 == test2 || test1 == test3 || test1 == test4 || test2 == test3) {
        errln("Some of the test variables point to the same locale!");
    }

    if (test3 == null) {
        errln("clone() failed to produce a valid object!");
    }

    if (!test1.equals(test2) || !test1.equals(test3) || !test2.equals(test3)) {
        errln("clone() or equals() failed: objects that should compare equal don't");
    }

    if (test1.equals(test4) || test2.equals(test4) || test3.equals(test4)) {
        errln("equals() failed: objects that shouldn't compare equal do");
    }

    int hash1 = test1.hashCode();
    int hash2 = test2.hashCode();
    int hash3 = test3.hashCode();

    if (hash1 != hash2 || hash1 != hash3 || hash2 != hash3) {
        errln("hashCode() failed: objects that should have the same hash code don't");
    }
}
 
Example 4
Project: openjdk-jdk10   File: HashCodeTest.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    Locale[] locales = Locale.getAvailableLocales();
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    Map<Integer, Locale> map = new HashMap<>(locales.length);
    int conflicts = 0;

    for (int i = 0; i < locales.length; i++) {
        Locale loc = locales[i];
        int hc = loc.hashCode();
        min = Math.min(hc, min);
        max = Math.max(hc, max);
        Integer key = hc;
        if (map.containsKey(key)) {
            conflicts++;
            System.out.println("conflict: " + map.get(key) + ", " + loc);
        } else {
            map.put(key, loc);
        }
    }
    System.out.println(locales.length + " locales: conflicts=" + conflicts
            + ", min=" + min + ", max=" + max + ", diff=" + (max - min));
    if (conflicts >= (locales.length / 10)) {
        throw new RuntimeException("too many conflicts: " + conflicts
                + " per " + locales.length + " locales");
    }
}