jdk/test/java/util/IdentityHashMap/DistinctEntrySetElements.java
author dl
Fri, 03 Feb 2017 13:24:59 -0800
changeset 43522 f9c6f543c4db
parent 9734 b33a24d77590
permissions -rw-r--r--
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02 Reviewed-by: martin, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     1
/*
9734
b33a24d77590 7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles
katleman
parents: 9235
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     4
 *
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     8
 *
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    13
 * accompanied this code).
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    14
 *
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    18
 *
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    21
 * questions.
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    22
 */
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    23
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    24
/*
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    25
 * Portions Copyright (c) 2011 IBM Corporation
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    26
 */
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    27
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    28
/*
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    29
 * @test
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    30
 * @bug 6312706
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    31
 * @summary Sets from Map.entrySet() return distinct objects for each Entry
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    32
 * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    33
 */
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    34
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    35
import java.util.IdentityHashMap;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    36
import java.util.HashSet;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    37
import java.util.Map;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    38
import java.util.Set;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    39
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    40
public class DistinctEntrySetElements {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    42
        final IdentityHashMap<String, String> identityHashMap =
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    43
            new IdentityHashMap<>();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    44
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    45
        identityHashMap.put("One", "Un");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    46
        identityHashMap.put("Two", "Deux");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    47
        identityHashMap.put("Three", "Trois");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    48
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    49
        Set<Map.Entry<String, String>> entrySet = identityHashMap.entrySet();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    50
        HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    51
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    52
        // NB: These comparisons are valid in this case because none of the
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    53
        //     keys put into 'identityHashMap' above are equal to any other.
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    54
        if (false == hashSet.equals(entrySet)) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    55
            throw new RuntimeException("Test FAILED: Sets are not equal.");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    56
        }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    57
        if (hashSet.hashCode() != entrySet.hashCode()) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    58
            throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    59
        }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    60
    }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents:
diff changeset
    61
}