根據某個欄位篩選list資料
阿新 • • 發佈:2021-07-11
//測試類
package test; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; public class TestLambda { @Test public void test() { List<Person>list=new ArrayList<Person>(); list.add(new Person("張三",20,500)); list.add(new Person("李四",40,600)); list.add(new Person("趙四",35,500)); list.add(new Person("王五",36,500)); list.add(new Person("王七",30,600)); list.add(new Person("旺旺",31,700)); list.add(new Person("倪萍",35,500)); List<Person>personList=getPersonList(list,new GetInfoByAge()); System.out.println(list.toString()); System.out.println("personList=========="+personList); personList.forEach(person->System.out.println("結果==="+person)); } public List<Person>getPersonList(List<Person>list,GetInfoByAge info){ List<Person>personList=new ArrayList<Person>(); for(Person person:list) { if(info.getInfo(person)){ personList.add(person); } } return personList; } }
介面
package test; public interface MyPrivate<T> { public boolean getInfo(T t); }
實現類
package test; public class GetInfoByAge implements MyPrivate<Person>{ @Override public boolean getInfo(Person p) { return p.getAge()>=35; } }