`
Tristan_S
  • 浏览: 373070 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

guava

    博客分类:
  • bf
 
阅读更多
List<List<Rule>> partitionList = Lists.partition(rules, partitions);
可以对list进行分区

-------------------------

package com.tristan;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.BiMap;
import com.google.common.collect.Collections2;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.ObjectArrays;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;

public class TestGuava {
	public static void main(String[] args) {

		// 集合申明
		// t1();

		// 集合初始化
		// t2();

		// 新的集合
		//一种key可以重复的map
		//multimap();
		multimap2();

		/*	//可以重复的set 可以统计重复数
		multiset();
		
		//相当于有两个key的map
		table();
		
		//双向map
		biMap();*/
		
	}

	
	private static void biMap() {
		BiMap<Integer,String> biMap=HashBiMap.create();

		biMap.put(1,"hello");
		biMap.put(2,"helloa");
		biMap.put(3,"world");
		biMap.put(4,"worldb");
		biMap.put(5,"my");
		biMap.put(6,"myc");

		int value= biMap.inverse().get("my");
		System.out.println("my --"+value);
	}

	private static void table() {
		Table<Integer,Integer,Person> personTable=HashBasedTable.create();
		personTable.put(1,20,new Person(1, 1, "a", "46546", 1, 20));
		personTable.put(0,30,new Person(2, 1, "ab", "46546", 0, 30));
		personTable.put(0,25,new Person(3, 1, "abc", "46546", 0, 25));
		personTable.put(1,50,new Person(4, 1, "aef", "46546", 1, 50));
		personTable.put(0,27,new Person(5, 1, "ade", "46546",0, 27));
		personTable.put(1,29,new Person(6, 1, "acc", "46546", 1, 29));
		personTable.put(0,33,new Person(7, 1, "add", "46546",0, 33));
		personTable.put(1,66,new Person(8, 1, "afadsf", "46546", 1, 66));

		
		Person person = personTable.get(1, 29);
		System.out.println(person.f3);
		
		Map<Integer,Person> rowMap= personTable.row(0);
		int maxAge= Collections.max(rowMap.keySet());
		System.out.println(maxAge);
	}

	private static void multiset() {
		Multiset<Integer> multiSet = HashMultiset.create();
		multiSet.add(10);
		multiSet.add(30);
		multiSet.add(30);
		multiSet.add(40);

		System.out.println( multiSet.count(30)); // 2
		System.out.println( multiSet.size());	//4
		
		for (Integer integer : multiSet) {
			System.out.println(integer);
		}
	}

	private static void multimap() {
		Multimap<String, Person> customersByType = ArrayListMultimap.create();
		customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 20));
		customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 30));
		customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 40));
		customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 50));
		customersByType.put("abcd", new Person(1, 1, "a", "46546", 1, 50));
		customersByType.put("abcde", new Person(1, 1, "a", "46546", 1, 50));

		for (Person person : customersByType.get("abc")) {
			System.out.println(person.age);
		}
	}

	private static void multimap2() {
		Multimap<String, String> multimap = ArrayListMultimap.create();
		multimap.put("abc","a");
		multimap.put("abc", "b");
		multimap.put("abc", "c");
		multimap.put("abc", "c");
		multimap.put("abcd", "a");

		Collection<Entry<String, String>> entries = multimap.entries();
		for (Entry<String, String> entry : entries) {
			System.out.println(entry.getKey() +"   " + entry.getValue());
		}
		
		
	}
	
	private static void t2() {
		Set<String> set = Sets.newHashSet("one", "two", "three");

		List<String> list = Lists.newArrayList("one", "two", "three");

		Map<String, String> map = ImmutableMap.of("ON", "TRUE", "OFF", "FALSE");

		// 2,简化集合的初始化
		List<Person> personList2 = Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20), new Person(2, 1, "a",
				"46546", 1, 20));

		Set<Person> personSet2 = Sets.newHashSet(new Person(1, 1, "a", "46546", 1, 20), new Person(2, 1, "a", "46546",
				1, 20));

		Map<String, Person> personMap2 = ImmutableMap.of("hello", new Person(1, 1, "a", "46546", 1, 20), "fuck",
				new Person(2, 1, "a", "46546", 1, 20));
	}

	private static void t1() {
		Map<String, Map<String, String>> map = Maps.newHashMap();
		List<List<Map<String, String>>> list = Lists.newArrayList();

		List<Person> personList = Lists.newArrayList();
		Set<Person> personSet = Sets.newHashSet();
		Map<String, Person> personMap = Maps.newHashMap();
		Integer[] intArrays = ObjectArrays.newArray(Integer.class, 10);
	}

}

class Person {
	int id;
	int f2;
	String f3;
	String f4;
	int sex;
	int age;

	public Person(int f1, int f2, String f3, String f4, int f5, int f6) {
		super();
		this.id = f1;
		this.f2 = f2;
		this.f3 = f3;
		this.f4 = f4;
		this.sex = f5;
		this.age = f6;
	}

}


分享到:
评论

相关推荐

    guava-18.0(guava-18.0.jar和guava-18.0-sources.jar)

    Guava是Google开发的一个核心库,它为Java平台提供了许多实用工具类,涵盖了集合、并发、I/O、字符串处理、数学运算等多个方面。这个压缩包包含的是Guava库的18.0版本,分为两个部分:`guava-18.0.jar`和`guava-18.0...

    guava-17.0-API文档-中文版.zip

    赠送jar包:guava-17.0.jar; 赠送原API文档:guava-17.0-javadoc.jar; 赠送源代码:guava-17.0-sources.jar; 赠送Maven依赖信息文件:guava-17.0.pom; 包含翻译后的API文档:guava-17.0-javadoc-API文档-中文...

    guava-18.0-API文档-中文版.zip

    赠送jar包:guava-18.0.jar; 赠送原API文档:guava-18.0-javadoc.jar; 赠送源代码:guava-18.0-sources.jar; 包含翻译后的API文档:guava-18.0-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...

    guava-23.0-API文档-中文版.zip

    赠送jar包:guava-23.0.jar; 赠送原API文档:guava-23.0-javadoc.jar; 赠送源代码:guava-23.0-sources.jar; 赠送Maven依赖信息文件:guava-23.0.pom; 包含翻译后的API文档:guava-23.0-javadoc-API文档-中文...

    guava-11.0.2-API文档-中文版.zip

    赠送jar包:guava-11.0.2.jar; 赠送原API文档:guava-11.0.2-javadoc.jar; 赠送源代码:guava-11.0.2-sources.jar; 赠送Maven依赖信息文件:guava-11.0.2.pom; 包含翻译后的API文档:guava-11.0.2-javadoc-API...

    guava-20.0-API文档-中文版.zip

    赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...

    guava-19.0 jar和sources

    Guava是Google开发的一个核心库,它为Java平台提供了许多实用工具类,极大地丰富了标准库的功能。在Java开发中,Guava库被广泛使用,因为它包含了大量的集合框架、并发支持、缓存机制、字符串处理、I/O操作等多个...

    guava-20.0-API文档-中英对照版.zip

    赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...

    google开源项目guava.jar包

    谷歌的Guava库是Java开发中的一个非常重要的开源项目,它提供了一系列的高效、实用的工具类,大大简化了常见的编程任务。Guava的核心特性包括集合框架、缓存、原生类型支持、并发库、字符串处理、I/O操作等。这个...

    guava-r07.zip

    Guava是Google开发的一个Java库,它包含许多Google的核心库,如集合、缓存、并发库、原生类型支持、字符串处理、I/O等。Guava的R07版本是该库的一个特定发行版,可能包含了截止到那个版本的一些新特性、改进和错误...

    guava-28.2-jre-API文档-中文版.zip

    赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...

    guava-16.0.1-API文档-中文版.zip

    赠送jar包:guava-16.0.1.jar; 赠送原API文档:guava-16.0.1-javadoc.jar; 赠送源代码:guava-16.0.1-sources.jar; 赠送Maven依赖信息文件:guava-16.0.1.pom; 包含翻译后的API文档:guava-16.0.1-javadoc-API...

    guava-30.0-jre-API文档-中文版.zip

    赠送jar包:guava-30.0-jre.jar; 赠送原API文档:guava-30.0-jre-javadoc.jar; 赠送源代码:guava-30.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-30.0-jre.pom; 包含翻译后的API文档:guava-30.0-jre-...

    guava-31.1-jre.jar

    guava

    guava-27.0.1-jre-API文档-中文版.zip

    赠送jar包:guava-27.0.1-jre.jar; 赠送原API文档:guava-27.0.1-jre-javadoc.jar; 赠送源代码:guava-27.0.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-27.0.1-jre.pom; 包含翻译后的API文档:guava-...

    Guava-Cache本地缓存案例代码

    Guava Cache是Google Guava库中的一个强大特性,它提供了高效的本地缓存解决方案,用于存储经常访问的数据,以减少对远程服务或计算的调用,从而提高应用性能。本案例代码将详细介绍Guava Cache的使用,包括缓存的...

    guava-25.0-jre-API文档-中文版.zip

    赠送jar包:guava-25.0-jre.jar; 赠送原API文档:guava-25.0-jre-javadoc.jar; 赠送源代码:guava-25.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-25.0-jre.pom; 包含翻译后的API文档:guava-25.0-jre-...

    guava-30.1.1-jre-API文档-中文版.zip

    赠送jar包:guava-30.1.1-jre.jar; 赠送原API文档:guava-30.1.1-jre-javadoc.jar; 赠送源代码:guava-30.1.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-30.1.1-jre.pom; 包含翻译后的API文档:guava-30.1...

    guava(google的java集合包)

    Guava是Google为Java平台设计的一个开源库,它极大地扩展了Java的标准库,尤其是在集合、缓存、并发和I/O等方面。Guava的核心特性包括: 1. **集合框架增强**:Guava提供了丰富的集合类,如Multiset(多集)、...

    guava-26.0-jre.zip

    Guava是Google为Java平台设计并维护的一个开源库,它提供了许多高级功能和实用工具,极大地增强了Java开发的效率和代码质量。Guava 26.0-jre版本是针对Java运行时环境(JRE)优化的一个特定版本,旨在更好地支持Java...

Global site tag (gtag.js) - Google Analytics