guava

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)