用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

lambda表达式--过滤器

2015-12-20 作者: 永夜极光举报

[java]代码库

package s0403lambda表达式和过滤器;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Person {
	private String firstName, lastName, job, gender;  
	private int salary, age;  
	  
	public Person(String firstName, String lastName, String job, String gender, int age, int salary)       
	{  
	          this.firstName = firstName;  
	          this.lastName = lastName;  
	          this.gender = gender;  
	          this.age = age;  
	          this.job = job;  
	          this.salary = salary;  
	}  
	
	    static List<Person> javaProgrammers = new ArrayList<Person>() {  
		  {  
		    add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 43, 2000));  
		    add(new Person("Tamsen", "Brittany", "Java programmer", "female", 23, 1500));  
		    add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1800));  
		    add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600));  
		    add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200));  
		    add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900));  
		    add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300));  
		    add(new Person("Jayden", "Corrina", "Java programmer", "female", 35, 1700));  
		    add(new Person("Palmer", "Dene", "Java programmer", "male", 33, 2000));  
		    add(new Person("Addison", "Pam", "Java programmer", "female", 34, 1300));  
		  }  
		};  
		  
		static List<Person> phpProgrammers = new ArrayList<Person>() {  
		  {  
		    add(new Person("Jarrod", "Pace", "PHP programmer", "male", 34, 1550));  
		    add(new Person("Clarette", "Cicely", "PHP programmer", "female", 23, 1200));  
		    add(new Person("Victor", "Channing", "PHP programmer", "male", 32, 1600));  
		    add(new Person("Tori", "Sheryl", "PHP programmer", "female", 21, 1000));  
		    add(new Person("Osborne", "Shad", "PHP programmer", "male", 32, 1100));  
		    add(new Person("Rosalind", "Layla", "PHP programmer", "female", 25, 1300));  
		    add(new Person("Fraser", "Hewie", "PHP programmer", "male", 36, 1100));  
		    add(new Person("Quinn", "Tamara", "PHP programmer", "female", 21, 1000));  
		    add(new Person("Alvin", "Lance", "PHP programmer", "male", 38, 1600));  
		    add(new Person("Evonne", "Shari", "PHP programmer", "female", 40, 1800));  
		  }  
		}; 


       public static void main(String[] args){
    	   
    	     //给程序员加薪 5%  
    	   Consumer<Person> giveRaise = e -> e.setSalary(e.getSalary() / 100 * 5 + e.getSalary());      	     
    	   javaProgrammers.forEach(giveRaise);  
    	   phpProgrammers.forEach(giveRaise); 
    	   
    	   //过滤器,显示月薪超过1400美元的程序员的名字
    	   javaProgrammers.stream().filter(
    			   (p)->p.getSalary()>1400
    			   ).forEach( (p)->System.out.println(p.getFirstName()+" "+p.getLastName()+" "+p.getSalary()));
    	   
    	
    	   //使用limit方法,可以限制结果集的个数:
    	   System.out.println("使用limit方法,可以限制结果集的个数");
    	   javaProgrammers.stream()
    	   .filter((p)->p.getSalary()>1400)
    	   .limit(3)
    	   .forEach( (p)->System.out.println(p.getFirstName()+" "+p.getLastName()+" "+p.getSalary()));
    	   
    	   
       }
	
		public String getFirstName() {
			return firstName;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public String getLastName() {
			return lastName;
		}

		public void setLastName(String lastName) {
			this.lastName = lastName;
		}

		public String getJob() {
			return job;
		}

		public void setJob(String job) {
			this.job = job;
		}

		public String getGender() {
			return gender;
		}

		public void setGender(String gender) {
			this.gender = gender;
		}

		public int getSalary() {
			return salary;
		}

		public void setSalary(int salary) {
			this.salary = salary;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}  
		  
		
	  

}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...