标签:使用 com ade 集合排序 ret pen aries 排序 复制
Guava 是java api的增强与扩展,提供复杂的java 数据结构,使你的代码更简短精炼,具有良好的可读性。看看guava给我们提供了哪些很酷的功能:
Map<String, Map<String, String>> map = Maps.newHashMap(); List<List<Map<String, String>>> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet("one","two","three"); List<String> list = Lists.newArrayList("one","two","three");
ImmutableSet<String> immutableSet = ImmutableSet.of("RED", "GREEN");
从构造的方式来说,ImmutableSet 集合还提供了 Builder 模式来构造一个集合 :
Builder<String> builder = ImmutableSet.builder(); ImmutableSet<String> immutableSet = builder.add("RED").addAll(set).build();
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));
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
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)); // 得到行集合 Map<Integer,Person> rowMap= personTable.row(0); int maxAge= Collections.max(rowMap.keySet());
//双向map BiMap<Integer,String> biMap=HashBiMap.create(); biMap.put(1,"hello"); biMap.put(2,"my"); int value= biMap.inverse().get("my");
ClassToInstanceMap<Person> classToInstanceMap =MutableClassToInstanceMap.create(); classToInstanceMap.putInstance(Person.class, new Person(1,20,"abc","46464",1,100)); Person person = classToInstanceMap.getInstance(Person.class);
Collections2.filter(personList,new Predicate<Person>() { @Override public boolean apply( Person input) { return input.getSex()==0; } })
同时可以转换一个集合为另外一个集合:
Lists.transform(personList,new Function<Person, String>() { @Override public String apply( Person input) { return input.getName(); } })
ConcurrentMap<String, Object> mapAll = new MapMaker() .concurrencyLevel(8) .softKeys() .weakValues() .expireAfterWrite(30, TimeUnit.SECONDS) .maximumSize(100) .makeComputingMap( new Function<String, Object>() { public Object apply(String key) { return createObject(key); }});
Lists.newArrayList(30, 20, 60, 80, 10); Ordering.natural().sortedCopy(numbers); //10,20,30,60,80 Ordering.natural().reverse().sortedCopy(numbers); //80,60,30,20,10 Ordering.natural().min(numbers); //10 Ordering.natural().max(numbers); //80 Lists.newArrayList(30, 20, 60, 80, null, 10); Ordering.natural().nullsLast().sortedCopy(numbers); //10, 20,30,60,80,null Ordering.natural().nullsFirst().sortedCopy(numbers); //null,10,20,30,60,80
List<Person> personList=Lists.newArrayList( new Person(3, 1, "abc", "46546", 0, 25), new Person(2, 1, "ab", "46546", 0, 30), new Person(5, 1, "ade", "46546",0, 27) ); Ordering<Person> byAge = new Ordering<Person>() { @Override public int compare( Person left, Person right) { return right.getAge()-left.getAge(); } }; for(Person p: byAge.immutableSortedCopy(personList)) { System.out.println(p); }
// 使用ComparisonChain比较多字段
Collections.sort(list, new Comparator<Foo>(){
@Override return ComparisonChain.start() .compare(f1.a, f2.a, comparatorA) .compare(f1.b, f2.b, comparatorB) .compare(f1.c, f2.c, comparatorC).result(); }});
maven坐标:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0-rc2</version>
</dependency>
对于开发人员来说,Guava 确实是一把利器,封装了许多实用的API,让我们编写代码更简单易读,这篇文章是我学习Guava的总结,希望大家能从中有所收获。
标签:使用 com ade 集合排序 ret pen aries 排序 复制
原文地址:http://www.cnblogs.com/diegodu/p/6119749.html