Java Code Examples for java.util.TreeMap#comparator()

The following examples show how to use java.util.TreeMap#comparator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TreeMapAPITest.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
public void testComparator()
{
	// create a TreeMap
	TreeMap<TreeMapKey,Object> tree = TreeMapFactory.getTreeWithKey(TreeMapFactory.SIZE);
	
	// get comparator
	Comparator<?> c = tree.comparator();
	
	// check it's null
	assertNull("comparator() should return null as the tree is using the natural ordering of its keys", c);
	
	// create a tree with an explicit Comparator
	tree = new TreeMap<TreeMapKey,Object>(new TreeMapKeyComparator());
	TreeMapFactory.populateTreeWithKey(tree, TreeMapFactory.SIZE);
	
	// get comparator
	c = tree.comparator();
	
	// check not null
	assertNotNull("comparator() returned null when Comparator has been set", c);
}
 
Example 2
Source File: TreeMapImplementor.java    From jadira with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public <T> T clone(T obj, CloneDriver parentContext, IdentityHashMap<Object, Object> referencesToReuse, long stackDepth) {
    
	stackDepth++;
	
    final TreeMap<Object, Object> source = (TreeMap)obj;
    
    final TreeMap copy = new TreeMap(source.comparator());
    
    for (final Map.Entry e : source.entrySet()) {
        final Object key = parentContext.clone(e.getKey(), parentContext, referencesToReuse, stackDepth);
        final Object value = parentContext.clone(e.getValue(), parentContext, referencesToReuse, stackDepth);
        
        copy.put(key, value);
    }
    return (T) copy;
}
 
Example 3
Source File: Cloner.java    From Concurnas with MIT License 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object clone(final Object t, final Cloner cloner, final CopyTracker tracker) throws IllegalAccessException
{
	final TreeMap<Object, Object> m = (TreeMap) t;
	final TreeMap result = new TreeMap(m.comparator());
	for (final Map.Entry e : m.entrySet())
	{
		final Object key = cloner.clone(tracker, e.getKey(), null);
		final Object value = cloner.clone(tracker, e.getValue(), null);
		result.put(key, value);
	}
	return result;
}