Java .stream() 未在 .toList() 中保存 List<> 排序 - 打印空白
Java .stream() not saving List<> sorting in .toList() - prints blank
-- 我正在使用 .stream()
按用户最初选择的名称过滤列表并将其打印出来,但我无法正确打印排序后的列表;它只打印白色 space。我认为它没有在列表中正确保存排序。我怎样才能解决这个问题?
StudentFinder.java:
import java.util.*;
import java.util.stream.*; //throws error w/o it :(
public class StudentFinder {
//Print method
public static void contactPrint(List<Student> arr) {
for(int x = 0; x < arr.size(); ++x) {
System.out.println("[" + x + "]" + " Name: " + arr.get(x).name + " - ID: " + arr.get(x).id + " - GPA: " + arr.get(x).gpa);
}// end for loop
}// end contactPrint()
public static void main(String[] args) {
// "File" to hold Student's field 'name' info
String[] names = {
"Albert Einstein",
"Ada Lovelace",
"Blaise Pascal",
"Bruna Louise",
"Caroline Herschel",
"Cecilia Payne-Gaposchkin",
"Dorothy Hodgkin",
"Douglas Junior",
"Edwin Powell Hubble",
"Elizabeth Blackburn",
"Flossie Wong-Staal",
"Frieda Robscheit-Robbins",
"Geraldine Seydoux",
"Gertrude B. Elion",
"Ingrid Daubechies",
"Irma Sanchez",
"Jacqueline K. Barton",
"Johannes Kepler",
"Lene Vestergaard Hau",
"Lord Kelvin",
"Maria Mitchell",
"Max Planck",
"Nicolaus Copernicus",
"Niels Bohr",
"Patricia S. Goldman-Rakic",
"Patty Jo Watson",
"Richard Phillips Feynman",
"Rita Levi-Montalcini",
"Sarah Boysen",
"Stephen Hawking",
"Werner Karl Heisenberg",
"Wilhelm Conrad Roentgen"
};
int numStudents = names.length;
String holdUserInput;
//Create new Student object students
List<Student> students = new ArrayList<Student>();
// Initialize List of objects Student students with **CONSTRUCTOR**
for(int x = 0; x < numStudents; ++x) {
students.add(new Student(names[x], (100 + x), (double)((2.2 + (x + 2))))); // constructor
}
//Prints original object students
contactPrint(students);
//prompt user for input
System.out.println("Enter the first letter of the student's name:");
Scanner userInput = new Scanner(System.in);
holdUserInput = userInput.nextLine();//<<<******INT??? String???********
//handle user's input
List<String> studentsNames =
students.stream()
.filter(n -> n.getName().startsWith(holdUserInput.toUpperCase()))
.sorted(Comparator.comparing(Student::getName))
.map(Student::getName)
.collect(Collectors.toList());
//print sorted list
System.out.println(" \n");
for(int i = 0; i < studentsNames.size(); i++) {
System.out.println(studentsNames.get(i));
}
//end program message to user
System.out.println("Program Ended.");
}// end main()
}// end class StudentFinder
Student.java:
public class Student {
String name = "";
int id;
double gpa;
// constructor
public Student(String sName, int sId, double sGpa) {
name = sName;
id = sId;
gpa = sGpa;
}
//get name value to main()
public String getName() {
return name;
}
//get ID value to main()
public int getId() {
return id;
}
//get GPA value to main()
public double getGpa() {
return gpa;
}
}
我不认为你的问题来自于排序...下面的代码可能与你没有内部 class 的代码非常相似似乎有效。向我们提供有关其余代码的更多详细信息,我们可能会提供帮助。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class SortTest
{
public static class Student
{
String string;
public Student(String s) { this.string = s; }
public String getName() { return this.string; }
}
public static void main(String[] args)
{
List<Student> students = new ArrayList<Student>();
students.add(new Student("AA"));
students.add(new Student("AC"));
students.add(new Student("AB"));
students.add(new Student("BA"));
System.out.println("Initial Order of all students: ");
for(Student student : students) { System.out.print(student.getName() + " "); }
System.out.print("\nPick your letter: ");
Scanner scanner = new Scanner(System.in);
String letter = scanner.next();
scanner.close();
List<String> studentNames = students.stream()
.filter(n -> n.getName().toUpperCase().startsWith(letter.toUpperCase()))
.sorted(Comparator.comparing(Student::getName))
.map(Student::getName)
.collect(Collectors.toList());
System.out.println("\nNew order of names starting by " + letter + ":");
for(String name : studentNames) { System.out.print(name + " "); }
}
}
输出为:
Initial order of all students:
AA AC AB BA
Pick your letter: a
New order of names starting by A:
AA AC AB
-- 我正在使用 .stream()
按用户最初选择的名称过滤列表并将其打印出来,但我无法正确打印排序后的列表;它只打印白色 space。我认为它没有在列表中正确保存排序。我怎样才能解决这个问题?
StudentFinder.java:
import java.util.*;
import java.util.stream.*; //throws error w/o it :(
public class StudentFinder {
//Print method
public static void contactPrint(List<Student> arr) {
for(int x = 0; x < arr.size(); ++x) {
System.out.println("[" + x + "]" + " Name: " + arr.get(x).name + " - ID: " + arr.get(x).id + " - GPA: " + arr.get(x).gpa);
}// end for loop
}// end contactPrint()
public static void main(String[] args) {
// "File" to hold Student's field 'name' info
String[] names = {
"Albert Einstein",
"Ada Lovelace",
"Blaise Pascal",
"Bruna Louise",
"Caroline Herschel",
"Cecilia Payne-Gaposchkin",
"Dorothy Hodgkin",
"Douglas Junior",
"Edwin Powell Hubble",
"Elizabeth Blackburn",
"Flossie Wong-Staal",
"Frieda Robscheit-Robbins",
"Geraldine Seydoux",
"Gertrude B. Elion",
"Ingrid Daubechies",
"Irma Sanchez",
"Jacqueline K. Barton",
"Johannes Kepler",
"Lene Vestergaard Hau",
"Lord Kelvin",
"Maria Mitchell",
"Max Planck",
"Nicolaus Copernicus",
"Niels Bohr",
"Patricia S. Goldman-Rakic",
"Patty Jo Watson",
"Richard Phillips Feynman",
"Rita Levi-Montalcini",
"Sarah Boysen",
"Stephen Hawking",
"Werner Karl Heisenberg",
"Wilhelm Conrad Roentgen"
};
int numStudents = names.length;
String holdUserInput;
//Create new Student object students
List<Student> students = new ArrayList<Student>();
// Initialize List of objects Student students with **CONSTRUCTOR**
for(int x = 0; x < numStudents; ++x) {
students.add(new Student(names[x], (100 + x), (double)((2.2 + (x + 2))))); // constructor
}
//Prints original object students
contactPrint(students);
//prompt user for input
System.out.println("Enter the first letter of the student's name:");
Scanner userInput = new Scanner(System.in);
holdUserInput = userInput.nextLine();//<<<******INT??? String???********
//handle user's input
List<String> studentsNames =
students.stream()
.filter(n -> n.getName().startsWith(holdUserInput.toUpperCase()))
.sorted(Comparator.comparing(Student::getName))
.map(Student::getName)
.collect(Collectors.toList());
//print sorted list
System.out.println(" \n");
for(int i = 0; i < studentsNames.size(); i++) {
System.out.println(studentsNames.get(i));
}
//end program message to user
System.out.println("Program Ended.");
}// end main()
}// end class StudentFinder
Student.java:
public class Student {
String name = "";
int id;
double gpa;
// constructor
public Student(String sName, int sId, double sGpa) {
name = sName;
id = sId;
gpa = sGpa;
}
//get name value to main()
public String getName() {
return name;
}
//get ID value to main()
public int getId() {
return id;
}
//get GPA value to main()
public double getGpa() {
return gpa;
}
}
我不认为你的问题来自于排序...下面的代码可能与你没有内部 class 的代码非常相似似乎有效。向我们提供有关其余代码的更多详细信息,我们可能会提供帮助。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class SortTest
{
public static class Student
{
String string;
public Student(String s) { this.string = s; }
public String getName() { return this.string; }
}
public static void main(String[] args)
{
List<Student> students = new ArrayList<Student>();
students.add(new Student("AA"));
students.add(new Student("AC"));
students.add(new Student("AB"));
students.add(new Student("BA"));
System.out.println("Initial Order of all students: ");
for(Student student : students) { System.out.print(student.getName() + " "); }
System.out.print("\nPick your letter: ");
Scanner scanner = new Scanner(System.in);
String letter = scanner.next();
scanner.close();
List<String> studentNames = students.stream()
.filter(n -> n.getName().toUpperCase().startsWith(letter.toUpperCase()))
.sorted(Comparator.comparing(Student::getName))
.map(Student::getName)
.collect(Collectors.toList());
System.out.println("\nNew order of names starting by " + letter + ":");
for(String name : studentNames) { System.out.print(name + " "); }
}
}
输出为:
Initial order of all students: AA AC AB BA
Pick your letter: a
New order of names starting by A: AA AC AB