中山网站建设,招聘seo专员,深圳哪做网站,最近出入上海最新规定/*以上就是我们采用map方法new Function(有两个形参 一个传入一个传出)apply方法接受参数并且返回一个Student类型 最后封装成List集合再用Iterator迭代器进行遍历的一个操作流程 但是我们不禁思考这样做真的便捷吗 我们尝试把map内的代码放到JavaBean Student中去 再考虑问题*…/*以上就是我们采用map方法new Function(有两个形参 一个传入一个传出)apply方法接受参数并且返回一个Student类型 最后封装成List集合再用Iterator迭代器进行遍历的一个操作流程 但是我们不禁思考这样做真的便捷吗 我们尝试把map内的代码放到JavaBean Student中去 再考虑问题*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;public class Main{public static void main(String[] args){//创建集合对象ArrayListString list new ArrayList();//添加数据Collections.addAll(list,张无忌,15,周芷若,14,赵敏,13,张强,12,张三丰,100,张翠山,40,张良,35,王二麻子,37);//封装成Student对象并且收集到List中去//我的需求实现String-Student的转化并且储存在List集合中ListStudent l list.stream().map(new FunctionString, Student() {Overridepublic Student apply(String s){String[] arr s.split(,);String name arr[0];int age Integer.parseInt(arr[1]);return new Student(name,age);}}).collect(Collectors.toList());Iterator it l.iterator();while(it.hasNext()){System.out.println(it.next());}/*以上就是我们采用map方法new Function(有两个形参 一个传入一个传出)apply方法接受参数并且返回一个Student类型 最后封装成List集合再用Iterator迭代器进行遍历的一个操作流程 但是我们不禁思考这样做真的便捷吗 我们尝试把map内的代码放到JavaBean Student中去 再考虑问题*/System.out.println(——————————————————————————————);ListStudent ll list.stream().map(Student::new).collect(Collectors.toList());Iterator it2 ll.iterator();while(it2.hasNext()){System.out.println(it2.next());}}
}
public class Student
{String name;int age;public Student() {}public Student (String s){String[] arr s.split(,);this.name arr[0];this.age Integer.parseInt(arr[1]);}public Student(String name, int age) {this.name name;this.age age;}/*** 获取* return name*/public String getName() {return name;}/*** 设置* param name*/public void setName(String name) {this.name name;}/*** 获取* return age*/public int getAge() {return age;}/*** 设置* param age*/public void setAge(int age) {this.age age;}public String toString() {return Student{name name , age age };}}