创建List、Map
Before:
Map
<String, Map
<Long, List
<String
>>> map
=
new HashMap
<String, Map
<Long,List
<String
>>>();
After:(JDK7将实现该功能)
Map
<String, Map
<Long, List
<String
>>> map
= Maps.newHashMap();
Before:
List
<String
> list
=
new ArrayList
<String
>();
list.add( "a" );
list.add( "b" );
list.add( "c" );
list.add( "d" );
list.add( "a" );
list.add( "b" );
list.add( "c" );
list.add( "d" );
After:
List
<Integer
> list
= Lists.newArrayList(
1 ,
2 ,
3 );
针对不可变集合:
ImmutableList
<String
> of
= ImmutableList.of(
"a" ,
"b" ,
"c" ,
"d" );
ImmutableMap <String,String > map = ImmutableMap.of( "key1" , "value1" , "key2" , "value2" );
ImmutableMap <String,String > map = ImmutableMap.of( "key1" , "value1" , "key2" , "value2" );
Sets
set的交集, 并集, 差集的用法(
http://publicobject.com/2008/08/coding-in-small-with-google-collections.html)
HashSet setA
= newHashSet(
1 ,
2 ,
3 ,
4 ,
5 );
HashSet setB = newHashSet( 4 , 5 , 6 , 7 , 8 );
SetView union = Sets.union(setA, setB);
System.out.println( "union:" );
for (Integer integer : union)
System.out.println(integer);
SetView difference = Sets.difference(setA, setB);
System.out.println( "difference:" );
for (Integer integer : difference)
System.out.println(integer);
SetView intersection = Sets.intersection(setA, setB);
System.out.println( "intersection:" );
for (Integer integer : intersection)
System.out.println(integer);
HashSet setB = newHashSet( 4 , 5 , 6 , 7 , 8 );
SetView union = Sets.union(setA, setB);
System.out.println( "union:" );
for (Integer integer : union)
System.out.println(integer);
SetView difference = Sets.difference(setA, setB);
System.out.println( "difference:" );
for (Integer integer : difference)
System.out.println(integer);
SetView intersection = Sets.intersection(setA, setB);
System.out.println( "intersection:" );
for (Integer integer : intersection)
System.out.println(integer);
Maps
针对Map的用法:
MapDifference differenceMap
=
Maps.difference(mapA, mapB);
differenceMap.areEqual();
Map entriesDiffering = differenceMap.entriesDiffering();
Map entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft();
Map entriesOnlyOnRight = differenceMap.entriesOnlyOnRight();
Map entriesInCommon = differenceMap. entriesInCommon();
differenceMap.areEqual();
Map entriesDiffering = differenceMap.entriesDiffering();
Map entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft();
Map entriesOnlyOnRight = differenceMap.entriesOnlyOnRight();
Map entriesInCommon = differenceMap. entriesInCommon();
ImmutableList
Before:
public Directions(Address from, Address to, List
<Step
> steps) {
this .from = from;
this .to = to;
this .steps = Collections.unmodifiableList( new ArrayList<Step>(steps));
}
this .from = from;
this .to = to;
this .steps = Collections.unmodifiableList( new ArrayList<Step>(steps));
}
After:
public Directions(Address from, Address to, List
<Step
> steps) {
this .from = from;
this .to = to;
this .steps = ImmutableList.of(steps);
}
this .from = from;
this .to = to;
this .steps = ImmutableList.of(steps);
}
Iterables.getOnlyElement()
针对集合中只有一个元素的情况: