标签: hashset

Java HashSet键/值对

为什么Java不提供函数来获取HashSet类似的键/值对Hashtable?每次你需要得到某些东西时,必须迭代它似乎是一种真正的痛苦.或者有更简单的方法吗?

java iteration hashset

0
推荐指数
1
解决办法
2万
查看次数

Java中的HashSet - 比较和hashkeys

我知道HashSet.contains()方法使用.equals方法来检查相等性,因为它检查指针以查看它们是否相等.

我需要它来检查指针上的实际对象是否相等 - 在我的具体情况下,我需要查看HashSet中是否已经存在打开的"Node"(一个int []数组).这对我的搜索算法至关重要,因此我的双向迭代深化搜索的实现并不那么天真.

如果可能的话,我仍然喜欢线性搜索时间,或者我应该使用不同的类?

谢谢你的帮助.

java queue hashset

0
推荐指数
1
解决办法
407
查看次数

Consistency of contains method on a HashSet and HashMap

Why does containsAll method on a HashSet does not remain consistent if remove is called on the Set whereas a containsValue method on a HashMap remains consistent after a value is removed After a value is removed from a HashSet containsAll returns false even if all values were present where as in case of HashMap the containsValue method returns correct value

public static void main(String[] args)
    {

    HashSet<String> lookup=new HashSet<String>();
    HashMap<Integer,String> findup=new HashMap<Integer,String>();

    String[] Alltokens={"This","is","a","programming","test","This","is","a","any","language"};
    for(String s:Alltokens)
        lookup.add(s);

    String[] tokens={"This","is","a"}; …
Run Code Online (Sandbox Code Playgroud)

java hashmap hashset

0
推荐指数
1
解决办法
289
查看次数

使用HashSet测试包含

我正在用Java编写一个程序,我想使用HashSet(和HashMap).我无法使contains(和containsKey)方法起作用.我想我必须在某处覆盖一些equals方法才能使用.我们的想法是获得以下代码来生成输出:true.有关如何做到这一点的任何想法?

import java.util.HashSet;
import java.util.Set;

public class Sets {

    public static void main(String args[]){

        Set<StringBuilder> wordSet = new HashSet<StringBuilder>();
        StringBuilder element = new StringBuilder("Element");
        wordSet.add(element);
        StringBuilder element2 = new StringBuilder("Element");
        System.out.println(wordSet.contains(element2));

    }

}
Run Code Online (Sandbox Code Playgroud)

java hashset

0
推荐指数
1
解决办法
328
查看次数

整数对添加到hashset java

我将整数对类设置如下:

public class pair{
    int a;
    int b;
    pair(int p,int q){
        this.a=p;
        this.b=q;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将它们添加到hashset时,没有重复:

HashSet<pair> set=new HashSet<pair>();
        pair temp=new pair(3,5);
        set.add(temp);
        pair temp1=new pair(3,5);
        set.add(temp1);
        for(pair p:set){
            System.out.println(p.a+"  "+p.b);
        }
Run Code Online (Sandbox Code Playgroud)

但它给了我这个输出:

3  5
3  5
Run Code Online (Sandbox Code Playgroud)

我应该编辑什么以在hashset中没有重复?

java hashset

0
推荐指数
1
解决办法
3754
查看次数

如何返回一个没有重复的属性的列表?

我有一个从数据库中提取州和城市列表的方法.州是独一无二的,但该州可能有许多城市.我的方法目前所做的是将每个州和城市对作为单独的项目返回.我需要它做的是拥有许多城市的州.

目前退货

辛辛那提

哦 - 克利夫兰

哦,findlay

在印第安纳波利斯

我需要它返回

哦,辛辛那提,克利夫兰,芬德利

在印第安纳波利斯

模型

public class Location
{
    public string State { get; set; }
    public string city { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

REPOSITORY

public HashSet<Location> getlocation()
    {
        HashSet<Location> myHashset = new HashSet<Location>();

        const string storedProc = "someProc";

        dynamic locations;


        using (var conn = DbFactory.myConnection())
        {
            locations = conn.Query(storedProc, commandType: CommandType.StoredProcedure);

        }

        foreach (var location in locations)
        {

                myHashset.Add(new location{State = location.state,City = location.city});


        }
          return myHashset
    }
Run Code Online (Sandbox Code Playgroud)

c# list unique hashset

0
推荐指数
1
解决办法
61
查看次数

Java:为什么我的HashSet和TreeSet包含重复项?

当我的文本文件包含:

a b c b
Run Code Online (Sandbox Code Playgroud)

我的HashSet和TreeSet说有3个独特的单词.

当我的文本文件包含:

a b c a
Run Code Online (Sandbox Code Playgroud)

我的HashSet和TreeSet说有4个独特的单词.

为什么?

  public static int countUnique1A(WordStream words) {

    HashSet<String> hashSetA = new HashSet<String>();
    for (String i : words) {
      hashSetA.add(i);
    }

    return hashSetA.size();
  }

  public static int countUnique1B(WordStream words) {

    TreeSet<String> treeSetA = new TreeSet<String>();
    for (String i : words) {
      treeSetA.add(i);
    }
    return treeSetA.size();
  }
Run Code Online (Sandbox Code Playgroud)

java unique hashset treeset

0
推荐指数
1
解决办法
136
查看次数

HashSet和HashMap都使用Hashtable吗?或者Hashtable是完全不同的数据结构?

我已经知道当我们有Key-Value对时我们使用HashMap,而当我们不需要重复数据时我们使用HashSet.但是我总是对Hashtable发挥作用感到困惑.它是一个完全不同的数据结构吗?或者HashMap和HashSet都使用哈希函数在Hashtable中存储数据?彻底的解释将非常有用.

java hashtable hashmap hashset

0
推荐指数
2
解决办法
2618
查看次数

Java-如何知道HashSet被淘汰了什么?

如何知道什么已被淘汰HashSet?

我有一个阵列 int [] x = {2, 4, 4, 5};

当我隐身时, HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(x));

我怎么知道哪些元素被淘汰xset

java arrays hashset

0
推荐指数
1
解决办法
80
查看次数

为什么这个HashSet代码片段会以这种方式运行?

给出以下代码:

public class NewClass {

    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("Orange", "Pineapple", "Banana", "Banana");
        Set<String> fruitsSet = new HashSet<>();

        for (String fruit : fruits) {
            fruitsSet.add(fruit);
        }

        for (String fruit : fruitsSet) {
            System.out.println(fruit);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

每次运行代码时,元素的顺序都是相同的,消除了重复项Banana,这是典型的HashSet实现:

Banana
Pineapple
Orange
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么订单每次都相同,因为规范说"它不能保证集合的迭代顺序"(https://docs.oracle.com/javase/7/docs/api/ java/util/HashSet.html)

java hashset

0
推荐指数
1
解决办法
42
查看次数

标签 统计

hashset ×10

java ×9

hashmap ×2

unique ×2

arrays ×1

c# ×1

hashtable ×1

iteration ×1

list ×1

queue ×1

treeset ×1