java - 计算链表Java中的重复数
问题描述
我正在尝试将文本文件放入链接列表并计算链接列表中有多少重复单词。
这是我的基本代码。
public class Node{
private Node next;
private String data;
private int Dup_Counter= 0;
public Node(){
this.next = null;
this.data = data;
this.Dup_Counter = 0;
}
public String fiile_Reader() throws FileNotFoundException {
File file = new File("/Users/djhanz/IdeaProjects/datalab2/pg174.txt"); //reading a plain text file
Scanner scan = new Scanner(file);
String fileContent = ""; // initalizing an empty string to put scanned string text file
while (scan.hasNextLine()) {
fileContent = fileContent.concat(scan.nextLine() + "\n"); // scan and put in into string object
}
fileContent = fileContent.replaceAll("\\p{Punct}", ""); // remove all the punctuation characters
fileContent = fileContent.toLowerCase();
return fileContent;
}
public void insert() throws FileNotFoundException{
Node cursor = head;
Single_LL Linked_L = new Single_LL();
String file_content = Linked_L.fiile_Reader();
String[] splitted_File = file_content.split(" ");
for(int i=0 ; i<splitted_File.length; i++){
Linked_L.add(splitted_File[i]);
}
}
public int Word_Counter(String word){
String compare =word;
Node cursor = head;
int counter = 0;
while(cursor!=null){
if (cursor.data.equals(compare)){
counter++;
}
cursor = cursor.next;
}
return counter;
}
public void Dup_Count(){
Node cursor = head.next;
while (cursor != null){
if(head.data == cursor.data){
head.Dup_Counter++;
break;
}
cursor = cursor.next;
System.out.println(cursor.Dup_Counter);
}
head = head.next;
}
public String dup_num(){
Node cursor = head;
String rtn = "";
while (cursor!= null){
if(cursor.Dup_Counter > 20 ){
rtn += cursor.data + " -> ";
}
cursor = cursor.next;
}
return rtn;
}
public static void main(String[] args) throws FileNotFoundException {
Program1 test = new Program1();
String file_content = test.fiile_Reader();
Single_LL Linked_L = new Single_LL();
String[] splitted_File = file_content.split(" ");
int spli_len = splitted_File.length;
for(int i =0; i< spli_len; i++){
Linked_L.add(splitted_File[i]);
}
我的方法是在名为 dup_counter 的节点类中添加另一个变量。函数 Dup_Count() 在链表中循环,当它看到重复项时,它会更新节点的 dup_counter 变量。
我试图找到出现超过 20 次的单词,而 dup_num() 是我这样做的方法。循环遍历链表,如果节点的 dup_counter 大于 20,则将其添加到字符串中并返回。但是,Dup_Count() 实际上并没有更新 dup_count 值。插入工作正常,但我似乎找不到我的 dup_counter 有什么问题。有人可以帮我修复错误吗?
解决方案
我建议尝试使用 Map 来简化您的任务,如下所示
不知何故把所有的词都放到一个集合Collection<String> words
中。使用您已有的阅读器代码应该很容易做到这一点。
现在,要计算每个单词的出现次数,我们可以使用 Map:
Map<Integer, Long> counter = words.stream()
.collect(Collectors.groupingBy(p -> p, Collectors.counting()));
现在你想找出所有出现超过 20 次的单词,你可以评估
Set<String> wordsOccuringManyTimes = counter
.entrySet().stream()
.filter(e -> e.getValue() > 20)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
如果您想获得所有重复项的总和,您可以简单地评估
int duplicateCount = counter.values().stream().mapToInt(x -> x - 1).sum();
推荐阅读
- jquery - JQuery在单击时显示div并再次显示
- python - Django DateTimeField() 导致 TypeError('datetime.timedelta' 对象不可调用)
- python - 为将提取所有具有三角形关系的子图的方法创建一个循环
- javascript - render react component when prop changes
- laravel - 使用 laravel 在 vue js 中转换日期格式
- c# - IdentityServer4:数据库中不同表中的不同用户类型
- c# - 发布模式下的 Xamarin.UWP 运行时错误
- makefile - 找不到链接库 (lNrrdIO)
- javascript - 使用 Chrome for IOS 从网站录制声音
- xml - 对具有不同节点名称的 XSLT 1.0 的 for-each 中的值求和