public class TantanitReader {
private int age;
private String loginName;
private String realName;
private String career;
public TantanitReader() {
}
public TantanitReader(int age, String loginName, String realName, String career) {
this.age = age;
this.loginName = loginName;
this.realName = realName;
this.career = career;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getCareer() {
return career;
}
public void setCareer(String career) {
this.career = career;
}
@Override
public String toString() {
return "age:"+this.getAge()+",loginName:"+this.loginName
+",realName:"+this.getRealName()+",career:"+this.getCareer();
}
}
public interface Predicate<T> {
boolean test(T t);
}
public class SelectService<T> {
public List<T> select(Collection<T> source, Predicate<T> predicate){
List result = new LinkedList();
for(T element:source){
if (predicate.test(element)) {
result.add(element);
}
}
return result;
}
}
public class TantanitReaderPredicateTest {
public static void main(String[] args) {
SelectService tantanitReaderSelectSerive
=new SelectService<TantanitReader>();
List<TantanitReader> source = new LinkedList<>();
source.add(new TantanitReader(10,"jack","张三","学生"));
source.add(new TantanitReader(18,"rose","李四","学生"));
source.add(new TantanitReader(19,"mike","王五","程序员"));
source.add(new TantanitReader(20,"jack","赵六","作家"));
List<TantanitReader> audultReaders
=tantanitReaderSelectSerive.select(source, new Predicate() {
@Override
public boolean test(Object o) {
TantanitReader tantanitReader=(TantanitReader)o;
return tantanitReader.getAge()>=18;
}
});
System.out.println("tantanit.com 成年读者名单如下:");
printTantanitReaders(audultReaders);
System.out.println("tantanit.com 十多岁(包含 10 岁)成员如下:");
List<TantanitReader> teenReaders
=tantanitReaderSelectSerive.select(source, new Predicate() {
@Override
public boolean test(Object o) {
TantanitReader tantanitReader=(TantanitReader)o;
return tantanitReader.getAge()>=10 && tantanitReader.getAge()<=19;
}
});
printTantanitReaders(teenReaders);
}
public static void printTantanitReaders(List<TantanitReader> tantanitReaders) {
for (TantanitReader tantanitReader : tantanitReaders) {
System.out.println(tantanitReader.toString());
}
}
}
tantanit.com 成员读者名单如下: age:18,loginName:rose,realName: 李四,career: 学生 age:19,loginName:mike,realName: 王五,career: 程序员 age:20,loginName:jack,realName: 赵六,career: 作家 tantanit.com 十多岁(包含10 岁)成员如下: age:10,loginName:jack,realName: 张三,career: 学生 age:18,loginName:rose,realName: 李四,career: 学生 age:19,loginName:mike,realName: 王五,career: 程序员
tantanitReader.getAge()>=18
tantanitReader.getAge()>=10 && tantanitReader.getAge()<=19
public interface Predicate<T> {
boolean test(T t);
}
@FunctionalInterface
public interface PredicateFunction<T> {
boolean test(T t);
}
public List<T> select(Collection<T> source, PredicateFunction<T> predicate){
List result = new LinkedList();
for(T element:source){
if (predicate.test(element)) {
result.add(element);
}
}
return result;
}
public class TantanitReaderPredicateFunctionTest {
public static void main(String[] args) {
SelectService tantanitReaderSelectSerive
=new SelectService<TantanitReader>();
List<TantanitReader> source = new LinkedList<>();
source.add(new TantanitReader(10,"jack","张三","学生"));
source.add(new TantanitReader(18,"rose","李四","学生"));
source.add(new TantanitReader(19,"mike","王五","程序员"));
source.add(new TantanitReader(20,"jack","赵六","作家"));
PredicateFunction<TantanitReader> predicateFunction
= (TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18;
List<TantanitReader> audultReaders
=tantanitReaderSelectSerive.select(source,predicateFunction);
System.out.println("tantanit.com 成员读者名单如下:");
printTantanitReaders(audultReaders);
System.out.println("tantanit.com 十多岁(包含 10 岁)成员如下:");
PredicateFunction<TantanitReader> predicateFunction2
= (TantanitReader tantanitReader)
-> tantanitReader.getAge()>=10 && tantanitReader.getAge()<=19;
List<TantanitReader> teenReaders
=tantanitReaderSelectSerive.select(source,predicateFunction2);
printTantanitReaders(teenReaders);
}
public static void printTantanitReaders(List<TantanitReader> tantanitReaders) {
for (TantanitReader tantanitReader : tantanitReaders) {
System.out.println(tantanitReader.toString());
}
}
}
PredicateFunction<TantanitReader> predicateFunction = (TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18; List<TantanitReader> audultReaders =tantanitReaderSelectSerive.select(source,predicateFunction);
List<TantanitReader> audultReaders =tantanitReaderSelectSerive.select( source,(TantanitReader tantanitReader) -> tantanitReader.getAge() >= 18 );
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有