import java.util.Iterator; import java.util.List; import java.util.Map; import com.google.common.base.Joiner; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; /** * Guava tool class * * @author *@time Sep 5, 2017 9:39:22 AM */ public class GuavaUtils { private GuavaUtils() { } /** * Separate the corresponding strings with a delimiter * * @param separator * =>| * @param objects * =>1,2,3 * @return 1|2|3 */ public static String joinString(String separator, Object... objects) { Joiner joiner1 = Joiner.on(separator).skipNulls(); return joiner1.appendTo(new StringBuilder(""), objects).toString(); } /** * Convert map to string * * @param map * @return */ @SuppressWarnings("all") public static String mapToString(Map map) { return Joiner.on(";").withKeyValueSeparator("=").join(map); } /** * Convert string to List collection * * @param separator * @param str * @return */ public static List<String> getListByStr(String separator, String str) { Splitter sp = Splitter.on(separator).trimResults(); return sp.splitToList(str); } /** * Check if the element exists in the collection * @param list * @param element * @return */ public static boolean isListContainsObject(List<? extends Object> list,Object element){ return Iterables.contains(list, element); } public static void main(String[] args) throws Exception { System.out.println(GuavaUtils.joinString("|", "1", "2", "3")); Map<String, String> testMap = Maps.newLinkedHashMap(); testMap.put("Cookies", "12332"); testMap.put("Content-Length", "30000"); testMap.put("Date", "2016.12.16"); testMap.put("Mime", "text/html"); System.out.println(mapToString(testMap)); // The delimiter is |, and remove the leading and trailing blanks of the obtained element String str = "hello | world | your | Name "; List<String> ss = getListByStr("|", str); for (String it : ss) { System.out.println(it); } // check if empty System.out.println(Strings.isNullOrEmpty("")); // Preconditions.checkArgument(false, "You are wrong"); Person person1 = new Person(30); Person person2 = new Person(32); Person person3 = new Person(33); Person person4 = new Person(34); List<Person> personList = Lists.newArrayList(person1, person2, person3,person4); // Filter persons whose age is greater than or equal to 32 Iterable<Person> personsFilteredByAge = FluentIterable.from(personList).filter(new Predicate<Person>() { @Override public boolean apply(Person input) { return input.getAge() > 31; } }); // Iterable has an iterator method, and collection classes have an Iterator method for (Iterator<Person> it = personsFilteredByAge.iterator(); it.hasNext();) { System.out.println(it.next()); } System.out.println(Iterables.contains(personsFilteredByAge, person2)); System.out.println(isListContainsObject(personList, person3)); } } class Person { private int age; public Person(int age) { super(); this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Java Guava toolset learning (powerful)
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326989326&siteId=291194637
Recommended
Ranking