Java语言读取打开字典以后,字典的内容是不重复的,此时插入重复的内容,怎么对字典进行一个判断呢
看下是这个意思不 :
代码如下 :
import java.util.HashSet;
public class DictionaryExample {
public static void main(String[] args) {
HashSet<String> dictionary = new HashSet<>();
HashSet<String> duplicateValues = new HashSet<>(); // 保存重复的值
// 假设字典已经包含以下内容
dictionary.add("Apple");
dictionary.add("Banana");
dictionary.add("Orange");
// 要插入的内容
String newWord = "Banana";
if (dictionary.contains(newWord)) { // 判断是否已存在
duplicateValues.add(newWord);
System.out.println("重复插入: " + newWord);
} else {
dictionary.add(newWord);
System.out.println("插入成功: " + newWord);
}
// 打印字典内容
System.out.println("字典内容:");
for (String word : dictionary) {
System.out.println(word);
}
// 打印重复的值
System.out.println("重复的值:");
for (String duplicate : duplicateValues) {
System.out.println(duplicate);
}
}
}
java中字典的键是不允许重复的,如果是要判断字典的值是否重复,可以在插入数据前,先遍历字典原有的值,判断插入的数据是否和字典中的值是否重复, 然后再根据判断情况进行相应操作即可,不知道是不是你想问的那种情况?
测试代码如下:
参考链接:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class DictionaryTest {
// https://ask.csdn.net/questions/7971190?spm=1005.2025.3001.5141
public static void main(String[] args) {
// TODO Auto-generated method stub
// https://blog.csdn.net/weixin_63880793/article/details/124311529
Map<String,String> datas = new HashMap<>();
// 先产生原始字典数据
for(int i=0;i<5;i++) {
datas.put((i+1)+"", "数据"+(i+1));
}
System.out.println("插入数据前,字典内容为:");
// 打印插入数据前的字典数据
// https://blog.csdn.net/weixin_42495556/article/details/114892902
for(Map.Entry entry:datas.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
// 从输入获取键值,然后判断是否重复,再决定是否插入字典
Scanner sc = new Scanner(System.in);
System.out.println("\n\n请输入一个字典的键和值(输入! !退出):");
String key= sc.next();
String value = sc.next();
int find=0;
while("!".equals(key)==false) {
// 如果输入的键在字典不存在
if(datas.containsKey(key)==false) {
find=0;
// https://blog.csdn.net/weixin_39966765/article/details/114572909
// 判断输入的值是否和字典中原有的值是否重复
for (String v :datas.values()) {
if(value.equals(v)==true) {
System.out.println("值"+value+"已存在,请输入其他值!");
find=1;
break;
}
}
// 如果不重复,则将输入的键值插入字典
if(find==0) {
datas.put(key, value);
}
}else { // 否则,打印错误提示信息
System.out.println("键"+(key)+"已存在,请输入其他键!");
}
//获取下一个键
System.out.println("\n请输入一个字典的键和值(输入! !退出):");
key= sc.next();
value = sc.next();
}
// 打印插入数据后的字典数据
System.out.println("插入数据后,字典内容为:");
// https://blog.csdn.net/weixin_42495556/article/details/114892902
for(Map.Entry entry:datas.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
要实现这个功能,可以使用Java的HashSet集合来存储字典中的内容。
HashSet<String> dictionary = new HashSet<String>();
File file = new File("dictionary.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
dictionary.add(word);
}
scanner.close();
String wordToInsert = "hello";
boolean isDuplicate = dictionary.contains(wordToInsert);
if (isDuplicate) {
System.out.println("该字典中已存在相同内容");
} else {
dictionary.add(wordToInsert);
System.out.println("插入成功");
}
以上是一种基于HashSet的解决方案,可以用来判断字典中是否已存在相同内容。如果字典的内容非常庞大,这种方法可能会消耗大量的内存。如果内存有限,可以考虑使用其他数据结构,如Trie树。但是Trie树的实现稍微复杂一些,如果您有兴趣可以深入学习。
可以使用HashSet来实现一个字典(不重复集合)
Set<String> dict = new HashSet<>();
// 添加元素
dict.add("cat");
dict.add("dog");
dict.add("bird");
// 判断重复插入
if (!dict.add("cat")) {
// "cat"已经存在,插入失败
}
HashSet的add方法在插入成功时返回true,在元素已经存在时返回false。
所以我们可以在添加元素后判断返回值,如果为false则表示元素重复。
import java.util.HashSet;
import java.util.Set;
import java.util.Scanner;
public class Dictionary {
private Set<String> words = new HashSet<>();
public void add(String word) {
if (words.add(word)) {
System.out.println(word + " added.");
} else {
System.out.println(word + " is already in the dictionary.");
}
}
public void remove(String word) {
if (words.remove(word)) {
System.out.println(word + " removed.");
} else {
System.out.println(word + " is not in the dictionary.");
}
}
public void printf() {
for (String word : words) {
System.out.println(word);
}
}
public static void main(String[] args) {
Dictionary dict = new Dictionary();
Scanner in = new Scanner(System.in);
while (true) {
String line = in.nextLine();
String[] tokens = line.split(" ");
if (tokens[0].equals("add")) {
dict.add(tokens[1]);
} else if (tokens[0].equals("remove")) {
dict.remove(tokens[1]);
} else if (tokens[0].equals("print")) {
dict.printf();
} else if (tokens[0].equals("exit")) {
break;
}
}
}
}
望采纳