Java打开一个文件里存储的字典的数据结构,读取其中的所有的key,判断是否有重复,有重复的把重复的键值对调,这个过程总是思路不清,具体来说,文件的打开和加载怎么判断重复呢
这种情况使用你读取后文件中的内容后使用LinkedHashMap类存储键值对,判断是否有重复的键。有重复的,重复的键值对换位置,新的键值对的键为原键值对的键加上"_duplicate"。
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
File file = new File("test.txt");
Scanner scanner = new Scanner(file);
Map<String, String> map = new LinkedHashMap<>();
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(":", 2);
String key = parts[0];
String value = parts[1];
if(map.containsKey(key)) {
String tempValue = map.get(key);
map.put(key, value);
map.put(key + "_duplicate", tempValue);
} else {
map.put(key, value);
}
}
scanner.close();
FileWriter writer = new FileWriter(file);
for(String key: map.keySet()) {
String value = map.get(key);
writer.write(key + ":" + value + "\n");
}
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Dictionary {
public static void main(String[] args) {
// 创建一个HashMap对象,用于存储键值对
Map<String, String> map = new HashMap<>();
try {
// 创建一个File对象,指定字典文件
File file = new File("dictionary.txt");
// 创建一个Scanner对象,用于读取字典文件
Scanner scanner = new Scanner(file);
// 逐行读取字典文件中的内容
while (scanner.hasNextLine()) {
// 获取当前行的内容
String line = scanner.nextLine();
// 使用":"将当前行的内容分割为键和值
String[] parts = line.split(":");
// 判断当前行的键是否已经存在于map中
if (map.containsKey(parts[0])) {
// 如果存在,将当前行的值与已有的值交换
String temp = map.get(parts[0]);
map.put(parts[0], parts[1]);
map.put(parts[1], temp);
} else {
// 如果不存在,直接将当前行的键值对添加到map中
map.put(parts[0], parts[1]);
}
}
// 关闭Scanner对象
scanner.close();
} catch (FileNotFoundException e) {
// 如果文件不存在,打印异常信息
e.printStackTrace();
}
// 输出map中的所有键值对
System.out.println(map);
}
}
打开文件
在Java中可以使用File类来打开文件,例如:
File file = new File("dictionary.txt");
这里假设字典文件名为dictionary.txt,如果文件不存在,则会创建一个空文件。
加载数据
可以使用Java的IO流来读取文件中的数据,例如使用BufferedReader类来读取:
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// 处理每一行数据
}
} catch (IOException e) {
e.printStackTrace();
}
这里使用了try-with-resources语句来自动关闭流,避免资源泄漏。在while循环中,每次读取一行数据,并进行处理。
判断重复
如果字典中的key是唯一的,可以使用Java中的Set来存储key,Set中不允许重复元素。例如:
Set<String> keys = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(":");
String key = parts[0];
if (keys.contains(key)) {
// key重复,进行处理
} else {
keys.add(key);
// 处理key
}
}
} catch (IOException e) {
e.printStackTrace();
}
在while循环中,首先将每一行数据按照":"分割成key和value两部分,然后判断key是否已经存在于Set中,如果存在则说明重复,进行处理;否则将key添加到Set中,进行处理。
不知道你这个问题是否已经解决, 如果还没有解决的话:和上边删除第一次出现的key类似(注释可以看上边删除第一次出现的key),只不过在删除节点过后不要break,让遍历循环继续进行
//删除所有值为key的节点 public void removeAllKey(int key){ ListNode cur = this.head; while(cur != null){ if(cur.val==key){ if(cur==head){//在头部 head=head.next; if (head==null) {//只有一个元素,要检查 break; } head.prev=null; } if (cur==last){//在尾部 last=last.prev; last.next=null; } if (cur.prev!=null && cur.next!=null){//在中间 cur.prev.next=cur.next; cur.next.prev=cur.prev; } } cur=cur.next; } display(); }