Java语言读取打开字典以后

Java语言读取打开字典以后,字典的内容是不重复的,此时插入重复的内容,怎么对字典进行一个判断呢

看下是这个意思不 :

img

代码如下 :

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中字典的键是不允许重复的,如果是要判断字典的值是否重复,可以在插入数据前,先遍历字典原有的值,判断插入的数据是否和字典中的值是否重复, 然后再根据判断情况进行相应操作即可,不知道是不是你想问的那种情况?

测试代码如下:

参考链接:


java如何遍历字典_JAVA 中 Map的遍历_刘宏甲的博客-CSDN博客 首先我们需要一个map。。。(有好多键值)Mapmap=newHashMap();map.put("name1","value1");map.put("name2","value2");map.put("name3","value3");法一:使用map.entrySet() 方法Entry是一个键值对对象,包含一个key和一个value,他是Map接口中定义的内部接口(Map.Entry... https://blog.csdn.net/weixin_42495556/article/details/114892902

java遍历字典_Map集合的四种遍历方式代码示例_weixin_39966765的博客-CSDN博客 很久以前写的代码,和上一个做比较吧!便于以后查看。import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class TestMap {public static void main(String[] args) {Map map = new HashMap();map.put(1, "a");m..._java怎么遍历字典 https://blog.csdn.net/weixin_39966765/article/details/114572909



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());
        }
    }

}


img

可以使用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;
            }
        }
    }
} 

望采纳