Storing Multiple Values in a Map--by Tim O'Brien 整理by博主
2016-02-18 11:56
555 查看
当我们需要实现key和value间的一对多关系时,MultiMap必不可少。
Maven项目使用前,加入下述依赖在你的项目pom.xml中。
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency> 一个简单MutiMap实例:
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
/**
*MultiMap maintains a collection of values for a given key.
*when a new key/value pair is added to a MultiMap, the value
*is added to the collection of values for that key.
* @author
*
*/
public class MapTest {
public static void main(String[] args) {
MultiMap map=new MultiHashMap();
map.put( "ONE", "TEST" );
map.put( "TWO", "PICNIC" );
map.put( "ONE", "HELLO" );
map.put( "TWO", "TESTIMONY" );
Set keySet=map.keySet();
Iterator keyIterator=keySet.iterator();
while(keyIterator.hasNext()){
Object key=keyIterator.next();
System.out.print( "Key: " + key + ", " );
Collection values=(Collection)map.get(key);
Iterator valuesIterator=values.iterator();
while(valuesIterator.hasNext()){
System.out.print( "Value: " + valuesIterator.next( ) + ". " );
}
System.out.println("\n");
}
}
}
输出为:
Key: ONE, Value: TEST. Value: HELLO.
Key: TWO, Value: PICNIC. Value: TESTIMONY.
MultiMap的remove机制实例如下:
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
public class MultiMapExample {
public static void main(String[] args) {
MultiMapExample example = new MultiMapExample( );
example.start( );
}
public void start(){
MultiMap map=new MultiHashMap();
map.put( "ONE", "TEST" );
map.put( "ONE", "WAR" );
map.put( "ONE", "CAR" );
map.put( "ONE", "WEST" );
map.put( "TWO", "SKY" );
map.put( "TWO", "WEST" );
map.put( "TWO", "SCHOOL" );
// At this point "ONE" should correspond to "TEST", "WAR", "CAR", "WEST"
map.remove("ONE","WAR");
map.remove("ONE","CAR");
// The size of this collection should be two "TEST", "WEST"
Collection oneCollection=(Collection)map.get("ONE");
Iterator it=oneCollection.iterator();
while(it.hasNext()){
System.out.println( "One Value: " + it.next( ) + ". " );
}
Collection values=map.values();
Iterator valuesIt=values.iterator();
while(valuesIt.hasNext()){
System.out.println( "Values: " + valuesIt.next( ) + ". " );
}
}
}结果如下:
One Value: TEST.
One Value: WEST.
Values: TEST.
Values: WEST.
Values: SKY.
Values: WEST.
Values: SCHOOL.
Maven项目使用前,加入下述依赖在你的项目pom.xml中。
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency> 一个简单MutiMap实例:
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
/**
*MultiMap maintains a collection of values for a given key.
*when a new key/value pair is added to a MultiMap, the value
*is added to the collection of values for that key.
* @author
*
*/
public class MapTest {
public static void main(String[] args) {
MultiMap map=new MultiHashMap();
map.put( "ONE", "TEST" );
map.put( "TWO", "PICNIC" );
map.put( "ONE", "HELLO" );
map.put( "TWO", "TESTIMONY" );
Set keySet=map.keySet();
Iterator keyIterator=keySet.iterator();
while(keyIterator.hasNext()){
Object key=keyIterator.next();
System.out.print( "Key: " + key + ", " );
Collection values=(Collection)map.get(key);
Iterator valuesIterator=values.iterator();
while(valuesIterator.hasNext()){
System.out.print( "Value: " + valuesIterator.next( ) + ". " );
}
System.out.println("\n");
}
}
}
输出为:
Key: ONE, Value: TEST. Value: HELLO.
Key: TWO, Value: PICNIC. Value: TESTIMONY.
MultiMap的remove机制实例如下:
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
public class MultiMapExample {
public static void main(String[] args) {
MultiMapExample example = new MultiMapExample( );
example.start( );
}
public void start(){
MultiMap map=new MultiHashMap();
map.put( "ONE", "TEST" );
map.put( "ONE", "WAR" );
map.put( "ONE", "CAR" );
map.put( "ONE", "WEST" );
map.put( "TWO", "SKY" );
map.put( "TWO", "WEST" );
map.put( "TWO", "SCHOOL" );
// At this point "ONE" should correspond to "TEST", "WAR", "CAR", "WEST"
map.remove("ONE","WAR");
map.remove("ONE","CAR");
// The size of this collection should be two "TEST", "WEST"
Collection oneCollection=(Collection)map.get("ONE");
Iterator it=oneCollection.iterator();
while(it.hasNext()){
System.out.println( "One Value: " + it.next( ) + ". " );
}
Collection values=map.values();
Iterator valuesIt=values.iterator();
while(valuesIt.hasNext()){
System.out.println( "Values: " + valuesIt.next( ) + ". " );
}
}
}结果如下:
One Value: TEST.
One Value: WEST.
Values: TEST.
Values: WEST.
Values: SKY.
Values: WEST.
Values: SCHOOL.
相关文章推荐
- MultiMap 的作用
- STL中Multimap的一个例子
- Java CookBook--Using a Buffer--by Tim O'Brien 整理by博主
- Java CookBook--Creating a Priority Queue--by Tim O'Brien 整理by博主
- Java CookBook--Using a BlockingBuffer--by Tim O'Brien 整理by博主
- multimap(一个键值对应多个值)的应用
- 【学习笔记】《STL使用入门教程》第五讲:容器map、multimap
- stl之multimap容器
- 【深度探索STL】关联式容器map和multimap
- LeetCode 1 Two Sum
- LeetCode 3 Longest Substring Without Repeating Characters
- 关联容器
- Guava学习——集合Multimap
- multimap的学习
- BidiMapMultiMapLazyMap-apache-commons-collections中Map辅助类的使用
- STL学习笔记-map/multimap容器
- C++ STL stack 括号匹配 源代码
- STL之map和multimap总结
- map,set,multimap,multiset的异同
- 【STL】STL中红黑树的应用set、multiset、map、multimap