Java 计算链表中重复项的数量
我试图将文本文件放入linkedlist,并计算链接列表中有多少重复的单词 这是我的基本代码Java 计算链表中重复项的数量,java,Java,我试图将文本文件放入linkedlist,并计算链接列表中有多少重复的单词 这是我的基本代码 public class Node{ private Node next; private String data; private int Dup_Counter= 0; public Node(){ this.next = null; this.data = data;
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]);
}
公共类节点{
私有节点下一步;
私有字符串数据;
专用int Dup_计数器=0;
公共节点(){
this.next=null;
这个数据=数据;
此.Dup_计数器=0;
}
公共字符串fiile_Reader()引发FileNotFoundException{
File File=new File(“/Users/djanz/IdeaProjects/datalab2/pg174.txt”);//读取纯文本文件
扫描仪扫描=新扫描仪(文件);
String fileContent=“;//初始化空字符串以放入扫描的字符串文本文件
while(scan.hasNextLine()){
fileContent=fileContent.concat(scan.nextLine()+“\n”);//扫描并放入字符串对象
}
fileContent=fileContent.replaceAll(“\\p{Punct},”);//删除所有标点字符
fileContent=fileContent.toLowerCase();
返回文件内容;
}
public void insert()引发FileNotFoundException{
节点光标=头部;
Single_LL Linked_L=新的Single_LL();
字符串文件内容=链接的文件读取器();
字符串[]拆分的文件=文件内容。拆分(“”);
对于(int i=0;i 20){
rtn+=cursor.data+“->”;
}
cursor=cursor.next;
}
返回rtn;
}
公共静态void main(字符串[]args)引发FileNotFoundException{
Program1测试=新的Program1();
字符串文件_content=test.fiile_Reader();
Single_LL Linked_L=新的Single_LL();
字符串[]拆分的文件=文件内容。拆分(“”);
int spli_len=拆分的文件.length;
对于(int i=0;i
我的方法是在节点类中添加一个名为dup_counter的anthoer变量。
函数Dup_Count()在链表中循环,当它看到重复时,会更新节点的Dup_计数器变量
我正在尝试查找出现超过20次的单词,dup_num()是我的方法。通过链接列表循环,如果节点的dup_计数器超过20,则将其添加到字符串中并返回它。但是,dup_Count()实际上没有更新dup_计数值。插入工作正常,但我似乎找不到我的dup_计数器有什么问题。有人能帮我修复这个错误吗?我建议尝试使用以下映射来简化您的任务 以某种方式将所有单词放入一个集合
集合单词
。使用您已有的读卡器代码应该很容易做到这一点
现在,为了计算每个单词出现的次数,我们可以使用地图:
Map<Integer, Long> counter = words.stream()
.collect(Collectors.groupingBy(p -> p, Collectors.counting()));
你真的在某处调用了
Dup\u Count()
吗?是的,它没有显示在我的代码中,但我在使用我的main方法进行测试时调用了它。我问这个问题的原因是,正如我所知,调用Dup\u Count()
会破坏列表,因为它会修改标题
。每次调用都会从列表前面删除一项;调用足够多次,列表就会消失!首先,感谢您的回答,但我不允许使用MapI see。祝您好运。
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();