function is used for synchronous transformations.
predicate is used for filtering.
Import java.util.Collection;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import com.google.common.base.Function;
Import com.google.common.base.Functions;
Import com.google.common.base.Objects;
Import Com.google.common.base.Predicate;
Import Com.google.common.collect.Collections2;
Import com.google.common.collect.Iterators;
Import com.google.common.collect.Lists;
Import Com.google.common.collect.Maps;
Public class User {
private String username;
private String sex;
Public User (string username, string sex) {
This . Username = username;
This . sex = sex;
}
void Setusername (String username) {
This . Username = username;
}
void Setsex (String sex) {
This . sex = sex;
}
String GetUserName () {
return username;
}
String Getsex () {
return sex;
}
@Override
Public String toString () {
return Objects.tostringhelper (this). Omitnullvalues (). Add ("username", username). Add ("Sex" , Sex). ToString ();
}
public static void Main (string[] args) {
user u1 = New User ("Malone", "man");
user U2 = New User ("Lanlan", "woman");
list<user> List = lists.newarraylist (u1, U2);
//Function here for type conversion
collection<string> result = Collections2.transform (list, new Function<user, string> ( ) {
@Override
Public String apply (user user) {
if (objects.equal (user, null)) {
return "";
}
return user.tostring ();
}
});
For (String SS: result) {
System. OUT.PRINTLN (ss);
}
//predicate here is for filtering
collection<user> resultuser = collections2.filter (list, new predicate<user> () {
@Override
Public boolean apply (user user) {
if (objects.equal (user, null)) {
return false;
}
if (objects.equal (user.getsex (), "Woman")) {
return false;
}
return true;
}
});
For (User SS: resultuser) {
System. OUT.PRINTLN (ss);
}
Function<user, string> f1 = new Function<user, string> () {
@Override
Public String apply (user user) {
if (objects.equal (user, null)) {
return "";
}
return user.getusername ();
}
};
//Multiple tool classes can be programmed using class-function
collection<string> result1 = collections2.transform (list, F1);
For (String SS: result1) {
System. Out.println ("1:" + ss);
}
list<string> strlist = Lists.transform (List, F1);
For (String SS: strlist) {
System. Out.println ("2:" + ss);
}
iterator<string> it = iterators.transform (list.iterator (), F1);
While (it.hasnext ()) {
System. Out.println ("3:" + It.next ());
}
function<string, string> f2 = new function<string, string> () {
@Override
Public string Apply (string input) {
if (objects.equal (input, "Malone")) {
return "Malone s function";
} else {
return "Lanlan s function";
}
}
};
//Connect multiple function
Warning: Here the parameter position of the compose method cannot be reversed,
* Function<a, c> compose (Function<b, c> G, Function<a,? extends b> f)
* Incoming function<b,c>, function<a,? Extends b> group synthesis Function<a, c>
Function<user, string> f3 = Functions.compose (f2, F1);
list<string> mylist = Lists.transform (List, F3);
System. out.println (mylist.get (0));
map<string, string> Map = Maps.newhashmap ();
map.put ("1", "2");
//functions can convert a map, make the code more straightforward, and provide default value support
function<string, string> lookup = functions.formap (map, "default value");
System. out.println (lookup.apply ("1"));
}
}
Guava Learning--function, predicate