如何在LinkedList中搜索特定单词,并返回该单词在列表中的位置以及出现的次数



我遇到了一个问题,因为我需要搜索我创建的链表,并输出指定单词出现的次数以及单词在列表中的位置。这一切都是在Java GUI中完成的,其中特定单词的输入是使用文本字段完成的。我在代码中遇到问题的特定按钮是"searchList"按钮,我已经在其中添加了代码,但我仍然没有得到所需的结果。

如有任何帮助,我们将不胜感激!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
FilledFrame frame = new FilledFrame();
frame.setVisible( true );
frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setTitle("Word List");
}
}
class  FilledFrame extends JFrame{
JLabel     addWord;
JTextField addW;
JTextArea wordArea;
// Creating linked list
private LinkedList<String> wordList = new LinkedList();
public FilledFrame(){
//Create JTextArea
wordArea                = new JTextArea();
//Create all the buttons, JLabel and the JPanel
JButton addButton       = new JButton("Add Word");
JButton specifiedLetter = new JButton("Display Specific Letter");
JButton searchList      = new JButton("Search List");
JButton removeLastOcc   = new JButton("Remove Last");
JButton removeAll       = new JButton("Remove All Word Occurrence's ");
JButton clearList       = new JButton("Clear List");
JPanel panel            = new JPanel();
//Add buttons and label to the window
panel.add(addButton);
add(panel, BorderLayout.NORTH);
panel.add(specifiedLetter);
add(panel, BorderLayout.NORTH);
panel.add(searchList);
add(panel, BorderLayout.NORTH);
panel.add(removeLastOcc);
add(panel, BorderLayout.NORTH);
panel.add(removeAll);
add(panel, BorderLayout.NORTH);
panel.add(clearList);
add(panel, BorderLayout.NORTH);

//Create all Text Fields and Labels
addWord          = new JLabel("Enter word");
addW             = new JTextField(20);
JPanel panel1     = new JPanel();
//Add labels and text fields to the window
panel1.add(addWord);
add(panel1, BorderLayout.SOUTH);
panel1.add(addW);
add(panel1, BorderLayout.SOUTH);

//Add JTextArea to the center and make sure user cannot type into it
add(wordArea, BorderLayout.CENTER);
wordArea.setEditable( false );

//  Action listeners for each button
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wordList.add((addW.getText()));
wordArea.setText(" The word " + addW.getText() + " was added to the list ");
System.out.println(wordList);
}
});
specifiedLetter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

}
});
//The button I am having trouble with
searchList.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String showWord = "";
TreeMap<String, Integer> treeMap = new TreeMap<>();
if ((addW.getText()).length() < 1){
for(String word : wordList)
{
treeMap.put(word, 1);
treeMap.keySet().contains(word);
if (treeMap.containsKey(word)) {
treeMap.replace(word, treeMap.get(word)+1);
}
else{
treeMap.put(word, 1);
}
}
wordArea.setText(showWord);
for (String word: wordList){
System.out.println(" This word appears " + word);
}
}
}

});

您可以使用java 8:中的Collector,用一行简单的代码替换您编写的整个逻辑

Map<String, Long> collect = list.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

在这里,您将得到一个Map<String, Long>,关键字是单词,值是它重复的次数。

要找到它出现的位置,您可以遍历列表,并从刚刚创建的映射中找到单词的第一个出现。

试试这个解决方案:

Map<String, List<Integer>> map = new HashMap<>();
IntStream.range(0, list.size())
.forEach(i -> {
map.computeIfAbsent(list.get(i), s -> new ArrayList<>()).add(i);
});

它将把所有索引存储在列表映射中。现在您可以很容易地通过map.get(str)获取索引或通过map.get(str).size()获取出现次数。

请注意,此解决方案同时执行所有操作。因此,您不需要对任何集合进行两次迭代。

最新更新