为什么在使用containsKey方法后,我的列表中出现了重复项?

huangapple go评论74阅读模式
英文:

why i am getting duplicates in my list after using containsKey method

问题

 private static void makeList(int n, ArrayList<Integer> minimum, HashMap<Integer, Integer> ugly) {
        for (int i = 1; minimum.size() < n; ++i) {
           
            if (!ugly.containsKey(2 * i)) {
                minimum.add(2 * i);
                ugly.put(2 * i, 2 * i);
            }
            int m3 = 3 * i; 
            if (!ugly.containsKey(3 * i)) {
                minimum.add(3 * i);
                ugly.put(3 * i, 3 * i);
            }
            int m5 = 5 * i;
            if (!ugly.containsKey(m5)) {
                minimum.add(m5);
                ugly.put(m5, m5);
            }
        }  
    }
英文:

i am using hashmap to validate that key dosent exist in the map and adding the key to a list i am using containsKey to prevent duplicate but its adding duplicate to my list

 private static  void makeList(int n,ArrayList&lt;Integer&gt; minimum ,HashMap&lt;Integer,Integer&gt; ugly) {
        for(int i=1; minimum.size() &lt; n; ++i) {
           
            if(!ugly.containsKey(2*i)); {
            minimum.add(2*i);
            ugly.put(2*i,2*i );
            }
            int m3=3*i; 
            if(!ugly.containsKey(3*i)){
            minimum.add(3*i);
             ugly.put(3*i,3*i);   
            }
            int m5=5*i;
            if(!ugly.containsKey(m5)){
           minimum.add(m5);
           ugly.put(m5,m5);
            }
            
            
        }
            
    } 

答案1

得分: 2

我看到你在if()条件后面加了分号;

只需将其删除然后尝试:

private static void makeList(int n, ArrayList<Integer> minimum, HashMap<Integer, Integer> ugly) {
    for (int i = 1; minimum.size() < n; ++i) {
        if (!ugly.containsKey(2 * i)) { // <-编辑部分,删除了分号
            minimum.add(2 * i);
            ugly.put(2 * i, 2 * i);
        }
        int m3 = 3 * i;
        if (!ugly.containsKey(3 * i)) {
            minimum.add(3 * i);
            ugly.put(3 * i, 3 * i);
        }
        int m5 = 5 * i;
        if (!ugly.containsKey(m5)) {
            minimum.add(m5);
            ugly.put(m5, m5);
        }
    }
}
英文:

I see you gave semicolon ; after if() condition

just remove that and try

 private static  void makeList(int n,ArrayList&lt;Integer&gt; minimum ,HashMap&lt;Integer,Integer&gt; ugly) {
        for(int i=1; minimum.size() &lt; n; ++i) {
           
            if(!ugly.containsKey(2*i)) { //&lt;-edited part, removed semicolon
            minimum.add(2*i);
            ugly.put(2*i,2*i );
            }
            int m3=3*i; 
            if(!ugly.containsKey(3*i)){
            minimum.add(3*i);
             ugly.put(3*i,3*i);   
            }
            int m5=5*i;
            if(!ugly.containsKey(m5)){
           minimum.add(m5);
           ugly.put(m5,m5);
            }
            
            
        }
            

答案2

得分: 0

简化并清晰代码。

private static void makeList(int n, List<Integer> minimum, Map<Integer, Integer> ugly) {
    for (int i = 1; minimum.size() < n; i++)
        for (int j : Arrays.asList(2, 3, 5))
            if (ugly.put(i * j, i * j) == null)
                minimum.add(i * j);
}

附注: 为什么你不使用 Set<Integer> 而不是带有 key == valueMap<Integer, Integer>

英文:

Be simple and it clear the code.

private static  void makeList(int n, List&lt;Integer&gt; minimum, Map&lt;Integer,Integer&gt; ugly) {
    for (int i = 1; minimum.size() &lt; n; i++)
        for (int j : Arrays.asList(2, 3, 5))
            if(ugly.put(i * j, i * j) == null)
                minimum.add(i * j);
}

P.S. Why don't you use Set&lt;Integer&gt; instead of Map&lt;Integer, Integer&gt; with key == value?

huangapple
  • 本文由 发表于 2020年9月13日 15:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定