/**
* 員工對象
*/
public class Employ {
private String name;
private Integer age;
private Double salary;
public Employ() {
}
public Employ(String name, Integer age, Double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employ{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
/**
* 過濾接口
*/
public interface MyPredict<T> {
boolean test(T t);
}
/**
* 不同種實現(xiàn)策略
*/
public class FileEmployByAge implements MyPredict<Employ> {
@Override
public boolean test(Employ employ) {
return employ.getAge() >= 35;
}
}
public class Test {
private static List<Employ> list = Arrays.asList(new Employ("張三",18,99999.9),
new Employ("李四",18,3439.9),
new Employ("王五",36,6666.9),
new Employ("趙六",41,7777.9),
new Employ("田七",9,2222.9));
public static void main(String[] args) {
// 獲取當(dāng)前公司中員工中年齡大于35的員工信息
// List<Employ> employs = filterEmploys(list);
// for (Employ employ:employs) {
// System.out.println(employ);
// }
// 獲取當(dāng)前公司中員工中工資大于5000的工資信息
// List<Employ> employs1 = filterEmploys2(list);
// for (Employ employ:employs1) {
// System.out.println(employ);
// }
// 優(yōu)化方式一 策略模式
// List<Employ> employs = fileEmploy(list, new FileEmployByAge());
// for (Employ employ:employs) {
// System.out.println(employ);
// }
// 優(yōu)化方式二 匿名內(nèi)部類
// List<Employ> employs = fileEmploy(list, new MyPredict<Employ>() {
// @Override
// public boolean test(Employ employ) {
// return employ.getSalary() < 5000;
// }
// });
// for (Employ employ:employs) {
// System.out.println(employ);
// }
// 優(yōu)化方式三 Lambda表達(dá)式
// List<Employ> employs = fileEmploy(list, t -> t.getAge() > 35);
// employs.forEach(System.out::println);
// 優(yōu)化方式四 Stream API
list.stream()
.filter(t->t.getAge()>35)
.limit(1)
.forEach(System.out::println);
list.stream()
.map(Employ::getName)
.forEach(System.out::println);
}
// 優(yōu)化方式一
public static List<Employ> fileEmploy(List<Employ> list,MyPredict<Employ> myPredict){
List<Employ> list1 = new ArrayList<>();
for (Employ employ:list) {
if (myPredict.test(employ)){
list1.add(employ);
}
}
return list1;
}
// 獲取當(dāng)前公司中員工中工資大于5000的工資信息
public static List<Employ> filterEmploys2(List<Employ> list){
List<Employ> list1 = new ArrayList<>();
for (Employ employ:list) {
if (employ.getSalary() >= 5000){
list1.add(employ);
}
}
return list1;
}
// 獲取當(dāng)前公司中員工中年齡大于35的員工信息
public static List<Employ> filterEmploys(List<Employ> list){
List<Employ> list1 = new ArrayList<>();
for (Employ employ:list) {
if (employ.getAge() >= 35){
list1.add(employ);
}
}
return list1;
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。