0

我正在尝试比较两个树形图并比较关键字,然后对于两个地图的共同点,我想将这些值加在一起,然后将其输出到文本区域

到目前为止,我有两个可以成功工作的树图,但我不知道如何将它们与其他树进行比较。

到目前为止,这是我的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


public class AnalysisFrame extends JFrame
{
private static final int FRAME_WIDTH = 370;
private static final int FRAME_HEIGHT = 700;

JPanel displayPnl, filePnl, fileOnePnl, fileTwoPnl, textPnl, controlPnl, buttonPnl, infoPnl;
JTextArea resultTA;
JButton quitBtn, oneBtn, twoBtn, goBtn;
JScrollPane resultSP;
Border blackline, empty;
Label infoLbl;
File fileOne, fileTwo, finalFile;

public AnalysisFrame()
{
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
    displayPnl = new JPanel();

    createFilePanel();
    displayPnl.add(filePnl);

    createTextPanel();
    displayPnl.add(textPnl);

    createControlPanel();
    displayPnl.add(controlPnl);

    add(filePnl, BorderLayout.NORTH);
    add(textPnl, BorderLayout.CENTER);
    add(controlPnl, BorderLayout.SOUTH);

    this.setTitle("Steven Qualls - CP3 Final");
}

private void createFilePanel() {
    filePnl = new JPanel();

    createFileOnePanel();
    filePnl.add(fileOnePnl);

    createFileTwoPanel();
    filePnl.add(fileTwoPnl);



}

            public void createFileOnePanel() {
                fileOnePnl = new JPanel();

                TitledBorder fileOneB;
                blackline = BorderFactory.createLineBorder(Color.black);
                fileOneB = BorderFactory.createTitledBorder(blackline, "File 1");
                fileOneB.setTitleJustification(TitledBorder.CENTER);
                fileOnePnl.setBorder(fileOneB);

                oneBtn = new JButton("Choose File");

                fileOnePnl.add(oneBtn);
                oneBtn.setPreferredSize(new Dimension (100,30));

                        class OneButtonListener implements ActionListener
                        {
                            @Override
                            public void actionPerformed(ActionEvent evt)
                            {
                                final JFileChooser oneFC = new JFileChooser();
                                oneFC.showOpenDialog(AnalysisFrame.this);
                                String newLine = null;
                                oneFC.getName(null);
                                int returnVal = 0;
                                File fileOne = oneFC.getSelectedFile();

    Scanner input = null;
            try {
                input = new Scanner(fileOne);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(AnalysisFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

    String inputText = input.nextLine();

    String[] words = inputText.split("[ \n\t\r,.;:!?(){}]");

    TreeMap<String, Integer> mapOne = new TreeMap<>();

    for(int i = 0; i < words.length; i++){
        String key = words[i].toLowerCase();

        if (words[i].length() > 1){
            if (mapOne.get(key) == null){
                mapOne.put(key, 1);

            }
            else {
                int value = mapOne.get(key).intValue();
                value++;
                mapOne.put(key, value);


            }
        }


    }
    /* Experimental Set Code

    Set<String> setOne = mapOne.keySet();
    for (String key : setOne)
    {
        Integer value = mapOne.get(key);

    }
    * 
    */



                            }

                        }
                        ActionListener oneListener = new OneButtonListener();
                        oneBtn.addActionListener(oneListener);

            }



            public void createFileTwoPanel() {
                fileTwoPnl = new JPanel();

                TitledBorder fileTwoB;
                blackline = BorderFactory.createLineBorder(Color.black);
                fileTwoB = BorderFactory.createTitledBorder(blackline, "File 2");
                fileTwoB.setTitleJustification(TitledBorder.CENTER);
                fileTwoPnl.setBorder(fileTwoB);

                twoBtn = new JButton("Choose File");

                fileTwoPnl.add(twoBtn);
                twoBtn.setPreferredSize(new Dimension (100,30));

                        class TwoButtonListener implements ActionListener
                        {
                            @Override
                            public void actionPerformed(ActionEvent evt)
                            {
                                final JFileChooser twoFC = new JFileChooser();
                                twoFC.showOpenDialog(AnalysisFrame.this);
                                String newLine = null;
                                twoFC.getName(null);
                                int returnVal = 0;
                                File fileTwo = twoFC.getSelectedFile();

                                        Scanner input = null;
            try {
                input = new Scanner(fileTwo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(AnalysisFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

    String inputText = input.nextLine();

    String[] words = inputText.split("[ \n\t\r,.;:!?(){}]");

    TreeMap<String, Integer> mapTwo = new TreeMap<>();

    for(int i = 0; i < words.length; i++){
        String key = words[i].toLowerCase();

        if (words[i].length() > 1){
            if (mapTwo.get(key) == null){
                mapTwo.put(key, 1);

            }
            else {
                int value = mapTwo.get(key).intValue();
                value++;
                mapTwo.put(key, value);


            }
        }


    }


                            }
                        }
                        ActionListener twoListener = new TwoButtonListener();
                        twoBtn.addActionListener(twoListener);
            }




private void createTextPanel() { 
    textPnl = new JPanel();

    resultTA = new JTextArea(100, 25);
    resultTA.setEditable(false);
    resultTA.setLineWrap(true);
    resultTA.setWrapStyleWord(true);

    resultSP = new JScrollPane(resultTA);

    textPnl.add(resultTA);
    textPnl.add(resultSP);
}

private void createControlPanel() {
    controlPnl = new JPanel();
    controlPnl.setLayout(new GridLayout(2,1));


    createInfoPanel();
    controlPnl.add(infoPnl);

    createButtonPanel();
    controlPnl.add(buttonPnl);


}

            private void createInfoPanel() {
                infoPnl = new JPanel();

                JLabel infoLbl = new JLabel("Execute will find and output words both files have in common.", JLabel.CENTER);
                infoPnl.add(infoLbl);
            }

            private void createButtonPanel() {
                buttonPnl = new JPanel();

                goBtn = new JButton("Execute");
                quitBtn = new JButton("Quit");

                buttonPnl.add(goBtn);
                buttonPnl.add(quitBtn);

                goBtn.setPreferredSize(new Dimension (100,30));
                quitBtn.setPreferredSize(new Dimension (100,30));





                class QuitButtonListener implements ActionListener
                {
                    @Override
                    public void actionPerformed(ActionEvent evt)
                    {
                        System.exit(0);
                    }
                }

                        ActionListener quitListener = new QuitButtonListener();
                        quitBtn.addActionListener(quitListener);

                class GoButtonListener implements ActionListener
                {
                    @Override
                    public void actionPerformed(ActionEvent evt)
                    {


                    }
                }
                        ActionListener goListener = new GoButtonListener();
                        goBtn.addActionListener(goListener);

            }


}

这是我想出的一些伪代码,我不确定这是否可行或可行,但这是我想出的唯一方法

Get a word from mapOne
Check if word exists in mapTwo //This part is going to be the hardest I'm pretty sure
If word is found, get the key and set as freq2 then remove from mapTwo //Not sure if this is even needed
If word is not found in mapTwo, remove word from mapOne
Check mapOne.isEmpty if true, end loop

我的括号或其他东西似乎也有问题,我是编程新手,所以它可能是类似的东西,但是当我制作树形图时,我可以在同一个代码块中成功输出它们,但如果我去底部并尝试在其他地方输出它出现为空或未找到,就像地图只存在于该区域内,我知道我应该知道为什么会发生这种情况,但我不知道,我假设与公共或私人有关,或其他什么。

提前感谢您的帮助,抱歉,问题太大了,我很迷茫。

4

1 回答 1

4

我正在尝试比较两个 树形 图地图并比较 关键字 键,然后对于两个地图共有的那些我想 值连接在一起,然后将其输出 到文本区域

final Set<String> intersection;
final Set<String> values2;

// Since we're going to mutate the first set, we create a new set that is 
// completely independent from the map.  We fill it with they keys of the 
// first map.

intersection = new HashSet<String>(map1.keySet());  // get the first map's keys
values2 = map2.keySet();                            // ... and the second map's keys

intersection.retainAll(values2);                    // find the intersection

for (String key : intersection)                     // for each value in both sets ...
{                                                   // ... output the details
  System.out.println("key: " + key);
  System.out.println("value 1: " + map1.get(key));
  System.out.println("value 2: " + map2.get(key));
}

见:Set.retainAll()

于 2012-06-03T03:46:01.953 回答