我们如何比较两个字符串并获得重复值

How can we compare two Strings and get duplicates values

我创建了一个window,有一个按钮和3个字段,第1、2个字段是文本,在第3个字段中,需要写下在第1个和第2个字段中重复的单词。怎么做到的?

        JFrame frame = new JFrame("new Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        Label lbTextLabel = new Label("1:");
        lbTextLabel.setBounds(50, 10, 120, 20);
 
        Label lbTextLabel2 = new Label("2:");
        lbTextLabel2.setBounds(50, 50, 120, 20);
 
        Label lbTextLabel3 = new Label("3:");
        lbTextLabel3.setBounds(50, 90, 120, 20);
 
        JTextArea TextArea = new JTextArea("Welcome to javatpoint");
        TextArea.setBounds(50,30, 120,20);
 
        JTextArea TextArea2 = new JTextArea("Welcome to javatpoint 2");
        TextArea2.setBounds(50,70, 127,20);
 
        JTextArea TextArea3 = new JTextArea("");
        TextArea3.setBounds(50,110, 127,20);
 
        JButton button =new JButton("Click Here");
        button.setBounds(140,130,95,30);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
            }
        });

您必须拆分两个文本区域单词并检查第一个文本区域单词是否与第二个文本区域单词匹配,如果匹配则将其打印在第三个文本区域中。

下面是示例:

import java.awt.event.*;  
import javax.swing.*;    
import java.util.*;
class CompareWord{
    public static void main(String[] args) {
        JFrame f;
        JLabel lbTextLabelt1, lbTextLabel2, lbTextLabel3;
        JTextArea TextArea, TextArea2, TextArea3;
        JButton button;

        f = new JFrame("new Frame");
        lbTextLabelt1 = new JLabel("1:");
        lbTextLabelt1.setBounds(50, 10, 120, 20);

        lbTextLabel2 = new JLabel("2:");
        lbTextLabel2.setBounds(50, 70, 120, 20);

        lbTextLabel3 = new JLabel("3:");
        lbTextLabel3.setBounds(50, 140, 120, 20);

        TextArea = new JTextArea("Welcome to javatpoint");
        TextArea.setBounds(50,30, 120,30);

        TextArea2 = new JTextArea("Welcome to javatpoint");
        TextArea2.setBounds(50,90, 127,30);

        TextArea3 = new JTextArea("");
        TextArea3.setBounds(50,160, 127,30);

        button = new JButton("Click Here");
        button.setBounds(120,200,95,30);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                String str1 = TextArea.getText();  //   ;
                String str2 = TextArea2.getText(); //   
                String str3 = "";

                String arrStr1[] = str1.split("//s+");
                String arrStr2[] = str2.split("//s+");

                List<String> wordsOfFirstStr = Arrays.asList(str1.split(" "));

                for (String wordsOfSecondStr : str2.split(" ")) {
                    if(wordsOfFirstStr.contains(wordsOfSecondStr))
                    {
                        str3 += wordsOfSecondStr + " ";
                    }
                }
                TextArea3.setText(str3);

            }
        });        

        f.add(lbTextLabelt1);
        f.add(lbTextLabel2);
        f.add(lbTextLabel3);
        f.add(TextArea);
        f.add(TextArea2);
        f.add(TextArea3);
        f.add(button);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
    }
}

输出:

告诉我下面的代码是你想要的,我会添加解释。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Identify {
    private JTextArea  oneTextArea;
    private JTextArea  twoTextArea;
    private JTextArea  threeTextArea;

    private void createAndDisplayGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createMainPanel(), BorderLayout.CENTER);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        JButton button = new JButton("Click Me");
        button.setMnemonic(KeyEvent.VK_C);
        button.addActionListener(this::findCommonWords);
        buttonsPanel.add(button);
        return buttonsPanel;
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new GridLayout(0, 2, 10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        JLabel oneLabel = new JLabel("                                                  1:");
        oneLabel.setDisplayedMnemonic(KeyEvent.VK_1);
        mainPanel.add(oneLabel);
        oneTextArea = new JTextArea("Apple Mango Orange");
        mainPanel.add(oneTextArea);
        oneLabel.setLabelFor(oneTextArea);
        JLabel twoLabel = new JLabel("                                                  2:");
        twoLabel.setDisplayedMnemonic(KeyEvent.VK_2);
        mainPanel.add(twoLabel);
        twoTextArea = new JTextArea("Mango Orange Banana");
        mainPanel.add(twoTextArea);
        twoLabel.setLabelFor(twoTextArea);
        JLabel threeLabel = new JLabel("                                                  3:");
        threeLabel.setDisplayedMnemonic(KeyEvent.VK_3);
        mainPanel.add(threeLabel);
        threeTextArea = new JTextArea(1, 10);
        mainPanel.add(threeTextArea);
        threeLabel.setLabelFor(threeTextArea);
        return mainPanel;
    }

    private void findCommonWords(ActionEvent event) {
        String text1 = oneTextArea.getText();
        String text2 = twoTextArea.getText();
        String[] words1 = text1.split(" ");
        String[] words2 = text2.split(" ");
        List<String> list1;
        List<String> list2;
        if (words1.length > words2.length) {
            list1 = new ArrayList<>(Arrays.asList(words1));
            list2 = new ArrayList<>(Arrays.asList(words2));
        }
        else {
            list1 = new ArrayList<>(Arrays.asList(words2));
            list2 = new ArrayList<>(Arrays.asList(words1));
        }
        list1.retainAll(list2);
        threeTextArea.setText(list1.stream().collect(Collectors.joining(" ")));
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Identify().createAndDisplayGui());
    }
}

这是我 运行 它(在单击按钮之前)时的样子。