/** * Return the comparator used to order this map, or <code>null</code> * if this map uses its keys' natural order. * * @return the comparator used to order the map, or null if natural order */ public Comparator comparator() { if (fast) { return (map.comparator()); } else { synchronized (map) { return (map.comparator()); } } }
public Comparator<? super K> comparator() { return this.treeMap.comparator(); }
@Override public Comparator<? super K> comparator() { return this.treeMap.comparator(); }
@Override public Comparator<? super K> comparator() { return this.treeMap.comparator(); }
/** * Return the comparator used to order this map, or <code>null</code> * if this map uses its keys' natural order. * * @return the comparator used to order the map, or null if natural order */ public Comparator comparator() { if (fast) { return (map.comparator()); } else { synchronized (map) { return (map.comparator()); } } }
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { TreeMap tm = (TreeMap) super.unmarshal(reader,context); return new Tree(tm,tm.comparator()); }
/** * Attempts to create a new map of the same type as {@code map} has. Otherwise returns new {@code HashMap} * instance. * * @param map Original map. * @return New map. */ public static <K, V> Map<K, V> newMap(Map<K, V> map) { if (map instanceof LinkedHashMap) return U.newLinkedHashMap(map.size()); else if (map instanceof TreeMap) return new TreeMap<>(((TreeMap<Object, Object>)map).comparator()); else if (map instanceof ConcurrentHashMap) return new ConcurrentHashMap<>(map.size()); return U.newHashMap(map.size()); }
private void writeObject(ObjectOutputStream stream) throws IOException { stream.putFields().put("comparator", comparator()); stream.writeFields(); stream.writeInt(size); for (Map.Entry<K, V> entry : entrySet()) { stream.writeObject(entry.getKey()); stream.writeObject(entry.getValue()); } }
public void testTreeMapWithInitialMap() { SortedMap<Integer, Integer> map = Maps.newTreeMap(); map.put(5, 10); map.put(3, 20); map.put(1, 30); TreeMap<Integer, Integer> copy = Maps.newTreeMap(map); assertEquals(copy, map); assertSame(copy.comparator(), map.comparator()); }
public void testTreeMap() { TreeMap<Integer, Integer> map = Maps.newTreeMap(); assertEquals(Collections.emptyMap(), map); assertNull(map.comparator()); }
/** * Attempts to create a new map of the same known type. Will return null if map type is not supported. * * @param map Map. * @return New map of the same type or null. */ public static <K, V> Map<K, V> newKnownMap(Object map) { Class<?> cls = map == null ? null : map.getClass(); if (cls == HashMap.class) return U.newHashMap(((Map)map).size()); else if (cls == LinkedHashMap.class) return U.newLinkedHashMap(((Map)map).size()); else if (!wrapTrees() && cls == TreeMap.class) return new TreeMap<>(((TreeMap<Object, Object>)map).comparator()); else if (cls == ConcurrentHashMap.class) return new ConcurrentHashMap<>(((Map)map).size()); return null; }
public void testTreeMapWithComparator() { TreeMap<Integer, Integer> map = Maps.newTreeMap(SOME_COMPARATOR); assertEquals(Collections.emptyMap(), map); assertSame(SOME_COMPARATOR, map.comparator()); }
/** {@inheritDoc} */ @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException { BinaryRawWriter rawWriter = writer.rawWriter(); rawWriter.writeObject(map.comparator()); int size = map.size(); rawWriter.writeInt(size); for (Map.Entry<Object, Object> entry : ((TreeMap<Object, Object>)map).entrySet()) { rawWriter.writeObject(entry.getKey()); rawWriter.writeObject(entry.getValue()); } }
/** * 排序已有Map,Key有序的Map * * @param map Map * @param comparator Key比较器 * @return TreeMap * @since 4.0.1 * @see #newTreeMap(Map, Comparator) */ public static <K, V> TreeMap<K, V> sort(Map<K, V> map, Comparator<? super K> comparator) { TreeMap<K, V> result; if (map instanceof TreeMap) { // 已经是可排序Map,此时只有比较器一致才返回原map result = (TreeMap<K, V>) map; if (null == comparator || comparator.equals(result.comparator())) { return result; } } else { result = newTreeMap(map, comparator); } return result; }
/** * 排序已有Map,Key有序的Map * * @param map Map * @param comparator Key比较器 * @return TreeMap * @since 4.0.1 * @see #newTreeMap(Map, Comparator) */ public static <K, V> TreeMap<K, V> sort(Map<K, V> map, Comparator<? super K> comparator) { TreeMap<K, V> result; if (map instanceof TreeMap) { // 已经是可排序Map,此时只有比较器一致才返回原map result = (TreeMap<K, V>) map; if (null == comparator || comparator.equals(result.comparator())) { return result; } } else { result = newTreeMap(map, comparator); } return result; }
public void testTreeMapNonGeneric() { TreeMap<LegacyComparable, Integer> map = Maps.newTreeMap(); assertEquals(Collections.emptyMap(), map); map.put(new LegacyComparable("foo"), 1); map.put(new LegacyComparable("bar"), 2); assertThat(map.keySet()) .containsExactly(new LegacyComparable("bar"), new LegacyComparable("foo")) .inOrder(); assertThat(map.values()).containsExactly(2, 1).inOrder(); assertNull(map.comparator()); }
public void testTreeMapDerived() { TreeMap<Derived, Integer> map = Maps.newTreeMap(); assertEquals(Collections.emptyMap(), map); map.put(new Derived("foo"), 1); map.put(new Derived("bar"), 2); assertThat(map.keySet()).containsExactly(new Derived("bar"), new Derived("foo")).inOrder(); assertThat(map.values()).containsExactly(2, 1).inOrder(); assertNull(map.comparator()); }
/** * Creates a new instance that will have both the same property entries and * the same behavior as the given source. * <p/> * Note that the source instance and the copy instance will share the same * comparator instance if a custom ordering had been configured on the source. * * @param source the source to copy from * @return the copy */ public static OrderedProperties copyOf(OrderedProperties source) { // create a copy that has the same behaviour OrderedPropertiesBuilder builder = new OrderedPropertiesBuilder(); if (source.properties instanceof TreeMap) { builder.withOrdering(((TreeMap<String, String>) source.properties).comparator()); } OrderedProperties result = builder.build(); // copy the properties from the source to the target for (Map.Entry<String, String> entry : source.entrySet()) { result.setProperty(entry.getKey(), entry.getValue()); } return result; }
/** * Check {@code TreeMap} data structure. * * @param useBinary Whether to go through binary mode. * @param useComparator Whether comparator should be used. * @throws Exception If failed. */ @SuppressWarnings("unchecked") private void checkTreeMapAsValue(boolean useBinary, boolean useComparator) throws Exception { // Populate map. TreeMap<TestKey, Integer> map; map = testMap(useComparator); // Put and get value from cache. cache().put(KEY, map); TreeMap<TestKey, Integer> resMap; if (useBinary) { BinaryObject resMapBinary = (BinaryObject)cache().withKeepBinary().get(KEY); resMap = resMapBinary.deserialize(); } else resMap = (TreeMap<TestKey, Integer>)cache().get(KEY); // Ensure content is correct. if (useComparator) assert resMap.comparator() instanceof TestKeyComparator; else assertNull(resMap.comparator()); assertEquals(map, resMap); cache().clear(); }
/** * Check {@code TreeMap} data structure when used as key. * * @param useComparator Whether comparator should be used. * @throws Exception If failed. */ @SuppressWarnings("unchecked") private void checkTreeMapAsKey(boolean useComparator) throws Exception { // Populate map. TreeMap<TestKey, Integer> map; map = testMap(useComparator); // Put and get value from cache. cache().put(map, KEY); TreeMap<TestKey, Integer> resMap = (TreeMap<TestKey, Integer>)((Entry)cache().iterator().next()).getKey(); // Ensure content is correct. if (useComparator) assert resMap.comparator() instanceof TestKeyComparator; else assertNull(resMap.comparator()); assertEquals(map, resMap); // Ensure value is correct. Integer resSameMap = (Integer)cache().get(map); assertEquals((Object)KEY, resSameMap); Integer resIdenticalMap = (Integer)cache().get(testMap(useComparator)); assertEquals((Object)KEY, resIdenticalMap); // Ensure wrong comparator is not accepted. Integer resDifferentComp = (Integer)cache().get(testMap(!useComparator)); assertEquals(null, resDifferentComp); cache().clear(); }