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

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

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

问题

  1. private static void makeList(int n, ArrayList<Integer> minimum, HashMap<Integer, Integer> ugly) {
  2. for (int i = 1; minimum.size() < n; ++i) {
  3. if (!ugly.containsKey(2 * i)) {
  4. minimum.add(2 * i);
  5. ugly.put(2 * i, 2 * i);
  6. }
  7. int m3 = 3 * i;
  8. if (!ugly.containsKey(3 * i)) {
  9. minimum.add(3 * i);
  10. ugly.put(3 * i, 3 * i);
  11. }
  12. int m5 = 5 * i;
  13. if (!ugly.containsKey(m5)) {
  14. minimum.add(m5);
  15. ugly.put(m5, m5);
  16. }
  17. }
  18. }
英文:

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

  1. private static void makeList(int n,ArrayList&lt;Integer&gt; minimum ,HashMap&lt;Integer,Integer&gt; ugly) {
  2. for(int i=1; minimum.size() &lt; n; ++i) {
  3. if(!ugly.containsKey(2*i)); {
  4. minimum.add(2*i);
  5. ugly.put(2*i,2*i );
  6. }
  7. int m3=3*i;
  8. if(!ugly.containsKey(3*i)){
  9. minimum.add(3*i);
  10. ugly.put(3*i,3*i);
  11. }
  12. int m5=5*i;
  13. if(!ugly.containsKey(m5)){
  14. minimum.add(m5);
  15. ugly.put(m5,m5);
  16. }
  17. }
  18. }

答案1

得分: 2

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

只需将其删除然后尝试:

  1. private static void makeList(int n, ArrayList<Integer> minimum, HashMap<Integer, Integer> ugly) {
  2. for (int i = 1; minimum.size() < n; ++i) {
  3. if (!ugly.containsKey(2 * i)) { // <-编辑部分,删除了分号
  4. minimum.add(2 * i);
  5. ugly.put(2 * i, 2 * i);
  6. }
  7. int m3 = 3 * i;
  8. if (!ugly.containsKey(3 * i)) {
  9. minimum.add(3 * i);
  10. ugly.put(3 * i, 3 * i);
  11. }
  12. int m5 = 5 * i;
  13. if (!ugly.containsKey(m5)) {
  14. minimum.add(m5);
  15. ugly.put(m5, m5);
  16. }
  17. }
  18. }
英文:

I see you gave semicolon ; after if() condition

just remove that and try

  1. private static void makeList(int n,ArrayList&lt;Integer&gt; minimum ,HashMap&lt;Integer,Integer&gt; ugly) {
  2. for(int i=1; minimum.size() &lt; n; ++i) {
  3. if(!ugly.containsKey(2*i)) { //&lt;-edited part, removed semicolon
  4. minimum.add(2*i);
  5. ugly.put(2*i,2*i );
  6. }
  7. int m3=3*i;
  8. if(!ugly.containsKey(3*i)){
  9. minimum.add(3*i);
  10. ugly.put(3*i,3*i);
  11. }
  12. int m5=5*i;
  13. if(!ugly.containsKey(m5)){
  14. minimum.add(m5);
  15. ugly.put(m5,m5);
  16. }
  17. }

答案2

得分: 0

简化并清晰代码。

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

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

英文:

Be simple and it clear the code.

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

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:

确定