◎筱米加步枪◎.Blog

Happy coding

Google Collections 学习小结

筱米加步枪 posted @ 2010年7月27日 13:05 in [ 开源框架 ] with tags google-collections , 4606 阅读

今天,浏览别人的技术博客的时候,无意中看到Google Colections这个东东,下载看了相关资料,感觉还不错,某些场合还挺好用的。主要是提供了更多集合供用户使用,Google Collections是基于JDK5.0上的扩展。用起来方便,前不久1.0版本正式发布。今天花了些时间了解内容。注意几个新的集合的特点。

要用Google Collections 要导入google-collect-1.0.jar包才可以,以下是今天写的一些Demo程序,为了方便,把一些相关知识写在代码里面的注释上了。

public class GoogleCollectionsDemo {
	
	/**
	 * 比较传统的JDK与Google Collections获取不可修改的List集合
	 */
	public void testImmutableList(){
		//1.JDK5.0 传统的方式
		List<String> names = new ArrayList<String>();
		names.add("james");
		names.add("bryant");
		List<String> immutableNamesJdk = Collections.unmodifiableList(names);
		System.out.println("JDK1.5方式:"+immutableNamesJdk);
		
		//2.Google Collections 第一种方式获取
		List<String> immutableNamesGoogle1 = ImmutableList.of("james", "bryant");
		System.out.println("Google Collections获取方式1:"+immutableNamesGoogle1);
		
		//2.Google Collections 第二种方式获取
		ImmutableList.Builder<String> builder= ImmutableList.builder();
		builder.add("james","bryant");
		ImmutableList<String> immutableNamesGoogle2 = builder.build();
		System.out.println("Google Collections获取方式2:"+immutableNamesGoogle2);
	}
	
	/**
	 * 测试Multiset的用法
	 */
	public void testMultiset(){
		//对比:
		//1.JDK5.0 中的Set是一个无序不重复的集合
		//2.Google Collections的MultiSet是一个无序但是可添加重复的元素的集合
		Multiset<String> multiSet = HashMultiset.create();
		multiSet.add("james");
		multiSet.add("bryant");
		multiSet.add("james");
		//MultiSet用来计数统计是很方便的,传统的需要用Map把值取出来+1再放回去
		System.out.println("获取Multiset中的james个数:"+multiSet.count("james"));
		System.out.println("Multiset中的元素:"+multiSet);
	}
	
	/**
	 * 测试Multimap的用法
	 */
	public void testMultimap(){
		//对比:
		//Google Collections 的MultiMap<K,V> 类似于  Map<K ,Collection<V>> 
		Multimap<String, String> multiMap = HashMultimap.create();
		multiMap.put("23", "james");
		multiMap.put("24", "bryant");
		multiMap.put("23", "jordan");
		//注意,MultiMap取得的数据不是String类型,而是Collection类型
		Collection<String> collection = multiMap.get("23");
		System.out.println("23号球员有:"+collection);
	}
	
	/**
	 * 测试BiMap的用法
	 */
	public void testBiMap(){
		// Google Collections 的BiMap 是一个双向的Map
		// 可以根据Key得到value,也可以根据value得到key
		// BiMap的健唯一、值也唯一
		BiMap<String, String> biMap = HashBiMap.create();
		biMap.put("23Cel", "james");
		biMap.put("24Laker", "bryant");
		System.out.println("获取23Cel的值:"+biMap.get("23Cel"));
		
		// 将Map反向,即得到Map<V,K>
		BiMap<String , String> inverseMap = biMap.inverse();
		System.out.println("获取值为bryant的key值:"+inverseMap.get("bryant"));
	}
	
	/**
	 * 测试MapMaker
	 */
	public void testMapMaker(){
		
		//MapMaker是对ConcurrentMap的建立,对传进来的Key进行一些列操作生成Value值
		//当没有Put的时候,根据function传入的value值,计算出value值,
		//当有put的时候,对应的value值就是放入的value值
		
		//线程安全,高并发性能,异步超时清理,自定义构建元素等强大功能于一身
		ConcurrentMap<String, Integer> mapMaker = new MapMaker()
		//这一串暂时不知是啥意思,根据给出的例子是这么写的。只知道运用了Builder模式
				// new MapMaker().weakKeys()   // 指定Map保存的Key为WeakReference机制
				// new MapMaker().weakValues() // 指定Map保存的Value为WeakReference机制
				.concurrencyLevel(32).softKeys().weakValues().expiration(30,
						TimeUnit.MINUTES).makeComputingMap(
						new Function<String, Integer>() {
							@Override
							public Integer apply(String key) {
								return Integer.parseInt(key) + 100;
							}
						});
		//如果沒有Put的时候,按照Function提供的算法
		System.out.println("没有Put的情况下:"+mapMaker.get("111"));
		
		//如果有Put的情况下,按照指定的值赋予Value值
		mapMaker.put("111", 123);
		System.out.println("有Put的情况下:"+mapMaker.get("111"));
	}
	
	public static void main(String[] args) {
		GoogleCollectionsDemo demo = new GoogleCollectionsDemo();
		demo.testImmutableList();
		demo.testMultiset();
		demo.testMultimap();
		demo.testBiMap();
		demo.testMapMaker();
	}
}

运行结果:

JDK1.5方式:[james, bryant]
Google Collections获取方式1:[james, bryant]
Google Collections获取方式2:[james, bryant]
获取Multiset中的james个数:2
Multiset中的元素:[james x 2, bryant]
23号球员有:[james, jordan]
获取23Cel的值:james
获取值为bryant的key值:24Laker
没有Put的情况下:211
有Put的情况下:123
  • 无匹配
  • 无匹配
boardmodelpaper.com 说:
2024年1月23日 19:40

The Board model paper" typically refers to a sample or model question paper that is designed by educational boards or institutions for various exams. These papers serve as practice material for students preparing for exams, providing them with an idea of the question format, difficulty level, and the type of content that may be covered in the actual examination. boardmodelpaper.com Model papers are usually created for specific subjects or courses. They cover a range of topics and chapters that students are expected to have studied during the academic term. Students often use these educational board model papers as an integral part of their exam preparation strategy, helping them familiarize themselves with the exam pattern and refine their understanding of the subject matter.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter