Eclipse搜索代码,用于搜索部分字符串而不是全部字符串

问题描述 投票:0回答:1

我在Eclipse上有一个代码可以搜索一个字符串,有没有一种方法可以对此进行调整,而不是搜索整个字符串,您只能输入部分字符串,它仍然会返回值?另外,是否有一种方法可以调整代码以使其搜索多个值?即我想一次搜索两个城市?

package Main_Frame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


/**
 * MainFrame.java
 * This is the main user interface of the application.
 * @author www.codejava.net
 */
public class MainFrame extends JFrame {

    protected JButton buttonAdd = new JButton("Add New Person");
    protected JButton buttonSearch = new JButton("Search Persons");
    protected  JButton buttonSearch2 = new JButton("Search Tags");
    protected JButton buttonSort = new JButton("Sort Persons");

     JList<Person> listPerson = new JList<>();
     JList<Group> listGroup = new JList<>();
    protected CustomListModel<Person> listModel;
    protected java.util.List<Person> persons = new ArrayList<>();
    protected CustomListModel2<Group> listModel1;
    protected java.util.List<Group> Group = new ArrayList<>();




    public MainFrame() {
        super("Swing Search & Sort Example");

        initComponents();

        setSize(600, 480);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    protected void initComponents() {
        JPanel panelButton = new JPanel();
        panelButton.setBounds(26, 10, 547, 35);
        panelButton.setLayout(new FlowLayout(FlowLayout.CENTER));

        buttonAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                addPerson();
            }
        });

        buttonSort.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                sortPersons();
            }
        });

        buttonSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                searchPersons();
            }
        });

        panelButton.add(buttonAdd);
        panelButton.add(buttonSearch);
        buttonSearch2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                 searchGroup();
            }
        });
        getContentPane().setLayout(null);
        panelButton.add(buttonSearch2);
        panelButton.add(buttonSort);

        getContentPane().add(panelButton);
        listPerson.setBounds(46, 57, 200, 360);
        listGroup.setBounds(46, 57, 200, 360);
        listPerson.setPreferredSize(new Dimension(200, 360));
        listGroup.setPreferredSize(new Dimension(200, 360));
        listModel = new CustomListModel<Person>(persons);
        listModel1 = new CustomListModel2<Group>(Group);

        listPerson.setModel(listModel);
        listGroup.setModel(listModel1);


        listModel.addElement(new Person("Detroit"));
        listModel.addElement(new Person("Traverse City "));
        listModel.addElement(new Person("Kansas City"));
        listModel.addElement(new Person("Tampa"));
        listModel.addElement(new Person("Orlando"));
        listModel.addElement(new Person("Las Vegas"));
        listModel.addElement(new Person("Pheonix"));
        listModel1.addElement(new Group("Car"));


        getContentPane().add(listPerson);
        listGroup.setPreferredSize(new Dimension(200, 360));
        listGroup.setBounds(333, 57, 200, 360);

        getContentPane().add(listGroup);

    }

    private void addPerson() {
        String personName = JOptionPane.showInputDialog(this, "Enter person name");
        if (personName != null) {
            listModel.addElement(new Person(personName));
        }
    }

    private void sortPersons() {
        Collections.sort(persons);
        listModel.fireDataChanged();
    }

    private void searchPersons() {
        String personName = JOptionPane.showInputDialog(this, "Enter person name to search for:");

        if (personName == null) {
            return;
        }

        Collections.sort(persons);

        int foundIndex = Collections.binarySearch(persons, new Person(personName));

        if (foundIndex >= 0) {
            listPerson.setSelectedIndex(foundIndex);
        } else {
            JOptionPane.showMessageDialog(this, "Could not find the person " + personName);
        }
    }
    private void searchGroup() {
        String GroupName = JOptionPane.showInputDialog(this, "Enter tag to search for:");

        if (GroupName == null) {
            return;
        }

        Collections.sort(Group);

        int foundIndex = Collections.binarySearch(Group, new Group(GroupName));

        if (foundIndex >= 0) {

            listGroup.setSelectedIndex(foundIndex);
        } else {
            JOptionPane.showMessageDialog(this, "Could not find the tag " + GroupName);
        }
    }
}
java eclipse search
1个回答
0
投票

这是我实施解决方案的方式

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.Collectors;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{


public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customer(1, "Jack"));
        customers.add(new Customer(2, "James"));
        customers.add(new Customer(3, "Kelly"));

        List<Customer> findUsers = customers.stream()
                                  .filter(customer -> (customer.getName()).contains("Ja"))
                                  //.findAny()
                                  .collect(Collectors.toList());
//                                .orElse(null); 
        System.out.print("Check 1:-" +findUsers);

        findUsers = customers.stream()
                                  .filter(customer -> (customer.getName()).contains("M"))
                                  //.findAny()
                                  .collect(Collectors.toList());
//                                .orElse(null); 
        System.out.print("\nCheck 2:-" +findUsers);
    }
}
class Customer {

    private int id;
    private String name;

    public Customer(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Customer other = (Customer) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

转到https://www.baeldung.com/find-list-element-java了解有关在列表中查找元素的更多详细信息

© www.soinside.com 2019 - 2024. All rights reserved.