Guava中关于字符串的处理
String commonPrefix = Strings.commonPrefix("fenglang", "fengyue");
System.out.println(commonPrefix);
String commonSuffix = Strings.commonSuffix("fenglang", "fengling");
System.out.println(commonSuffix);
boolean nullOrEmpty = Strings.isNullOrEmpty("feng");
System.out.println(nullOrEmpty);
String emptyToNull = Strings.emptyToNull(null);
System.out.println(emptyToNull);
String nullToEmpty = Strings.nullToEmpty("");
System.out.println("-->" + nullToEmpty);
String padStart = Strings.padStart("feng", 6, 'g');
System.out.println(padStart);
String padEnd = Strings.padEnd("feng", 8, 'g');
System.out.println(padEnd);
String repeat = Strings.repeat("lang", 2);
System.out.println(repeat);
String sequence = "java12345python678cC++Ruby!-+冯朗sdk";
String retain = CharMatcher.JAVA_DIGIT.retainFrom(sequence);
System.out.println(retain);
String retain2 = CharMatcher.SINGLE_WIDTH.retainFrom(sequence);
System.out.println(retain2);
boolean matchesAnyOf = CharMatcher.is('c').matchesAnyOf(sequence);
System.out.println(matchesAnyOf);
int indexIn = CharMatcher.anyOf("abc").indexIn(sequence);
int countIn = CharMatcher.is('a').countIn(sequence);
System.out.println(indexIn + ":" + countIn);
String removeFrom = CharMatcher.anyOf("abc").removeFrom(sequence);
System.out.println(removeFrom);
String retainFrom = CharMatcher.anyOf("abc").retainFrom(sequence);
System.out.println(retainFrom);
String replaceFrom = CharMatcher.anyOf("abc").replaceFrom(sequence, "$");
System.out.println(replaceFrom);
String trimFrom = CharMatcher.anyOf("jadk").trimFrom(sequence);
System.out.println("trim:"+trimFrom);
String trimLeadingFrom = CharMatcher.anyOf("jdk").trimLeadingFrom(sequence);
System.out.println(trimLeadingFrom);
String trimTrailingFrom = CharMatcher.anyOf("jdk").trimTrailingFrom(sequence);
System.out.println(trimTrailingFrom);
String replace = CharMatcher.anyOf("apydk").replaceFrom(sequence, '@');
System.out.println(replace);
String collapseFrom = CharMatcher.anyOf("apydk").collapseFrom(sequence, '@');
System.out.println(collapseFrom);
String trimAndCollapseFrom = CharMatcher.anyOf("jadk").trimAndCollapseFrom(sequence, '#');
System.out.println(trimAndCollapseFrom);
String join = Joiner.on("#").skipNulls().join("A",null,"B");
System.out.println(join);
String join2=Joiner.on("#").useForNull("0").join("A",null,"B","C",null,"D");
System.out.println(join2);
StringBuilder sb=new StringBuilder();
StringBuilder appendTo = Joiner.on(";").skipNulls().appendTo(sb,"A","B","C","D");
System.out.println(sb+"-->"+appendTo);
Map<String,Object> map=Maps.newHashMap();
map.put("河南","郑州");
map.put("河北","石家庄");
map.put("湖南","长沙");
map.put("湖北","武汉");
String join3 = Joiner.on(",").withKeyValueSeparator("=").join(map);
System.out.println(join3);
List<String> list = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(" 1,a,b, ,3,5,6");
for (String str:list) {
System.out.println(str);
}
String sequence="k1=v1;k2=v2;k3=v3;";
Map<String, String> map = Splitter.on(";").trimResults().omitEmptyStrings().withKeyValueSeparator("=").split(sequence);
for(java.util.Map.Entry<String, String> entry:map.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
Guava中的Iterables的使用
List<String> list=Lists.newArrayList("Apple","Pear","Peach","Banana");
List<String> add=Lists.newArrayList("A","B","C","D");
boolean all = Iterables.all(list,new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("P");
}
});
System.out.println(all);
boolean any = Iterables.any(list,new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("P");
}
});
System.out.println(any);
Iterable<String> concat = Iterables.concat(list,add);
Iterator<String> iterator = concat.iterator();
while(iterator.hasNext()){
String next=iterator.next();
System.out.println(next);
}
boolean contains = Iterables.contains(list,"apple");
System.out.println(contains);
Iterable<String> filter = Iterables.filter(list,new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.length()==5;
}
});
System.out.println(Lists.newArrayList(filter));
String find = Iterables.find(list,new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("C");
}
},"NONE");
System.out.println(find);
int frequency = Iterables.frequency(Lists.newArrayList(1,2,1,1,2,3,1),1);
System.out.println(frequency);
String last = Iterables.getLast(list);
System.out.println(last);
int indexOf = Iterables.indexOf(list,new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("P");
}
});
System.out.println(indexOf);
boolean empty = Iterables.isEmpty(Lists.newArrayList());
System.out.println(empty);
Iterable<List<String>> partition = Iterables.paddedPartition(list,3);
System.out.println(partition.toString());
Iterable<List<String>> iterable = Iterables.partition(list,3);
System.out.println(iterable.toString());
boolean b = Iterables.removeIf(list,new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith("P");
}
});
System.out.println(b+"-->"+list.toString());
String[] array = Iterables.toArray(list,String.class);
System.out.println(array.length);
Iterable<Integer> transform = Iterables.transform(list,new Function<String,Integer>() {
@Override
public Integer apply(String input) {
return input.length();
}
});
System.out.println(transform.toString());
boolean retainAll = Iterables.retainAll(list,Lists.newArrayList("Apple","Banana","CD","Pear","Peach"));
System.out.println(retainAll+"-->"+list.toString());
Guava中的工具类Collections2的使用
List<Integer> list=Lists.newArrayList(1,2,6,8,9,3,4,5);
Collection<Integer> filter = Collections2.filter(list,new Predicate<Integer>() {
@Override
public boolean apply(Integer input) {
return input>=3;
}
});
System.out.println(filter.toString());
Collection<String> transform = Collections2.transform(list,new Function<Integer,String>() {
@Override
public String apply(Integer input) {
return (input+0.1)+"";
}
});
System.out.println(transform.toString());
List<String> fruits=Lists.newArrayList("Banana","Peach","Apple","Pear","Orange");
Collection<List<String>> permutations = Collections2.orderedPermutations(fruits);
System.out.println(permutations.toString());
Collection<List<String>> orderedPermutations = Collections2.orderedPermutations(fruits,new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
System.out.println(orderedPermutations);
Guava中的加强版集合的使用
HashMultiset<String> set = HashMultiset.create();
set.add("A");
set.add("B");
set.add("C");
set.add("A");
set.add("C");
set.add("D");
set.add("B");
set.add("A");
int size=set.entrySet().size();
System.out.println(size);
Set<String> elementSet = set.elementSet();
for(String str:elementSet){
System.out.println(str+"-->"+set.count(str));
}
for(Entry<String> entry:set.entrySet()){
System.out.println(entry.getElement()+"-->"+entry.getCount());
}
ArrayListMultimap<String,String> create = ArrayListMultimap.create();
create.put("k1","v1");
create.put("k2","v2");
create.put("k1","v11");
create.put("k2","v22");
create.put("k1","v111");
create.put("k3","v3");
int size=create.keySet().size();
System.out.println(size);
Set<String> set = create.keySet();
for(String key:set){
List<String> list = create.get(key);
System.out.println(list);
}
HashBiMap<String,String> map = HashBiMap.create();
map.put("星期一","Monday");
map.put("星期二","Tuesday");
map.put("星期三","Wednesday");
map.put("星期四","Thursday");
map.put("星期五","Friday");
map.put("星期六","Saturday");
map.put("星期日","Sunday");
System.out.println(map.get("星期日"));
System.out.println(map.inverse().get("Monday"));