多网站建设,形象类网站,凡科互动自动答题,阿里云服务器wordpress配置1#xff0c;大纲 让我们来熟悉瓜娃#xff0c;并体验下它的一些API,分成如下几个部分#xff1a; IntroductionGuava Collection APIGuava Basic UtilitiesIO APICache API2#xff0c;为神马选择瓜娃#xff1f; 瓜娃是java API蛋糕上的冰激凌#xff08;精华#xff… 1大纲 让我们来熟悉瓜娃并体验下它的一些API,分成如下几个部分 IntroductionGuava Collection APIGuava Basic UtilitiesIO APICache API2为神马选择瓜娃 瓜娃是java API蛋糕上的冰激凌精华高效设计良好的API.被google的开发者设计实现和使用。遵循高效的java这本书的好的语法实践。使代码更刻度简洁简单。使用java 1.5的特性流行的API动态的开发它提供了大量相关的应用类集合多线程比较字符串输入输出缓存网络原生类型数学反射等等百分百的单元测试被很多的项目使用帮助开发者专注业务逻辑而不是写java应用类节省时间资源提高生产力我的目的是为基本的java特征提供开源代码的支持而不是自己再写一个Apache Common库-Apache是一个很好的成熟的库但是不支持泛型Apache对早起的java版本很有用1.5之前的java7java8 最新的java支持一些guava的APImaven dependency groupIdcom.google.guava/groupId artifactIdguava/artifactId version22.0/version/dependency 3,集合API的使用 3.1简化工作 可以简化集合的创建和初始化 类别原来的写法guava的写法集合创建 MapString, MapString, String map new HashMapString, MapString,String(); ListListMapString, String list new ArrayListListMapString,String(); MapString, MapString, String map Maps.newHashMap(); ListListMapString, String list Lists.newArrayList(); //1,简化集合的创建ListPerson personList Lists.newLinkedList();SetPerson personSet Sets.newHashSet();MapString,Person personMap Maps.newHashMap();Integer[] intArrays ObjectArrays.newArray(Integer.class,10); 集合初始化 SetString set new HashSetString(); set.add(one); set.add(two); set.add(three); SetString set Sets.newHashSet(one,two,three); ListString list Lists.newArrayList(one,two,three); MapString, String map ImmutableMap.of(ON,TRUE,OFF,FALSE); //2,简化集合的初始化ListPerson personList2 Lists.newArrayList(new Person(1, 1, a, 46546, 1, 20), new Person(2, 1, a, 46546, 1, 20));SetPerson personSet2 Sets.newHashSet(new Person(1,1,a,46546,1,20), new Person(2,1,a,46546,1,20));MapString,Person personMap2 ImmutableMap.of(hello,new Person(1,1,a,46546,1,20),fuck,new Person(2,1,a,46546,1,20)); 3.2 不变性 很大一部分是google集合提供了不变性不变对比可变 数据不可改变线程安全不需要同步逻辑 可以被自由的共享容易设计和实现 内存和时间高效不变对比不可修改 google的不变被确保真正不可改变而不可修改实际上还是可以修改数据如下所示 SetInteger data new HashSetInteger(); data.addAll(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80)); SetInteger fixedData Collections.unmodifiableSet(data); // fixedData - [50, 70, 80, 20, 40, 10, 60, 30] data.add(90); // fixedData - [50, 70, 80, 20, 40, 10, 90, 60, 30] 如何创建不可变的集合 ImmutableSetInteger numbers ImmutableSet.of(10, 20, 30, 40, 50); 使用copyOf方法 ImmutableSetInteger another mmutableSet.copyOf(numbers); 使用Builder方法 ImmutableSetInteger numbers2 ImmutableSet.Integerbuilder().addAll(numbers) .add(60) .add(70).add(80).build(); //3,创建不可变的集合 ImmutableListPerson personImmutableListImmutableList.of(new Person(1, 1, a, 46546, 1, 20), new Person(2, 1, a, 46546, 1, 20)); ImmutableSetPerson personImmutableSetImmutableSet.copyOf(personSet2); ImmutableMapString,Person personImmutableMapImmutableMap.String,Personbuilder().put(hell,new Person(1,1,a,46546,1,20)).putAll(personMap2) .build(); 3.3 新的集合类型 The Guava API provides very useful new collection types that work very nicely with existing java collections. guava API 提供了有用的新的集合类型协同已经存在的java集合工作的很好。 分别是 MultiMap MultiSet Table BiMap ClassToInstanceMap 种类写的例子MultiMap 一种key可以重复的map子类有ListMultimap和SetMultimap对应的通过key分别得到list和set MultimapString, 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.getAge());} MultiSet 不是集合可以增加重复的元素并且可以统计出重复元素的个数例子如下 private static void testMulitiSet() {MultisetInteger multiSet HashMultiset.create();multiSet.add(10);multiSet.add(30);multiSet.add(30);multiSet.add(40); System.out.println( multiSet.count(30)); // 2System.out.println( multiSet.size()); //4} Table 相当于有两个key的map不多解释 private static void testTable() {TableInteger,Integer,Person personTableHashBasedTable.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)); //1,得到行集合MapInteger,Person rowMap personTable.row(0);int maxAge Collections.max(rowMap.keySet()); } BiMap是一个一一映射可以通过key得到value也可以通过value得到key private static void testBitMap() {//双向mapBiMapInteger,String biMapHashBiMap.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); } ClassToInstanceMap 有的时候你的map的key并不是一种类型他们是很多类型你想通过映射他们得到这种类型guava提供了ClassToInstanceMap满足了这个目的。 除了继承自Map接口ClassToInstaceMap提供了方法 T getInstance(ClassT) 和 T putInstance(ClassT, T),消除了强制类型转换。 该类有一个简单类型的参数通常称为B代表了map控制的上层绑定例如 ClassToInstanceMapNumber numberDefaults MutableClassToInstanceMap.create();numberDefaults.putInstance(Integer.class, Integer.valueOf(0)); 从技术上来说ClassToInstanceMapB 实现了MapClass? extends B, B或者说这是一个从B的子类到B对象的映射这可能使得ClassToInstanceMap的泛型轻度混乱但是只要记住B总是Map的上层绑定类型通常来说B只是一个对象。 guava提供了有用的实现 MutableClassToInstanceMap 和 ImmutableClassToInstanceMap. 重点像其他的MapClass,Object,ClassToInstanceMap 含有的原生类型的项目一个原生类型和他的相应的包装类可以映射到不同的值 private static void testClass() {ClassToInstanceMapPerson classToInstanceMap MutableClassToInstanceMap.create(); Person person new Person(1,20,abc,46464,1,100); classToInstanceMap.putInstance(Person.class,person); // System.out.println(string:classToInstanceMap.getInstance(String.class));// System.out.println(integer: classToInstanceMap.getInstance(Integer.class)); Person person1classToInstanceMap.getInstance(Person.class); } 3.4 谓词和筛选 谓词Predicate是用来筛选集合的 谓词是一个简单的接口只有一个方法返回布尔值但是他是一个很令人惊讶的集合方法当你结合collections2.filter方法使用这个筛选方法返回原来的集合中满足这个谓词接口的元素 举个例子来说筛选出集合中的女人 public static void main(String[] args){OptionalImmutableMultisetPerson optionalOptional.fromNullable(testPredict()); if(optional.isPresent()){for(Person p:optional.get()){System.out.println(女人p);}} System.out.println(optional.isPresent());} public static ImmutableMultisetPerson testPredict(){ListPerson personListLists.newArrayList(new Person(1, 1, a, 46546, 1, 20),new Person(2, 1, ab, 46546, 0, 30),new Person(3, 1, abc, 46546, 0, 25),new Person(4, 1, aef, 46546, 1, 50),new Person(5, 1, ade, 46546,0, 27),new Person(6, 1, acc, 46546, 1, 29),new Person(7, 1, add, 46546,0, 33)); return ImmutableMultiset.copyOf(Collections2.filter(personList,new PredicatePerson() {Overridepublic boolean apply( Person input) {return input.getSex()0;}}));} Predicates含有一些内置的筛选方法比如说 in ,and ,not等根据实际情况选择使用。 3.5 功能和转换 转换一个集合为另外一个集合 实例如下 public static void main(String[] args){OptionalImmutableMultisetString optionalOptional.fromNullable(testTransform()); if(optional.isPresent()){for(String p:optional.get()){System.out.println(名字p);}} System.out.println(optional.isPresent());} public static ImmutableMultisetString testTransform(){ListPerson personListLists.newArrayList(new Person(1, 1, a, 46546, 1, 20),new Person(2, 1, ab, 46546, 0, 30),new Person(3, 1, abc, 46546, 0, 25),new Person(4, 1, aef, 46546, 1, 50),new Person(5, 1, ade, 46546,0, 27),new Person(6, 1, acc, 46546, 1, 29),new Person(7, 1, add, 46546,0, 33)); return ImmutableMultiset.copyOf(Lists.transform(personList,new FunctionPerson, String() {Overridepublic String apply( Person input) {return input.getName();}}));} 3.6 排序 是guava一份非常灵活的比较类可以被用来操作扩展当作比较器排序提供了集合排序的很多控制 实例如下 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 public static void testOrdering(){ListPerson personListLists.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),new Person(1, 1, a, 46546, 1, 20),new Person(6, 1, acc, 46546, 1, 29),new Person(4, 1, aef, 46546, 1, 50),new Person(7, 1, add, 46546,0, 33)); OrderingPerson byAgenew OrderingPerson() {Overridepublic int compare( Person left, Person right) {return right.getAge()-left.getAge();}}; for(Person p: byAge.immutableSortedCopy(personList)){System.out.println(p);}} guava在结合部分的API使用记录完毕希望对广大屌丝有所帮助。 no pays,no gains!转载于:https://www.cnblogs.com/dzcWeb/p/6999694.html