本文整理了Java中java.util.TreeMap.comparator()
方法的一些代码示例,展示了TreeMap.comparator()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TreeMap.comparator()
方法的具体详情如下:
包路径:java.util.TreeMap
类名称:TreeMap
方法名:comparator
[英]The comparator used to maintain order in this tree map, or null if it uses the natural ordering of its keys.
[中]用于维护此树映射中的顺序的比较器,如果它使用其键的自然顺序,则为null。
代码示例来源:origin: commons-collections/commons-collections
/**
* 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());
}
}
}
代码示例来源:origin: goldmansachs/gs-collections
public Comparator<? super K> comparator()
{
return this.treeMap.comparator();
}
代码示例来源:origin: eclipse/eclipse-collections
@Override
public Comparator<? super K> comparator()
{
return this.treeMap.comparator();
}
代码示例来源:origin: eclipse/eclipse-collections
@Override
public Comparator<? super K> comparator()
{
return this.treeMap.comparator();
}
代码示例来源:origin: wildfly/wildfly
/**
* 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());
}
}
}
代码示例来源:origin: jenkinsci/jenkins
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
TreeMap tm = (TreeMap) super.unmarshal(reader,context);
return new Tree(tm,tm.comparator());
}
代码示例来源:origin: apache/ignite
/**
* 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());
}
代码示例来源:origin: robovm/robovm
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());
}
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
public void testTreeMap() {
TreeMap<Integer, Integer> map = Maps.newTreeMap();
assertEquals(Collections.emptyMap(), map);
assertNull(map.comparator());
}
代码示例来源:origin: apache/ignite
/**
* 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;
}
代码示例来源:origin: google/guava
public void testTreeMapWithComparator() {
TreeMap<Integer, Integer> map = Maps.newTreeMap(SOME_COMPARATOR);
assertEquals(Collections.emptyMap(), map);
assertSame(SOME_COMPARATOR, map.comparator());
}
代码示例来源:origin: apache/ignite
/** {@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());
}
}
代码示例来源:origin: looly/hutool
/**
* 排序已有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;
}
代码示例来源:origin: looly/hutool
/**
* 排序已有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;
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: google/guava
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());
}
代码示例来源:origin: apache/kylin
/**
* 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;
}
代码示例来源:origin: apache/ignite
/**
* 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();
}
代码示例来源:origin: apache/ignite
/**
* 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();
}
内容来源于网络,如有侵权,请联系作者删除!