Please ref to the demo code: git clone https://github.com/AlphaWang/guava-demo.git
The Guava library has its history rooted in working with collections, starting out as google-collections. The Google Collections Library has long since been abandoned, and all the functionality from the original library has been merged into Guava.
Create
com.alphawang.guava.ch2.collection.Test1_Create
Create List/Set
123456789
// Before
List<String> list1 = new ArrayList<String>(2);
list1.add("A");
list1.add("B");
// After
List<String> list2 = Lists.newArrayList(); //Lists.newArrayListWithCapacity(2)
list2.add("A");
list2.add("B");
// Before
Map<String, String> map1 = new HashMap<String, String>();
// After
Map<String, String> map2 = Maps.newHashMapWithExpectedSize(2); // Maps.newHashMap();
Maps.newHashMap()
Maps.newHashMapWithExpectedSize()
Maps.newLinkedHashMap()
Maps.newTreeMap()
Immutable Collections
12345678
// Before
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
List<String> list1 = Collections.unmodifiableList(list);
// After
List<String> list2 = ImmutableList.of("A", "B");
12345678
// Before
Map<String, String> map = new HashMap<String, String>();
map.put("A", "1");
map.put("B", "2");
Map<String, String> map1 = Collections.unmodifiableMap(map);
// After
Map<String, String> map2 = ImmutableMap.of("A", "1", "B", "2");
Immutable objects have many advantages, including:
Safe for use by untrusted libraries.
Thread-safe: can be used by many threads with no risk of race conditions.
Doesn’t need to support mutation, and can make time and space savings with that assumption. All immutable collection implementations are more memory-efficient than their mutable siblings.
Can be used as a constant, with the expectation that it will remain fixed.
Maps.uniqueIndex method uses Function to generate keys from the given values.
Maps.asMap method takes a set of objects to be used as keys, and Function is applied to each key object to generate the value for entry into a map instance.
Maps.toMap returns ImmutableMap.
123456789101112131415161718
Function<City, Long> cityToIdFunction = new Function<City, Long>() {
@Override
public Long apply(City input) {
return input.getId();
}
};
@Test
public void listToMapAsValue() {
Map<Long, City> idCityMap = Maps.uniqueIndex(cities, cityToIdFunction);
}
@Test
public void listToMapAsKey() {
Map<City, Long> cityIdMap = Maps.toMap(cities, cityToIdFunction);
// or
cityIdMap = FluentIterable.from(cities).toMap(cityToIdFunction);
}
Map to List
12345678910111213141516
@Test
public void mapToList() {
Map<City, String> cityCommentMap = ImmutableMap.of(
new City(1L, "Shanghai", 1360L), "big",
new City(2L, "Beijing", 1020L), "dirty"
);
List<String> cityComments = FluentIterable.from(cityCommentMap.entrySet())
.transform(new Function<Map.Entry<City,String>, String>() {
@Override
public String apply(Map.Entry<City, String> input) {
return input.getKey().getName() + " IS " + input.getValue();
}
}).toList();
System.out.println("\nmapToList:\n" + cityComments);
}