起因

昨天晚上闲自己的java code写的很一般,于是就把PTP的collector 部分的code拿来看,发现自己有很多地方需要学习。首先吸引我的是Google guava


练习

optional

1
2
3
4
5
6
7
8
9
10
11
12
Optional<Integer> optional = Optional.of(4);//Optional.absent();Optional.fromNullable(val);
System.out.println(optional.isPresent());
System.out.println(optional.get());
System.out.println(optional.or(3));//default value
System.out.println(optional.orNull());//inverse of fromNullable
System.out.println(optional.asSet()
.size());//Returns an immutable singleton Set containing the instance in this Optional, if there is one, or otherwise an empty immutable set.
System.out.println(Strings.emptyToNull(""));
System.out.println(Strings.isNullOrEmpty(""));
System.out.println(Strings.nullToEmpty(null));

Preconditions

1
2
3
4
5
6
7
Preconditions.checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
Preconditions.checkArgument(i < j, "Expected i < j, but %s >= %s",i,j);
Preconditions.checkNotNull(null);
Preconditions.checkState(boolean);
Preconditions.checkElementIndex(2,2);//exception,start from 0;
Preconditions.checkPositionIndex(2,2);//no exception, start from 1
Preconditions.checkPositionIndexes(3,2,2);

Ordering

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Ordering<String> ordering = Ordering.natural();Ordering.usingToString();
List<String>strs = Arrays.asList("dd","aa","bb","cc");
List<Integer>ints = Arrays.asList(4,3,2,11,9);
List<Integer>list1 = Lists.newArrayList(1,2,3);
List<Integer>list2=Lists.newArrayList(-1,100,4);
List<Integer>list3=Lists.newArrayList(0,98,4);
List<List<Integer>>lists = Lists.newArrayList(list1,list2,list3);
System.out.println(Ordering.<Integer>natural().lexicographical().sortedCopy(lists));
System.out.println(ordering.sortedCopy(strs));
Collections.sort(strs,Ordering.natural().nullsFirst().reversed());
System.out.println(strs);
System.out.println(Ordering.natural().nullsLast().compound(Ordering.natural()).sortedCopy(ints));
System.out.println(Ordering.natural().isOrdered(strs));
System.out.println(Ordering.natural().max(1,2,3,4));
System.out.println(Ordering.natural().max(ints));
System.out.println(Ordering.natural().greatestOf(ints,3));
System.out.println(Ordering.natural().leastOf(ints,3));
List<Person>person = Lists.newArrayList(new Person(1,"Tao"),new Person(2,"Xia"));
System.out.println(Ordering.natural().nullsFirst().reverse().onResultOf(new Function<Person, Integer>() {
public Integer apply(Person p){
return p.age;
}
}).sortedCopy(person));
Collections.sort(person, new Ordering<Person>() {
@Override
public int compare(@Nullable Person person, @Nullable Person t1) {
return ComparisonChain.start()
.compare(person.name,t1.name)
.compare(person.age,t1.age)
.result();
}
});

Object methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
System.out.println(Objects.equal("a","a"));
System.out.println(Objects.equal("a",null));
System.out.println(Objects.equal("b",null));
System.out.println(Objects.equal(null,null));
Person a = new Person(2,"Tao");
Person b =a;
System.out.println(Objects.hashCode(a));
System.out.println(Objects.hashCode(b));
System.out.println(MoreObjects.toStringHelper(a).add("x",1));
Ordering<Person>personOrdering = Ordering.from(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return ComparisonChain.start()
.compare(o1.age,o2.age)
.compare(o1.name,o2.name)
.result();
}
});

Throwables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
try{
int bb=0;
System.out.println(1/bb);
Person p=null;
System.out.println(p.age);
}catch (java.lang.NullPointerException t){
t.printStackTrace();
} catch (Throwable t){
Throwables.propagateIfInstanceOf(t, IOException.class);
Throwables.propagateIfInstanceOf(t, SQLException.class);
throw Throwables.propagate(t);
Throwables.getRootCause(t).printStackTrace();
System.out.println(Throwables.getStackTraceAsString(t));
List<Throwable>throwables=Throwables.getCausalChain(t);
for(Throwable throwable:throwables)
throwable.printStackTrace();
};