Java.stream()未保存列表<&燃气轮机;在.toList()中排序-打印空白

Java.stream()未保存列表<&燃气轮机;在.toList()中排序-打印空白,java,list,sorting,java-stream,tolist,Java,List,Sorting,Java Stream,Tolist,--我正在使用.stream()按用户最初选择的名称筛选列表并将其打印出来,但无法正确打印排序后的列表;它只打印空白。我认为它没有在列表中正确保存排序。我怎样才能解决这个问题 StudentFinder.java: import java.util.*; import java.util.stream.*; //throws error w/o it :( public class StudentFinder { //Print method

--我正在使用
.stream()
按用户最初选择的名称筛选列表并将其打印出来,但无法正确打印排序后的列表;它只打印空白。我认为它没有在列表中正确保存排序。我怎样才能解决这个问题

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

我不认为你的问题来自分类。。。下面的代码可能与没有内部类的代码非常相似,似乎可以正常工作。给我们更多关于代码其余部分的细节,我们可能会提供帮助

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 + " "); }
    }
}
import java.util.ArrayList;
导入java.util.Comparator;
导入java.util.List;
导入java.util.Scanner;
导入java.util.stream.collector;
公共类分类测试
{
公立静态班学生
{
字符串字符串;
公立学生(字符串s){this.String=s;}
公共字符串getName(){返回this.String;}
}
公共静态void main(字符串[]args)
{
List students=new ArrayList();
学生。添加(新学生(“AA”);
学生。添加(新学生(“AC”);
学生。添加(新学生(“AB”);
学生。添加(新学生(“BA”);
System.out.println(“所有学生的初始顺序:”);
for(Student:students){System.out.print(Student.getName()+“”);}
System.out.print(“\n点击您的字母:”);
扫描仪=新的扫描仪(System.in);
字符串字母=scanner.next();
scanner.close();
List studentNames=students.stream()
.filter(n->n.getName().toUpperCase().StartWith(letter.toUpperCase()))
.sorted(Comparator.comparing(Student::getName))
.map(学生::getName)
.collect(Collectors.toList());
System.out.println(“\n名称的新顺序以“+字母+”:”)开头;
对于(字符串名称:studentNames){System.out.print(name+“”);}
}
}
输出为:

所有学生的初始顺序: AA AC AB BA

选择你的信:a

以A开头的名称的新顺序: AA AC AB


在不知道
students
holdUserInput
是什么的情况下,我们无法重现这一点,因此我们无法给出错误的建议。请提供一个示例来说明您的问题。这是我的完整代码:而不是
。排序(Comparator.Comparating(Student::getName)).map(Student::getName)
,您可以使用
.map(Student::getName).sorted()
@Holger我刚试过,没用。太奇怪了。它打印两行空白,名字的数量以每个选定的字母开头,但它不打印名字…:(@Maaaatt我正在打印一个以选定的字母开头的名字列表,而不是字母本身:)编辑我的帖子,添加了扫描器+将姓名和字母大写以进行比较。如果你比较“a”和“a”,如果你不知道的话,它就不起作用了。@Maaatt是的,我注意到了,并添加了
.toUpperCase()
。谢谢。我的代码有
扫描器
。我只是编辑了帖子并添加了整个代码。
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 + " "); }
    }
}