本文整理了Java中org.apache.commons.collections.keyvalue.MultiKey
类的一些代码示例,展示了MultiKey
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MultiKey
类的具体详情如下:
包路径:org.apache.commons.collections.keyvalue.MultiKey
类名称:MultiKey
[英]A MultiKey
allows multiple map keys to be merged together.
The purpose of this class is to avoid the need to write code to handle maps of maps. An example might be the need to lookup a filename by key and locale. The typical solution might be nested maps. This class can be used instead by creating an instance passing in the key and locale.
Example usage:
// populate map with data mapping key+locale to localizedText
Map map = new HashMap();
MultiKey multiKey = new MultiKey(key, locale);
map.put(multiKey, localizedText);
// later retireve the localized text
MultiKey multiKey = new MultiKey(key, locale);
String localizedText = (String) map.get(multiKey);
[中]MultiKey
允许将多个地图键合并在一起。
此类的目的是避免编写代码来处理地图的地图。例如,可能需要按键和区域设置查找文件名。典型的解决方案可能是嵌套贴图。可以通过创建传入键和区域设置的实例来使用这个类。
用法示例:
// populate map with data mapping key+locale to localizedText
Map map = new HashMap();
MultiKey multiKey = new MultiKey(key, locale);
map.put(multiKey, localizedText);
// later retireve the localized text
MultiKey multiKey = new MultiKey(key, locale);
String localizedText = (String) map.get(multiKey);
代码示例来源:origin: commons-collections/commons-collections
/**
* Stores the value against the specified multi-key.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @param key5 the fifth key
* @param value the value to store
* @return the value previously mapped to this combined key, null if none
*/
public Object put(Object key1, Object key2, Object key3, Object key4, Object key5, Object value) {
int hashCode = hash(key1, key2, key3, key4, key5);
int index = map.hashIndex(hashCode, map.data.length);
AbstractHashedMap.HashEntry entry = map.data[index];
while (entry != null) {
if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) {
Object oldValue = entry.getValue();
map.updateEntry(entry, value);
return oldValue;
}
entry = entry.next;
}
map.addMapping(index, hashCode, new MultiKey(key1, key2, key3, key4, key5), value);
return null;
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Removes all mappings where the first four keys are those specified.
* <p>
* This method removes all the mappings where the <code>MultiKey</code>
* has four or more keys, and the first four match those specified.
*
* @param key1 the first key
* @param key2 the second key
* @param key3 the third key
* @param key4 the fourth key
* @return true if any elements were removed
*/
public boolean removeAll(Object key1, Object key2, Object key3, Object key4) {
boolean modified = false;
MapIterator it = mapIterator();
while (it.hasNext()) {
MultiKey multi = (MultiKey) it.next();
if (multi.size() >= 4 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
(key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2))) &&
(key4 == null ? multi.getKey(3) == null : key4.equals(multi.getKey(3)))) {
it.remove();
modified = true;
}
}
return modified;
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Recalculate the hash code after deserialization.
* The hash code of some keys might have change (hash codes based
* on the system hash code are only stable for the same process).
*
* @return the instance with recalculated hash code
*/
private Object readResolve() {
calculateHashCode(keys);
return this;
}
}
代码示例来源:origin: commons-collections/commons-collections
/**
* Is the key equal to the combined key.
*
* @param entry the entry to compare to
* @param key1 the first key
* @param key2 the second key
* @return true if the key matches
*/
protected boolean isEqualKey(AbstractHashedMap.HashEntry entry, Object key1, Object key2) {
MultiKey multi = (MultiKey) entry.getKey();
return
multi.size() == 2 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1)));
}
代码示例来源:origin: commons-collections/commons-collections
public void testHashCode() {
MultiKey mk1 = new MultiKey(ONE, TWO);
MultiKey mk2 = new MultiKey(ONE, TWO);
MultiKey mk3 = new MultiKey(ONE, "TWO");
Assert.assertTrue(mk1.hashCode() == mk1.hashCode());
Assert.assertTrue(mk1.hashCode() == mk2.hashCode());
Assert.assertTrue(mk1.hashCode() != mk3.hashCode());
int total = (0 ^ ONE.hashCode()) ^ TWO.hashCode();
Assert.assertEquals(total, mk1.hashCode());
}
代码示例来源:origin: commons-collections/commons-collections
switch (key.size()) {
case 2:
assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), value));
assertEquals(1, multimap.size());
assertEquals(value, multimap.get(key.getKey(0), key.getKey(1)));
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1))));
assertEquals(value, multimap.put(key.getKey(0), key.getKey(1), null));
assertEquals(1, multimap.size());
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1)));
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
break;
case 3:
assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), value));
assertEquals(1, multimap.size());
assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1), key.getKey(2))));
assertEquals(value, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), null));
assertEquals(1, multimap.size());
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
break;
case 4:
assertEquals(null, multimap.put(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), value));
assertEquals(1, multimap.size());
assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(true, multimap.containsKey(new MultiKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3))));
代码示例来源:origin: commons-collections/commons-collections
public Object[] getOtherKeys() {
return new Object[] {
new MultiKey(I1, I7),
new MultiKey(I1, I8),
new MultiKey(I2, I4),
new MultiKey(I2, I5),
};
}
代码示例来源:origin: commons-collections/commons-collections
public void testClone() {
MultiKeyMap map = new MultiKeyMap();
map.put(new MultiKey(I1, I2), "1-2");
Map cloned = (Map) map.clone();
assertEquals(map.size(), cloned.size());
assertSame(map.get(new MultiKey(I1, I2)), cloned.get(new MultiKey(I1, I2)));
}
代码示例来源:origin: commons-collections/commons-collections
public void testConstructors() throws Exception {
MultiKey mk = null;
mk = new MultiKey(ONE, TWO);
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO}, mk.getKeys()));
mk = new MultiKey(ONE, TWO, THREE);
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE}, mk.getKeys()));
mk = new MultiKey(ONE, TWO, THREE, FOUR);
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE, FOUR}, mk.getKeys()));
mk = new MultiKey(ONE, TWO, THREE, FOUR, FIVE);
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE, FOUR, FIVE}, mk.getKeys()));
mk = new MultiKey(new Object[] {THREE, FOUR, ONE, TWO}, false);
Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
}
代码示例来源:origin: commons-collections/commons-collections
public void testMultiKeyRemoveAll4() {
resetFull();
MultiKeyMap multimap = (MultiKeyMap) map;
assertEquals(12, multimap.size());
multimap.removeAll(I1, I1, I2, I3);
assertEquals(10, multimap.size());
for (MapIterator it = multimap.mapIterator(); it.hasNext();) {
MultiKey key = (MultiKey) it.next();
assertEquals(false, I1.equals(key.getKey(0)) && I1.equals(key.getKey(1)) && I2.equals(key.getKey(2)) && key.size() >= 4 && I3.equals(key.getKey(3)));
}
}
代码示例来源:origin: commons-collections/commons-collections
public void testGetIndexed() {
MultiKey mk = new MultiKey(ONE, TWO);
Assert.assertSame(ONE, mk.getKey(0));
Assert.assertSame(TWO, mk.getKey(1));
try {
mk.getKey(-1);
fail();
} catch (IndexOutOfBoundsException ex) {}
try {
mk.getKey(2);
fail();
} catch (IndexOutOfBoundsException ex) {}
}
代码示例来源:origin: stackoverflow.com
MultiKeyMap multiKeyMap = new MultiKeyMap();
multiKeyMap.put("Key 1A","Key 1B","Value 1");
multiKeyMap.put("Key 2A","Key 2B","Value 2");
multiKeyMap.put("Key 3A","Key 3B","Value 3");
MapIterator it = multiKeyMap.mapIterator();
while (it.hasNext()) {
it.next();
MultiKey mk = (MultiKey) it.getKey();
// Option 1
System.out.println(mk.getKey(0));
System.out.println(mk.getKey(1));
// Option 2
for (Object subkey : mk.getKeys())
System.out.println(subkey);
System.out.println(it.getValue());
}
代码示例来源:origin: commons-collections/commons-collections
public void testSize() {
Assert.assertEquals(2, new MultiKey(ONE, TWO).size());
Assert.assertEquals(2, new MultiKey(null, null).size());
Assert.assertEquals(3, new MultiKey(ONE, TWO, THREE).size());
Assert.assertEquals(3, new MultiKey(null, null, null).size());
Assert.assertEquals(4, new MultiKey(ONE, TWO, THREE, FOUR).size());
Assert.assertEquals(4, new MultiKey(null, null, null, null).size());
Assert.assertEquals(5, new MultiKey(ONE, TWO, THREE, FOUR, FIVE).size());
Assert.assertEquals(5, new MultiKey(null, null, null, null, null).size());
Assert.assertEquals(0, new MultiKey(new Object[] {}).size());
Assert.assertEquals(1, new MultiKey(new Object[] {ONE}).size());
Assert.assertEquals(2, new MultiKey(new Object[] {ONE, TWO}).size());
Assert.assertEquals(7, new MultiKey(new Object[] {ONE, TWO, ONE, TWO, ONE, TWO, ONE}).size());
}
代码示例来源:origin: commons-collections/commons-collections
switch (key.size()) {
case 2:
assertEquals(value, multimap.get(key.getKey(0), key.getKey(1)));
assertEquals(null, multimap.get(null, key.getKey(1)));
assertEquals(null, multimap.get(key.getKey(0), null));
assertEquals(null, multimap.get(null, null));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, null));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, null, null));
break;
case 3:
assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(null, multimap.get(null, key.getKey(1), key.getKey(2)));
assertEquals(null, multimap.get(key.getKey(0), null, key.getKey(2)));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null));
assertEquals(null, multimap.get(null, null, null));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null, null));
break;
case 4:
assertEquals(value, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(null, multimap.get(null, key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(null, multimap.get(key.getKey(0), null, key.getKey(2), key.getKey(3)));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), null, key.getKey(3)));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), null));
assertEquals(null, multimap.get(null, null, null, null));
assertEquals(null, multimap.get(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
break;
case 5:
代码示例来源:origin: commons-collections/commons-collections
switch (key.size()) {
case 2:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1)));
assertEquals(size - 1, multimap.size());
assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1)));
break;
case 3:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(size - 1, multimap.size());
assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
break;
case 4:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(size - 1, multimap.size());
assertEquals(null, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
break;
case 5:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
assertEquals(value, multimap.remove(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), key.getKey(4)));
代码示例来源:origin: commons-collections/commons-collections
public void testEquals() {
MultiKey mk1 = new MultiKey(ONE, TWO);
MultiKey mk2 = new MultiKey(ONE, TWO);
MultiKey mk3 = new MultiKey(ONE, "TWO");
Assert.assertEquals(mk1, mk1);
Assert.assertEquals(mk1, mk2);
Assert.assertTrue(mk1.equals(mk3) == false);
Assert.assertTrue(mk1.equals("") == false);
Assert.assertTrue(mk1.equals(null) == false);
}
代码示例来源:origin: pl.touk/proxy-generator-core
private void fillProperties(List<MultiKey> newProperties, String uri)
{
//for ( int i = 1; i <= newProperties.size(); i++)
for (MultiKey property : newProperties)
{
String uri_copy = uri;
//String temp = newProperties.get(newProperties.size() - i );
String temp = (String) property.getKey(0);
if(!uri_copy.endsWith("/"))
uri_copy = uri_copy.concat("/"+ property.getKey(1));
else
uri_copy = uri_copy.concat((String) property.getKey(1));
properties.put(temp, uri_copy);
}
}
代码示例来源:origin: commons-collections/commons-collections
switch (key.size()) {
case 2:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1)));
assertEquals(false, multimap.containsKey(null, key.getKey(1)));
assertEquals(false, multimap.containsKey(key.getKey(0), null));
assertEquals(false, multimap.containsKey(null, null));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, null));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, null, null));
break;
case 3:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2)));
assertEquals(false, multimap.containsKey(null, key.getKey(1), key.getKey(2)));
assertEquals(false, multimap.containsKey(key.getKey(0), null, key.getKey(2)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null));
assertEquals(false, multimap.containsKey(null, null, null));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null, null));
break;
case 4:
assertEquals(true, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(false, multimap.containsKey(null, key.getKey(1), key.getKey(2), key.getKey(3)));
assertEquals(false, multimap.containsKey(key.getKey(0), null, key.getKey(2), key.getKey(3)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), null, key.getKey(3)));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), null));
assertEquals(false, multimap.containsKey(null, null, null, null));
assertEquals(false, multimap.containsKey(key.getKey(0), key.getKey(1), key.getKey(2), key.getKey(3), null));
break;
case 5:
代码示例来源:origin: SpoonLabs/astor
@SuppressWarnings("unchecked")
public void toJSON(String output) {
JSONObject space = new JSONObject();
JSONArray list = new JSONArray();
space.put("nrall", this.allElementsFromSpace.size());
space.put("space", list);
for (Object key : mkp.keySet()) {
JSONObject keyjson = new JSONObject();
MultiKey mk = (MultiKey) key;
keyjson.put("key", Arrays.toString(mk.getKeys()));
list.add(keyjson);
JSONArray ingredients = new JSONArray();
keyjson.put("ingredients", ingredients);
List ings = (List) mkp.get(key);
keyjson.put("nringredients", ings.size());
for (Object v : ings) {
ingredients.add(v.toString());
}
;
}
String fileName = output + "ingredients.json";
try (FileWriter file = new FileWriter(fileName)) {
file.write(space.toJSONString());
file.flush();
log.info("Storing ing JSON at " + fileName);
} catch (IOException e) {
e.printStackTrace();
log.error("Problem storing ing json file" + e.toString());
}
}
代码示例来源:origin: it.geosolutions.unredd/teststats
for (Object o : classes.getKeys()) {
内容来源于网络,如有侵权,请联系作者删除!