近くにGava(三):集合

8121 ワード

集合:
FlouentIterable類:
  • FlouentIterable.filterフィルタリング、すなわちPredicateを用いて実現される:
  • Iterable<Person> adults = FluentIterable.from(ps).filter(
    	new Predicate<Person>() {
    		@Override
    		public boolean apply(Person p) {
    			return p.getAge() >= 18; //   >18
    		}
    	});
  • FlouentIterable.tranform()変換を使用して、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.newArayListを用いてリストを作成する:
  • 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()はS 1-S 2
  • を求めます.
    Set<String> s1 = Sets.newHashSet("1", "2", "3");
    Set<String> s2 = Sets.newHashSet("2", "3", "4");
    Sets.difference(s1, s2); //[1]
  • Sets.intersection()はS 1を求めて、S 2は
  • を交差させます.
    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()と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:
  • 個のキーは複数のvalueに対応する.
  • ArayListMultiMap:
    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]}
    重複値が発生すると、ArayListMultiMapのvalueの時にArayListが追加されます.
    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,   LinkedHashMap
    TreeMultimap //    MultiMap,   TreeMap
    //      map
    ImmutableListMultimap
    ImmutableMultimap
    ImmutableSetMultimap
    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
    テーブル:
  • は、2つのkey[行,列]を有し、1つの値に対応する.
  • Hash BasedTable:
  • 汎用動作
  • 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)); //true
    System.out.println(table.containsColumn(2)); //true
    System.out.println(table.containsRow(1)); //true
    System.out.println(table.containsValue("Rook")); //true
    System.out.println(table.remove(1, 3)); //Bishop
    System.out.println(table.get(3, 4)); //null
  • Table view、テーブル行列図
  • Map<Integer,String> columnMap = table.column(1);
    Map<Integer,String> rowMap = table.row(2);
  • 他のテーブル
  • ArrayTable //      
    ImmutableTable //   table,       
    TreeBasedTable //      table
    Range:
  • は範囲のクラスを表します.
  • Range<Integer> numberRange = Range.closed(1, 10); //    
    System.out.println(numberRange.contains(10)); //true
    System.out.println(numberRange.contains(1)); //true
    		
    Range<Integer> numberRange1 = Range.open(1,10); //    
    System.out.println(numberRange1.contains(10)); //false
    System.out.println(numberRange1.contains(1)); //false
  • RangeとFunctionは、Predicateのフィルタリング条件
  • に組み合わされる.
    Range<Integer> ageRange = Range.closed(35, 50);
    //Person age   function
    Function<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 
    見たところGavaは集合の方面でやはりとても力があります.
    指摘を惜しまない.