打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
走近Guava(三): 集合

 集合:

FluentIterable类:

  • 使用FluentIterable.filter过滤, 即利用Predicate实现:
Iterable<Person> adults = FluentIterable.from(ps).filter(	new Predicate<Person>() {		@Override		public boolean apply(Person p) {			return p.getAge() >= 18; // 年龄>18		}	});
  • 使用FluentIterable.transform()转换,即利用Function实现:
FluentIterable.from(ps).transform(new Function<Person, String>() {	@Override	public String apply(Person p) {	     return Joiner.on('#').join(p.getName(), p.getAge());	}});

Lists类:

  • 使用Lists.newArrayList创建列表:
ps = Lists.newArrayList(	new Person("person1", 22),	new Person("person2", 23),	new Person("person3", 17));
  • 使用Lists.partition()方法分割列表:
//[a, b, c, d, e] --> [[a, b], [c, d, e]] -List<List<Person>> subList = Lists.partition(ps, 2);

Sets类:

  • Sets.difference()求S1-S2
Set<String> s1 = Sets.newHashSet("1", "2", "3");Set<String> s2 = Sets.newHashSet("2", "3", "4");Sets.difference(s1, s2); //[1]
  • Sets.intersection()求S1,S2交集
Set<String> s1 = Sets.newHashSet("1", "2", "3");Set<String> s2 = Sets.newHashSet("3", "2", "4");Sets.SetView<String> sv = Sets.intersection(s1, s2); // [2, 3]
  • Sets.union()求合集
Set<String> s1 = Sets.newHashSet("1", "2", "3");Set<String> s2 = Sets.newHashSet("3", "2", "4");Sets.SetView<String> sv = Sets.union(s1, s2); // [3, 2, 1 ,4]

Maps类:

  • Maps.uniqueIndex()将列表转换为map
//iterator各个元素作为Map.values, key为Function.apply返回值Maps.uniqueIndex(ps.iterator(), new Function<Person, String>() {	@Override	public String apply(Person p) {		return p.getName();	}});
  • Maps.asMap(),<K, V>和Maps.uniqueIndex()相反
Maps.asMap(ps, new Function<Person, String>() {	@Override	public String apply(Person p) {		return p.getName();	}});
  • Maps Transform API:
Maps.transformEntries(map, new Maps.EntryTransformer<String, Boolean, String>() {    @Override    public String transformEntry(String key, Boolean value) {        return value ? "yes" : "no";    }}); 将Map<String, Boolean> --> Map<String, String>, 其他的还有Maps.transformValues转换值

Multimaps:

  • 一个key对应多个value

ArrayListMultiMap:

ArrayListMultimap<String, String> multiMap = ArrayListMultimap.create();multiMap.put("Foo", "1");multiMap.put("Foo", "2");multiMap.put("Foo", "3");System.out.println(multiMap); // {Foo=[1,2,3]}
当出现重复值时,依然会被添加,因为ArrayListMultiMap的value时一个ArrayList:
ArrayListMultimap<String, String> multiMap = ArrayListMultimap.create();multiMap.put("Bar", "1");multiMap.put("Bar", "2");multiMap.put("Bar", "3");multiMap.put("Bar", "3");multiMap.put("Bar", "3");System.out.println(multiMap); //{Bar=[1, 2, 3, 3, 3]},相同的value会重复,value内部是一个List

HashMultiMap:

HashMultimap<String, String> multiMap = HashMultimap.create();multiMap.put("Bar", "1");multiMap.put("Bar", "2");multiMap.put("Bar", "3");multiMap.put("Bar", "3");multiMap.put("Bar", "3");System.out.println(multiMap); //{Bar=[3, 2, 1]}, 相同的value不会重复,value内部是一个Set

其他一些MultiMap:

LinkedHashMultimap //顺序的HashMultimap, 形如LinkedHashMapTreeMultimap //可排序的MultiMap, 形如TreeMap//一些不可变的mapImmutableListMultimapImmutableMultimapImmutableSetMultimap

BiMap:

  • 其限制value是唯一的,且通过value可以找到key
BiMap<String,String> biMap = HashBiMap.create();biMap.put("1","Tom");         biMap.put("2","Tom"); //抛出异常
  • BiMap.forcePut()强制放入value相等的entry:
BiMap<String, String> biMap = HashBiMap.create();biMap.put("1", "Tom");biMap.forcePut("2", "Tom");System.out.println(biMap); //{2=Tom}
  • BiMap.inverse()反转key-value
BiMap<String, String> biMap = HashBiMap.create();biMap.put("1", "Tom");biMap.put("2", "Harry");BiMap<String, String> inverseMap = biMap.inverse();System.out.println(biMap); //{2=Harry, 1=Tom}System.out.println(inverseMap); //{Harry=2, Tom=1

Table:

  • 它具有2个key[行, 列],对应一个值

HashBasedTable:

  • 通用操作
HashBasedTable<Integer, Integer, String> table = HashBasedTable.create();table.put(1, 1, "Rook");table.put(1, 2, "Knight");table.put(1, 3, "Bishop");System.out.println(table.contains(1, 1)); //trueSystem.out.println(table.containsColumn(2)); //trueSystem.out.println(table.containsRow(1)); //trueSystem.out.println(table.containsValue("Rook")); //trueSystem.out.println(table.remove(1, 3)); //BishopSystem.out.println(table.get(3, 4)); //null
  • Table views,表行列视图
Map<Integer,String> columnMap = table.column(1);Map<Integer,String> rowMap = table.row(2);
  • 其他table
ArrayTable //二维数组实现ImmutableTable //不可变table,创建后不能改变TreeBasedTable //对行列排序的table

Range:

  • 代表一种范围的类。
Range<Integer> numberRange = Range.closed(1, 10); //包括首尾System.out.println(numberRange.contains(10)); //trueSystem.out.println(numberRange.contains(1)); //true		Range<Integer> numberRange1 = Range.open(1,10); //除去首尾System.out.println(numberRange1.contains(10)); //falseSystem.out.println(numberRange1.contains(1)); //false
  • RangeFunction组合成Predicate的过滤条件
Range<Integer> ageRange = Range.closed(35, 50);//Person到age转换的functionFunction<Person, Integer> ageFunction = new Function<Person, Integer>() {	@Override	public Integer apply(Person person) {		return person.getAge();	}};//这样得到年龄再[35, 50]之间的人Predicate<Person> predicate = Predicates.compose(ageRange,ageFunction);
  • 创建不可变的集合
MultiMap<Integer,String> map = new ImmutableListMultimap.Builder<Integer,String>()                                   .put(1,"Foo").putAll(2,"Foo","Bar","Baz")                                   .putAll(4,"Huey","Duey","Luey")                                   .put(3,"Single").build();

Ordering:

  • Ordering提供了一些简单强大的排序功能。
/** * 城市人口比较器 */public class CityByPopluation implements Comparator<City> {	@Override	public int compare(City city1, City city2) {		return Ints.compare(city1.getPopulation(), city2.getPopulation());	}}
  • 反序
Ordering.from(cityByPopluation).reverse();
  • 处理Null
Ordering.from(comparator).nullsFirst();//null值最小放在前面
  • 二次排序
/** * 雨量比较器 */public class CityByRainfall implements Comparator<City> {	@Override	public int compare(City city1, City city2) {		return Doubles.compare(city1.getAverageRainfall(),				city2.getAverageRainfall());	}}
Ordering<City> secondaryOrdering = Ordering.from(cityByPopulation).compound(cityByRainfall);//组合比较器Collections.sort(cities,secondaryOrdering); //排序
  • 获取最大最小
Ordering<City> ordering = Ordering.from(cityByPopluation);List<City> topFive = ordering.greatestOf(cityList,5); //前5个List<City> bottomThree = ordering.leastOf(cityList,3); //最后3个
看来Guava在集合方面还是很给力的。

不吝指正。 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用好这个Java工具类库,代码量直接减少50%!
Google Guava的介绍和使用(转)
Guava学习笔记:Guava新增集合类型-Bimap
guava之Table
google collection
带你领略 Google Collections
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服