List of usage examples for java.util SortedMap comparator
Comparator<? super K> comparator();
From source file:Main.java
public static void main(String[] args) { SortedMap<String, Integer> sortedMap = new TreeMap<String, Integer>(); sortedMap.put("A", 1); sortedMap.put("B", 2); sortedMap.put("C", 3); sortedMap.put("D", 4); sortedMap.put("E", 5); sortedMap.put("java2s", 6); System.out.println(sortedMap.comparator()); }
From source file:com.palantir.atlasdb.keyvalue.rdbms.utils.AtlasSqlUtils.java
private static <K, V> SortedMap<K, Set<V>> listSortedMapToSetSortedMap(SortedMap<K, List<V>> map) { SortedMap<K, Set<V>> result = Maps.newTreeMap(map.comparator()); for (Entry<K, List<V>> e : map.entrySet()) { result.put(e.getKey(), Sets.<V>newHashSet(e.getValue())); }/* w w w. j a v a2 s.co m*/ return result; }
From source file:com.erinors.hpb.server.handler.SortedMapHandler.java
@Override public Object clone(CloningContext context, Object object) { if (!(object instanceof SortedMap)) { return null; }/*w w w . j a v a 2s. com*/ SortedMap<Object, Object> source = (SortedMap<Object, Object>) object; SortedMap<Object, Object> result; if (source instanceof PersistentSortedMap && !((PersistentSortedMap) source).wasInitialized()) { result = new UninitializedPersistentSortedMap<Object, Object>(source.comparator()); context.addProcessedObject(object, result); } else { SortedMap<Object, Object> map = new TreeMap<Object, Object>(source.comparator()); context.addProcessedObject(object, map); for (Map.Entry<?, ?> entry : source.entrySet()) { map.put(context.clone(entry.getKey()), context.clone(entry.getValue())); } result = map; } return result; }
From source file:jenkins.metrics.util.NameRewriterMetricRegistry.java
private <T> SortedMap<String, T> rewrite(SortedMap<String, T> original) { if (StringUtils.isBlank(prefix) && StringUtils.isBlank(postfix)) { return original; }/*w w w . j a v a 2 s. com*/ SortedMap<String, T> result = new TreeMap<String, T>(original.comparator()); for (Map.Entry<String, T> entry : original.entrySet()) { result.put(name(prefix, entry.getKey(), postfix), entry.getValue()); } return result; }
From source file:com.erinors.hpb.server.handler.SortedMapHandler.java
@Override public Object merge(MergingContext context, Object object) { if (!(object instanceof SortedMap)) { return null; }//from www. ja v a 2 s.com SortedMap<Object, Object> source = (SortedMap<Object, Object>) object; SortedMap<Object, Object> result; if (source instanceof UninitializedPersistentSortedMap) { result = new PersistentSortedMap(context.getSessionImplementor(), source); context.addProcessedObject(object, result); } else { SortedMap<Object, Object> map = new TreeMap<Object, Object>(source.comparator()); context.addProcessedObject(object, map); for (Map.Entry<?, ?> entry : source.entrySet()) { map.put(context.merge(entry.getKey()), context.merge(entry.getValue())); } result = map; } return result; }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.SortedMap.comparator()'. * * @see java.util.SortedMap#comparator() *//*w w w . j av a 2 s . c om*/ public void testComparator() { SortedMap<K, V> sortedMap = createNavigableMap(); if (isNaturalOrder()) { assertEquals(null, sortedMap.comparator()); } else { assertEquals(getComparator(), sortedMap.comparator()); } TreeMap<K, V> treeMap = new TreeMap<>(); TreeMap<K, V> secondTreeMap = new TreeMap<>(treeMap); assertNull(treeMap.comparator()); assertNull(secondTreeMap.comparator()); treeMap = new TreeMap<>((Comparator<? super K>) null); secondTreeMap = new TreeMap<>(treeMap); assertNull(treeMap.comparator()); assertNull(secondTreeMap.comparator()); final Comparator<? super K> customComparator = new Comparator<K>() { @Override public int compare(K o1, K o2) { return o1.compareTo(o2); } }; treeMap = new TreeMap<>(customComparator); secondTreeMap = new TreeMap<>(treeMap); assertSame(customComparator, treeMap.comparator()); assertSame(customComparator, secondTreeMap.comparator()); treeMap = new TreeMap<>(new HashMap<K, V>()); secondTreeMap = new TreeMap<>(treeMap); assertNull(treeMap.comparator()); assertNull(secondTreeMap.comparator()); }
From source file:NavigableMap.java
/** * Constructs a new map containing the same mappings as the given * <tt>SortedMap</tt>, sorted according to the same ordering. * //from w w w . j a va 2s . c o m * @param m * the sorted map whose mappings are to be placed in this map, and * whose comparator is to be used to sort this map. * @throws NullPointerException * if the specified sorted map is <tt>null</tt>. */ public ConcurrentSkipListMap(SortedMap<K, ? extends V> m) { this.comparator = m.comparator(); initialize(); buildFromSorted(m); }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.Map.put(Object, Object)'. This test shows some * bad behavior of the TreeMap class before JDK 7. A mapping with null key can * be put in but several methods are are unusable afterward. * * A SortedMap with natural ordering (no comparator) is supposed to throw a * null pointer exception if a null keys are "not supported". For a natural * ordered TreeMap before JDK 7, a null pointer exception is not thrown. But, * the map is left in a state where any other key based methods result in a * null pointer exception./*from w w w.j a v a2 s . co m*/ * * @see java.util.Map#put(Object, Object) */ public void testPut_nullKey() { K[] keys = getSortedKeys(); V[] values = getSortedValues(); SortedMap<K, V> sortedMap = createNavigableMap(); if (useNullKey()) { assertNull(sortedMap.put(null, values[0])); assertTrue(sortedMap.containsValue(values[0])); // the map methods the continue to function sortedMap.containsValue(null); sortedMap.containsValue(values[0]); sortedMap.entrySet(); sortedMap.equals(createMap()); sortedMap.hashCode(); sortedMap.isEmpty(); sortedMap.keySet(); sortedMap.putAll(createMap()); sortedMap.size(); sortedMap.values(); // all of the sorted map methods still function sortedMap.comparator(); sortedMap.firstKey(); sortedMap.lastKey(); sortedMap.subMap(getLessThanMinimumKey(), getGreaterThanMaximumKey()); sortedMap.headMap(getLessThanMinimumKey()); sortedMap.tailMap(getLessThanMinimumKey()); } else if (TestUtils.getJdkVersion() > 6) { // nulls are rejected immediately and don't poison the map anymore try { assertNull(sortedMap.put(null, values[0])); fail("should have thrown"); } catch (NullPointerException e) { // expected outcome } try { assertNull(sortedMap.put(null, values[1])); fail("expected exception adding second null"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.containsKey(null); fail("expected exception on containsKey(null)"); } catch (NullPointerException e) { // expected outcome } sortedMap.containsKey(keys[0]); try { sortedMap.get(null); fail("expected exception on get(null)"); } catch (NullPointerException e) { // expected outcome } sortedMap.get(keys[0]); try { sortedMap.remove(null); } catch (NullPointerException e) { // expected } sortedMap.remove(keys[0]); } else { // before JDK 7, nulls poisoned the map try { assertNull(sortedMap.put(null, values[0])); // note: first null added is not required to throw NPE since no // comparisons are needed } catch (NullPointerException e) { // expected outcome } try { assertNull(sortedMap.put(null, values[1])); fail("expected exception adding second null"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.containsKey(null); fail("expected exception on containsKey(null)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.containsKey(keys[0]); fail("expected exception on contains(key)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.get(null); fail("expected exception on get(null)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.get(keys[0]); fail("expected exception on get(key)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.remove(null); fail("expected exception on remove(null)"); } catch (NullPointerException e) { // expected outcome } try { sortedMap.remove(keys[0]); fail("expected exception on remove(key)"); } catch (NullPointerException e) { // expected outcome } } }
From source file:org.apache.accumulo.server.master.balancer.HostRegexTableLoadBalancer.java
/** * Group the set of current tservers by pool name. Tservers that don't match a regex are put into a default pool. This could be expensive in the terms of the * amount of time to recompute the groups, so HOST_BALANCER_POOL_RECHECK_KEY should be specified in the terms of minutes, not seconds or less. * * @param current/* w ww .j av a 2s . c o m*/ * map of current tservers * @return current servers grouped by pool name, if not a match it is put into a default pool. */ protected synchronized Map<String, SortedMap<TServerInstance, TabletServerStatus>> splitCurrentByRegex( SortedMap<TServerInstance, TabletServerStatus> current) { if ((System.currentTimeMillis() - lastPoolRecheck) > poolRecheckMillis) { LOG.debug("Performing pool recheck - regrouping tablet servers based on regular expressions"); Map<String, SortedMap<TServerInstance, TabletServerStatus>> newPools = new HashMap<String, SortedMap<TServerInstance, TabletServerStatus>>(); for (Entry<TServerInstance, TabletServerStatus> e : current.entrySet()) { List<String> poolNames = getPoolNamesForHost(e.getKey().host()); for (String pool : poolNames) { SortedMap<TServerInstance, TabletServerStatus> np = newPools.get(pool); if (null == np) { np = new TreeMap<TServerInstance, TabletServerStatus>(current.comparator()); newPools.put(pool, np); } np.put(e.getKey(), e.getValue()); } } pools = newPools; this.lastPoolRecheck = System.currentTimeMillis(); } return pools; }
From source file:org.apache.jackrabbit.oak.plugins.document.util.Utils.java
/** * Deep copy of a map that may contain map values. * * @param source the source map/*from www . j a va2 s .c o m*/ * @param target the target map * @param <K> the type of the map key */ public static <K> void deepCopyMap(Map<K, Object> source, Map<K, Object> target) { for (Entry<K, Object> e : source.entrySet()) { Object value = e.getValue(); Comparator<? super K> comparator = null; if (value instanceof SortedMap) { @SuppressWarnings("unchecked") SortedMap<K, Object> map = (SortedMap<K, Object>) value; comparator = map.comparator(); } if (value instanceof Map<?, ?>) { @SuppressWarnings("unchecked") Map<K, Object> old = (Map<K, Object>) value; Map<K, Object> c = new TreeMap<K, Object>(comparator); deepCopyMap(old, c); value = c; } target.put(e.getKey(), value); } }