2012-11-24
這陣子看了Guava相關資料後有了些心得,將它做了個重點筆記有需要的可以拿去參考...:)
===========================================================================
常用package清單
com.google.common.base===========================================================================
com.google.common.collect
com.google.common.io
com.google.common.math
com.google.common.primitives
Function<A,B> == A => B,配合Colllections2.transform(),Iterables.transform(),Iterators.transform()使用3.常用class的API或SampeCode如下
Predicate<T> == T => Boolean,配合Colllections2.filter(),Iterables.filter(),Iterators.filter()使用
Optional<T> == "Present" or "Absent"
CharMatcher.DIGIT.retainFrom("test 1234");
CharMatcher.ASCII.retainFrom("test 1234");
String numbersAsString = Joiner.on(";").skipNulls().join(Ints.asList(numbers));
Iterable<String> splitResult = Splitter.on(",").split(numbsAsString);
Preconditions.checkArgument(count > 0, "must be positive: %s", count);
Objects.equal(Object a, Object b)
Objects.hashCode(Object... objects)
Objects.toStringHelper(Object self)
Strings.isNullOrEmpty(String string)
Strings.nullToEmpty(String string)
Strings.padStart(String string, int minLength, char padChar)
Strings.padEnd(String string, int minLength, char padChar)
Charsets.UTF_8
Charsets.UTF_16
Charsets.ISO_8859_1
Stopwatch stopwatch = new Stopwatch().start();===========================================================================
stopwatch.stop();
long millis = stopwatch.elapsedMillis();
Multiset:Set裡許可放入重覆的元素3.提供集合相加減功能
Multimaps:Map裡的同個key可以有多個value值
BiMap:Map裡的key跟value都只有唯一值
Immutable Collections:集合中的元素都不可變
Sets.union(a, b);//a聯集b4.常用class的API或SampeCode如下
Sets.intersection(a, b);//a交集b
Sets.symmetricDifference(a, b);//a聯集b - a交集b
Sets.difference(a, b);//a裡面減掉b的部份
Sets.difference(b, a);//b裡面減掉a的部份
MapDifference differenceMap = Maps.difference(a, b);
differenceMap.entriesDiffering();
differenceMap.entriesOnlyOnLeft();
differenceMap.entriesInCommon();
Maps.newHashMap();
Lists.newArrayList();
Sets.newHashSet();
Map<String, Integer> map = Maps.newHashMap();
List<Integer> list = Lists.newArrayList(1, 2, 3);
HashMultiset.create();
ArrayListMultimap.create();
BiMap.create();
ImmutableList<String> list = ImmutableList.of("a", "b", "c", "d");
ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");
ImmutableMap<String, Integer> map = new ImmutableMap.Builder<String, Integer>().put("one", 1).put("two", 2).put("three", 3).build();
Iterables、Iterators、Collections2這三個class提供了相似的功能,有相關需求可在這裡面找符合的功能來用
Iterables.filter(Iterable<T> unfiltered, Predicate<? super T> predicate),過濾集合所有元素,回傳符合結果的新集合
Iterables.transform(Iterable<F> fromIterable, Function<? super F,? extends T> function),將集合每個內容轉換成其它內容後回傳整個新集合
Iterables.partition(Iterable<T> iterable, int size),將集合切割成多等份
List<Integer> integers = Lists.newArrayList(1, 2, 3);
List<String> strings = Lists.transform(integers, Functions.toStringFunction());
integers.add(4);
integers.add(5);
assertThat(strings.size()).isEqualTo(5);
FluentIterable.from(database.getClientList()).filter(activeInLastMonth()).transform(Functions.toStringFunction()).limit(10).toImmutableList();
Constraints.constrainedCollection(Collection<E> collection, Constraint<? super E> constraint)
public int compareTo(Foo that) {
return ComparisonChain.start()
.compare(this.aString, that.aString)
.compare(this.anInt, that.anInt)
.compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
.result();
}
Ordering.natural().lexicographical().compare(T left, T right);
ObjectArrays.concat(T[] array, T element)
ObjectArrays.concat(T element, T[] array)
ObjectArrays.concat(T[] first, T[] second, Class<T> type)
ConcurrentMap<String, Object> mapExpireAfter30Sec = new MapMaker().expireAfterWrite(30, TimeUnit.SECONDS).makeMap();===========================================================================
String content = Resources.toString(url);
String[] lines = Resources.readLines(url, Charsets.UTF_8);
byte[] content = Resources.toByteArray(url);
URI uri = Resources.getResource("test.txt").toURI();
List<String> lines = Files.readLines(new File(uri), Charsets.UTF_8);
String content = Files.toString(textFile, Charsets.UTF_8);
byte[] byteArray = Files.toByteArray(binaryFile);
Files.copy(fromFile, toFile);
CharStreams、ByteStreams這二個class針對Stream提供了多樣的操作功能===========================================================================
List<String> lines = CharStreams.readLines(reader);
CharStreams.copy(reader, writer);
String content = CharStreams.toString(reader);
IntMath.checkedAdd(int a, int b),Returns the sum of a and b, provided it does not overflow.===========================================================================
IntMath.gcd(int a, int b)
IntMath.log10(int x, RoundingMode mode)
Ints.compare(int a, int b)
Ints.max(int... array)
Ints.min(int... array)
String joined = Ints.join(",", intArray);