Example usage for java.util IdentityHashMap isEmpty

List of usage examples for java.util IdentityHashMap isEmpty

Introduction

In this page you can find the example usage for java.util IdentityHashMap isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this identity hash map contains no key-value mappings.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    IdentityHashMap<Object, Object> objMap = new IdentityHashMap<Object, Object>();

    Object o1 = new Integer(123);
    Object o2 = new Integer(123);
    objMap.put(o1, "first");
    objMap.put(o2, "from java2s.com");

    Object v1 = objMap.get(o1);/*from  ww w  .  j  ava 2s .  co  m*/
    System.out.println(v1);
    Object v2 = objMap.get(o2);
    System.out.println(v2);

    System.out.println(objMap.isEmpty());
}

From source file:com.google.gwt.emultest.java.util.IdentityHashMapTest.java

/**
 * Check the state of a newly constructed, empty IdentityHashMap.
 *
 * @param hashMap/*from   w  w  w  . ja  v a  2s  .c  om*/
 */
private static void checkEmptyHashMapAssumptions(IdentityHashMap hashMap) {
    assertNotNull(hashMap);
    assertTrue(hashMap.isEmpty());

    assertNotNull(hashMap.values());
    assertTrue(hashMap.values().isEmpty());
    assertTrue(hashMap.values().size() == 0);

    assertNotNull(hashMap.keySet());
    assertTrue(hashMap.keySet().isEmpty());
    assertTrue(hashMap.keySet().size() == 0);

    assertNotNull(hashMap.entrySet());
    assertTrue(hashMap.entrySet().isEmpty());
    assertTrue(hashMap.entrySet().size() == 0);

    assertNotNull(hashMap.entrySet().iterator());
    assertFalse(hashMap.entrySet().iterator().hasNext());
}

From source file:com.google.gwt.emultest.java.util.IdentityHashMapTest.java

public void testHashMapMap() {
    IdentityHashMap srcMap = new IdentityHashMap();
    assertNotNull(srcMap);//  ww w .java 2s.  c  o m
    checkEmptyHashMapAssumptions(srcMap);

    srcMap.put(INTEGER_1, INTEGER_11);
    srcMap.put(INTEGER_2, INTEGER_22);
    srcMap.put(INTEGER_3, INTEGER_33);

    IdentityHashMap hashMap = new IdentityHashMap(srcMap);
    assertFalse(hashMap.isEmpty());
    assertTrue(hashMap.size() == SIZE_THREE);

    Collection valColl = hashMap.values();
    assertTrue(valColl.contains(INTEGER_11));
    assertTrue(valColl.contains(INTEGER_22));
    assertTrue(valColl.contains(INTEGER_33));

    Collection keyColl = hashMap.keySet();
    assertTrue(keyColl.contains(INTEGER_1));
    assertTrue(keyColl.contains(INTEGER_2));
    assertTrue(keyColl.contains(INTEGER_3));
}

From source file:com.google.gwt.emultest.java.util.IdentityHashMapTest.java

public void testIsEmpty() {
    IdentityHashMap srcMap = new IdentityHashMap();
    checkEmptyHashMapAssumptions(srcMap);

    IdentityHashMap dstMap = new IdentityHashMap();
    checkEmptyHashMapAssumptions(dstMap);

    dstMap.putAll(srcMap);/*from w ww .j a  v a2  s . c  o  m*/
    assertTrue(dstMap.isEmpty());

    dstMap.put(KEY_KEY, VALUE_VAL);
    assertFalse(dstMap.isEmpty());

    dstMap.remove(KEY_KEY);
    assertTrue(dstMap.isEmpty());
    assertEquals(dstMap.size(), 0);
}

From source file:com.google.gwt.emultest.java.util.IdentityHashMapTest.java

public void testClear() {
    IdentityHashMap hashMap = new IdentityHashMap();
    checkEmptyHashMapAssumptions(hashMap);

    hashMap.put("Hello", "Bye");
    assertFalse(hashMap.isEmpty());
    assertTrue(hashMap.size() == SIZE_ONE);

    hashMap.clear();//from   w w w.  j ava2  s. co  m
    assertTrue(hashMap.isEmpty());
    assertTrue(hashMap.size() == 0);
}

From source file:org.apache.pig.pen.LineageTrimmingVisitor.java

private Map<LOLoad, DataBag> PruneBaseDataConstrainedCoverage(Map<LOLoad, DataBag> baseData,
        LineageTracer lineage, Collection<IdentityHashSet<Tuple>> equivalenceClasses) {

    IdentityHashMap<Tuple, Collection<Tuple>> membershipMap = lineage.getMembershipMap();
    IdentityHashMap<Tuple, Double> lineageGroupWeights = lineage.getWeightedCounts(2f, 1);

    // compute a mapping from lineage group to the set of equivalence
    // classes covered by it
    // IdentityHashMap<Tuple, Set<Integer>> lineageGroupToEquivClasses = new
    // IdentityHashMap<Tuple, Set<Integer>>();
    IdentityHashMap<Tuple, Set<IdentityHashSet<Tuple>>> lineageGroupToEquivClasses = new IdentityHashMap<Tuple, Set<IdentityHashSet<Tuple>>>();
    for (IdentityHashSet<Tuple> equivClass : equivalenceClasses) {
        for (Object t : equivClass) {
            Tuple lineageGroup = lineage.getRepresentative((Tuple) t);
            // Set<Integer> entry =
            // lineageGroupToEquivClasses.get(lineageGroup);
            Set<IdentityHashSet<Tuple>> entry = lineageGroupToEquivClasses.get(lineageGroup);
            if (entry == null) {
                // entry = new HashSet<Integer>();
                entry = new HashSet<IdentityHashSet<Tuple>>();
                lineageGroupToEquivClasses.put(lineageGroup, entry);
            }//  w ww. ja  v a  2 s . c  o  m
            // entry.add(equivClassId);
            entry.add(equivClass);
        }
    }

    // select lineage groups such that we cover all equivalence classes
    IdentityHashSet<Tuple> selectedLineageGroups = new IdentityHashSet<Tuple>();
    while (!lineageGroupToEquivClasses.isEmpty()) {
        // greedily find the lineage group with the best "score", where
        // score = # equiv classes covered / group weight
        double bestWeight = -1;
        Tuple bestLineageGroup = null;
        Set<IdentityHashSet<Tuple>> bestEquivClassesCovered = null;
        int bestNumEquivClassesCovered = 0;
        for (Tuple lineageGroup : lineageGroupToEquivClasses.keySet()) {
            double weight = lineageGroupWeights.get(lineageGroup);

            Set<IdentityHashSet<Tuple>> equivClassesCovered = lineageGroupToEquivClasses.get(lineageGroup);
            int numEquivClassesCovered = equivClassesCovered.size();

            if ((numEquivClassesCovered > bestNumEquivClassesCovered)
                    || (numEquivClassesCovered == bestNumEquivClassesCovered && weight < bestWeight)) {

                if (selectedLineageGroups.contains(lineageGroup)) {
                    bestLineageGroup = lineageGroup;
                    bestEquivClassesCovered = equivClassesCovered;
                    continue;
                }

                bestWeight = weight;
                bestLineageGroup = lineageGroup;
                bestNumEquivClassesCovered = numEquivClassesCovered;
                bestEquivClassesCovered = equivClassesCovered;
            }
        }
        // add the best-scoring lineage group to the set of ones we plan to
        // retain
        selectedLineageGroups.add(bestLineageGroup);

        // make copy of bestEquivClassesCovered (or else the code that
        // follows won't work correctly, because removing from the reference
        // set)
        Set<IdentityHashSet<Tuple>> toCopy = bestEquivClassesCovered;
        bestEquivClassesCovered = new HashSet<IdentityHashSet<Tuple>>();
        bestEquivClassesCovered.addAll(toCopy);

        // remove the classes we've now covered
        Collection<Tuple> toRemove = new LinkedList<Tuple>();
        for (Tuple lineageGroup : lineageGroupToEquivClasses.keySet()) {

            Set<IdentityHashSet<Tuple>> equivClasses = lineageGroupToEquivClasses.get(lineageGroup);

            for (Iterator<IdentityHashSet<Tuple>> it = equivClasses.iterator(); it.hasNext();) {
                IdentityHashSet<Tuple> equivClass = it.next();
                if (bestEquivClassesCovered.contains(equivClass)) {
                    it.remove();
                }
            }
            if (equivClasses.size() == 0)
                toRemove.add(lineageGroup);

        }
        for (Tuple removeMe : toRemove)
            lineageGroupToEquivClasses.remove(removeMe);
    }

    // revise baseData to only contain the tuples that are part of
    // selectedLineageGroups
    IdentityHashSet<Tuple> tuplesToRetain = new IdentityHashSet<Tuple>();
    for (Tuple lineageGroup : selectedLineageGroups) {
        Collection<Tuple> members = membershipMap.get(lineageGroup);
        for (Tuple t : members)
            tuplesToRetain.add(t);
    }

    Map<LOLoad, DataBag> newBaseData = new HashMap<LOLoad, DataBag>();
    for (LOLoad loadOp : baseData.keySet()) {
        DataBag data = baseData.get(loadOp);
        // DataBag newData = new DataBag();
        DataBag newData = BagFactory.getInstance().newDefaultBag();
        for (Iterator<Tuple> it = data.iterator(); it.hasNext();) {
            Tuple t = it.next();
            if (tuplesToRetain.contains(t))
                newData.add(t);
        }
        newBaseData.put(loadOp, newData);
    }

    return newBaseData;
}