一.集合中存储了一些字符串的数据,比如:A,18
将它们收集到Student类型的数组当中(使用方法引用完成)
Student:
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String stream){
this.name=stream.split(",")[0];
this.age=Integer.parseInt(stream.split(",")[1]);
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class FunctionReference {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<>();
Collections.addAll(al, "A,18", "B,19", "C,20", "D,21", "E,22");
Student[] arr = al.stream().map(Student::new).toArray(Student[]::new);
System.out.println(Arrays.toString(arr));
}
}
二.创建集合添加学生对象,学生对象属性:String name,int age
只获取姓名并放到数组当中(使用方法引用完成)
Student:
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
public class FunctionReference {
public static void main(String[] args) {
ArrayList<Student> al = new ArrayList<>();
al.add(new Student("A", 18));
al.add(new Student("B", 19));
al.add(new Student("C", 20));
al.add(new Student("D", 21));
al.add(new Student("E", 22));
/*没有进行引用
String[] arr = al.stream().map(new Function<Student, String>() {
@Override
public String apply(Student stu) {
return stu.getName();
}
}).toArray(String[]::new);
System.out.println(Arrays.toString(arr));
*/
String[] arr = al.stream().map(Student::getName).toArray(String[]::new);
System.out.println(Arrays.toString(arr));
}
}
三.创建集合添加学生对象,学生对象属性:String name,int age
把姓名和年龄拼接成:A-18的字符串,并放到数组当中(使用方法引用完成)
Student:
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String get(){
return name+"-"+age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
public class FunctionReference {
public static void main(String[] args) {
ArrayList<Student> al = new ArrayList<>();
al.add(new Student("A", 18));
al.add(new Student("B", 19));
al.add(new Student("C", 20));
al.add(new Student("D", 21));
al.add(new Student("E", 22));
/*没有进行引用
String[] arr = al.stream().map(new Function<Student, String>() {
@Override
public String apply(Student stu) {
return stu.getName() + "-" + stu.getAge();
}
}).toArray(String[]::new);
System.out.println(Arrays.toString(arr));
*/
String[] arr = al.stream().map(Student::get).toArray(String[]::new);
System.out.println(Arrays.toString(arr));
}
}