【问题标题】:Want to iterate through hashTable keys, use keys() or keySet()?想要遍历 hashTable 键,使用 keys() 或 keySet()?
【发布时间】:2014-12-22 12:19:45
【问题描述】:

试图返回哈希表中出现奇数次的所有数字,我已经注释了坏行。

基本上,我希望代码通过 int 数组并将其分配给哈希表的键,如果它再次出现(偶数次),则将布尔值设为 false,如果出现奇数次则布尔值为真。然后我需要遍历哈希表的键并返回布尔值为 true 的键。

package practice;

import java.util.*;
/*

You are given an integer array, where all numbers except for TWO numbers appear even number of times. 

Q: Find out the two numbers which appear odd number of times.

*/

public class hashTable{
public static void main(String[] args){
    int[] test = {2, 2, 5, 7, 4, 4};
    List<Integer> solution = new ArrayList<Integer>();
    Hashtable<Integer, Boolean> ht = new Hashtable<Integer, Boolean>(); 
    Boolean check = true;
    for (int item : test){
        if (!ht.containsKey(item)){
            check = true;
        } else{
            check = false;
            ht.put(item, check);
        }
        }

    for (int item : ht.keySet()){
        if (ht.get(item) == true){
            solution.add(item);
        }
        }
    System.out.println("the result is");
    System.out.println(solution);
    }

 }

【问题讨论】:

  • 作为旁注,Hashtable 已过时,在这种情况下,您应该改用 HashMap。

标签: java hashtable


【解决方案1】:

当它为真时,您永远不会将项目放入哈希表中。

for (int item : test){
    if (!ht.containsKey(item)){
        check = true;
    } else{
        check = false;
        ht.put(item, check);
    }
    }

应该是

for (int item : test){
    if (!ht.containsKey(item)){
        check = true;
    } else{
        check = false;
    }
        ht.put(item, check);
    }

【讨论】:

    【解决方案2】:

    ht 始终为空:当您遍历测试时, containsKey 始终为 false,因此您永远不会插入到 ht 中。

    您根本没有正确构建 ht。

    【讨论】:

      【解决方案3】:

      keys() 方法将键集作为枚举返回。

      keySet() 方法将键集作为 Set 对象返回。

      !ht.containsKey(item) 不会是真的,所以这个check = true; 和你的ht 将是空的。

      因此,无论if 条件如何,您都需要首次将值添加到ht

      【讨论】:

      • 可能我不是很清楚,我已经更新了代码,它运行了,但是当它应该是 [5,7] 时解决方案是空的
      【解决方案4】:

      工作代码

      import java.util.*;
      /*
      
      You are given an integer array, where all numbers except for TWO numbers appear even number of times. 
      
      Q: Find out the two numbers which appear odd number of times.
      
      */
      
      public class hashTable{
      public static void main(String[] args){
          int[] test = {2, 2, 5, 7, 4, 4};
          List<Integer> solution = new ArrayList<Integer>();
          Hashtable<Integer, Boolean> ht = new Hashtable<Integer, Boolean>(); 
          Boolean check = true;
          for (int item : test){
              if (!ht.containsKey(item)){
                  check = true;
              } else{
                  System.out.println("else"+item);
                  check = false;
      
              }
              ht.put(item, check);
              }
          for (int item : ht.keySet()){
              System.out.println("output"+item);
              if (ht.get(item) == true){
                  solution.add(item);
              }
              }
          System.out.println("the result is");
          System.out.println(solution);
          }
      
       }
      

      【讨论】:

        【解决方案5】:

        如果您使用的是 Java 8,则可以使用 Streams 解决您的问题。一个简短的解决方案是:

        int[] test = {2, 2, 5, 7, 4, 4};
        
        // Collect to a Map<Integer, List<Integer>>
        Map<Integer, List<Integer>> m = 
            Arrays.stream(test).boxed().collect(Collectors.groupingBy(a -> a));
        
        // Stream the map, filter out the odd ones and collect to a list
        final List<Integer> odds = 
            m.entrySet().stream()
                .filter(e -> e.getValue().size() % 2 != 0)
                .map(e -> e.getKey())
                .collect(Collectors.toList());
        

        或者,如果您更喜欢它作为“oneliner”

        final List<Integer> odds =
                Arrays.stream(test)
                        .boxed() // Create a stream of Integers
                        .collect(Collectors.groupingBy(a -> a)) // Group by the key
                        .entrySet().stream() // Create a stream of the entrySet
                        .filter(e -> e.getValue().size() % 2 != 0) // Keep only the odd ones
                        .map(e -> e.getKey()) // Convert Entry to an integer (the key)
                        .collect(Collectors.toList()); // Collect it to a list
        

        【讨论】:

          猜你喜欢
          • 2015-11-17
          • 1970-01-01
          • 2016-07-09
          • 1970-01-01
          • 2022-01-02
          • 2012-09-19
          • 2017-03-26
          • 2023-01-12
          • 1970-01-01
          相关资源
          最近更新 更多