小编NCN*_*ros的帖子

如何使用Java 8 Stream从某些类属性中获取List?

我有一个List<Person>.我需要List从一个属性获得Person.

例如,我有一个Person班级:

class Person
{
    private String name;
    private String birthDate;
    public String getName() {
        return name;
    }
    public String getBirthDate() {
        return birthDate; 
    }
    Person(String name) {
        this.name = name;
    }
}

List<Person> personList = new ArrayList<>();
personList.add(new Person("David"));
personList.add(new Person("Joe"));
personList.add(new Person("Michel"));
personList.add(new Person("Barak"));
Run Code Online (Sandbox Code Playgroud)

我想获得StreamAPI 的名称列表,如下所示:

List<String> names = personList.stream().somecode().collect(Collectors.toList());
names.stream().forEach(System.out::println);

#David
#Joe
#Michel
#Barak
Run Code Online (Sandbox Code Playgroud)

此代码不起作用:

public class Main 
{
    public static void main(String[] args) 
    {
        List<Person> personList …
Run Code Online (Sandbox Code Playgroud)

java collections java-8 java-stream

108
推荐指数
1
解决办法
12万
查看次数

ListView与JavaFX中的自定义内容

我如何使用JavaFx为我的应用程序制作自定义ListView.我需要每个行listView的HBox图像和2个标签.在此输入图像描述

java user-interface javafx javafx-8

6
推荐指数
1
解决办法
4730
查看次数

如何在Kotlin中的Java 8 Stream上调用collect(Collectors.toList())?

我有一些代码:

directoryChooser.title = "Select the directory"
val file = directoryChooser.showDialog(null)
if (file != null) {
    var files = Files.list(file.toPath())
            .filter { f ->
                f.fileName.endsWith("zip") && f.fileName.endsWith("ZIP")
                        && (f.fileName.startsWith("1207") || f.fileName.startsWith("4407") || f.fileName.startsWith("1507") || f.fileName.startsWith("9007") || f.fileName.startsWith("1807"))
            }
    for (f in files) {
        textArea.appendText(f.toString() + "\n")
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我collect(Collectors.toList())在过滤器结束时调用,我得到:

Error:(22, 13) Kotlin: [Internal Error] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: no descriptor for type constructor of ('Captured(in ('Path'..'Path?'))'..'CapturedTypeConstructor(in ('Path'..'Path?'))?')
Cause: no descriptor for type constructor of ('Captured(in ('Path'..'Path?'))'..'CapturedTypeConstructor(in ('Path'..'Path?'))?')
File …
Run Code Online (Sandbox Code Playgroud)

java java-8 kotlin java-stream

6
推荐指数
1
解决办法
3230
查看次数

如何设置优先级鼠标监听器

我有一个包含列表和按钮的面板.列表设置MouseAdaptermouseClick().我添加到面板MouseAdaptermousePressed()mouseReleased()MouseMotionAdaptermouseDragged.

拖放仅在单击面板时有效.

即使我点击列表,如何使拖动工作?

简单的考试:

public class DragTest extends JFrame{
private boolean drag;
private Point btnCoord;
private Point startPoint;

public DragTest(){
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(500,500);
    setLayout(null);
    final JPanel panel = new JPanel();
    final JButton button = new JButton();
    button.setText("Button");
    button.setSize(30,60);
    button.setLocation(50, 50);
    panel.setLayout(null);
    setContentPane(panel);
    panel.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            if (drag){
                panel.setLocation(btnCoord.x-(startPoint.x-e.getX()),btnCoord.y-(startPoint.y-e.getY()));
            }
        }
    });
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            drag …
Run Code Online (Sandbox Code Playgroud)

java swing drag-and-drop mouseevent jlist

1
推荐指数
1
解决办法
1704
查看次数