public class Person implements Comparable{
public int Age;
public String Name;
public boolean Sex;
public Person(int age, String name, boolean sex) {
super();
Age = age;
Name = name;
Sex = sex;
}
@Override
public int compareTo(Person other) {
int result=pareTo(other.Name);
return result;
}
public class LinqTest {
@Test
public void name() {
Person p1=new Person(21,"21",true);
Person p2=new Person(22,"22",false);
Person p4=new Person(23,"24",true);
Person p3=new Person(23,"23",true);
List list=new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
List nameList= Linq4j.asEnumerable(list).where(new Predicate1() {
public boolean apply(Person arg0) {
//只要sex为true的
return arg0.Sex;
}
}).select(new Function1() {
public String apply(Person arg0) {
//只要Person的name字段,
return arg0.Name;
}
}).orderByDescending(new Function1() {
public String apply(String arg0) {
//降序排列
return arg0;
}
}).toList();
assertEquals(3, nameList.size());
assertEquals("24", nameList.get(0));
assertEquals("23", nameList.get(1));
assertEquals("21", nameList.get(2));
List list2= Linq4j.asEnumerable(list).where(new Predicate1() {
public boolean apply(Person arg0) {
//只要sex为true的
return arg0.Sex;
}
}).select(new Function1() {
public Person apply(Person arg) {
return arg;
}
}).orderByDescending(new Function1() {
public Person apply(Person arg) {
//降序排列
return arg;
}
}).toList();
assertEquals(3, list2.size());
assertEquals(p4, list2.get(0));
assertEquals(p3, list2.get(1));
assertEquals(p1, list2.get(2));
}
}
也可以参考:外链网址已屏蔽