Google Collections's technical principles and implementation in the Java library
Google Collections is an open source Java class library that provides many common tool categories and data structures for collecting operations.It is based on Google's GUAVA library and adds many useful functions and enhancements to the Java set framework.
Google Collections's technical principles mainly include the following aspects:
1. Uncharacteristic collection: Google Collections introduced a series of non -changing collection classes, which cannot be modified after the creation.The uncharacteristic set has better thread security and performance, and can be effectively used as a cache mechanism.
2. New collection class: In addition to the commonly used collection class in the Java collection framework, Google Collections also adds some new sets, such as Multimap, BIMAP and TABLE.These new collection classes provide richer data structure and functions that can better meet specific needs.
3. Functional programming: Many tool classes and interfaces in Google Collections use the idea of functional programming, such as Function, Predicate, and SUPPLIER.Functional programming can more concisely represent certain operations and logic, and can be easily used with Lambda expression introduced in Java 8.
4. Ididers extension: Google Collections provides many extended iterators tools, such as FluentIterable, Iterables, and Iterators.These tools can simplify the operation of the iterator and provide more functions, such as filtering, conversion and stitching.
Here are some examples of using Google Collections:
1. Create an uncharacteristic collection:
ImmutableList<Integer> immutableList = ImmutableList.of(1, 2, 3);
ImmutableSet<String> immutableSet = ImmutableSet.of("A", "B", "C");
2. Use Multimap:
Multimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.put("A", 1);
multimap.put("A", 2);
multimap.put("B", 3);
System.out.println (multimap.get ("a"); // Output: [1, 2]
3. Use functional programming:
List<Integer> numbers = ImmutableList.of(1, 2, 3, 4, 5);
List<Integer> evenNumbers = Lists.newArrayList(Iterables.filter(numbers, x -> x % 2 == 0));
System.out.println (EVENNUMBERS); // Output: [2, 4]
4. Use iterators to extend:
List<String> list = ImmutableList.of("A", "B", "C");
String concatenated = Iterables.concat(list, ImmutableList.of("D", "E")).stream()
.collect(Collectors.joining(", "));
System.out.println (Concateneated); // Output: A, B, C, D, E
In summary, Google Collections provides richer and more flexible collection operating tools and data structures for Java developers by introducing technical principles such as uncharacteristic collection, new collection classes, functional programming and iterators expansion.By using Google Collections reasonably, the readability, maintenance and replication of the code can be improved.