import com.google.common.collect.Collections2; import com.google.common.base.Predicate; List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.add("David"); Predicate<String> filter = new Predicate<String>() { @Override public boolean apply(String name) { return name.startsWith("A"); } }; Collection<String> filteredNames = Collections2.filter(names, filter); import com.google.common.collect.Maps; import com.google.common.base.Predicate; Map<Integer, String> users = new HashMap<>(); users.put(1, "Alice"); users.put(2, "Bob"); users.put(3, "Charlie"); users.put(4, "David"); Predicate<Map.Entry<Integer, String>> filter = new Predicate<Map.Entry<Integer, String>>() { @Override public boolean apply(Map.Entry<Integer, String> entry) { return entry.getValue().startsWith("A"); } }; Map<Integer, String> filteredUsers = Maps.filterEntries(users, filter); import com.google.common.collect.Collections2; import com.google.common.base.Function; List<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); names.add("David"); Function<String, String> transform = new Function<String, String>() { @Override public String apply(String name) { return name.toUpperCase(); } }; Collection<String> transformedNames = Collections2.transform(names, transform); import com.google.common.collect.Maps; import com.google.common.base.Function; Map<Integer, String> users = new HashMap<>(); users.put(1, "Alice"); users.put(2, "Bob"); users.put(3, "Charlie"); users.put(4, "David"); Function<String, String> transform = new Function<String, String>() { @Override public String apply(String name) { return name.toUpperCase(); } }; Map<Integer, String> transformedUsers = Maps.transformValues(users, transform);


上一篇:
下一篇:
切换中文