为什么Java不提供函数来获取HashSet类似的键/值对Hashtable?每次你需要得到某些东西时,必须迭代它似乎是一种真正的痛苦.或者有更简单的方法吗?
我知道HashSet.contains()方法使用.equals方法来检查相等性,因为它检查指针以查看它们是否相等.
我需要它来检查指针上的实际对象是否相等 - 在我的具体情况下,我需要查看HashSet中是否已经存在打开的"Node"(一个int []数组).这对我的搜索算法至关重要,因此我的双向迭代深化搜索的实现并不那么天真.
如果可能的话,我仍然喜欢线性搜索时间,或者我应该使用不同的类?
谢谢你的帮助.
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编写一个程序,我想使用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) 我将整数对类设置如下:
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中没有重复?
我有一个从数据库中提取州和城市列表的方法.州是独一无二的,但该州可能有许多城市.我的方法目前所做的是将每个州和城市对作为单独的项目返回.我需要它做的是拥有许多城市的州.
目前退货
辛辛那提
哦 - 克利夫兰
哦,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) 当我的文本文件包含:
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) 我已经知道当我们有Key-Value对时我们使用HashMap,而当我们不需要重复数据时我们使用HashSet.但是我总是对Hashtable发挥作用感到困惑.它是一个完全不同的数据结构吗?或者HashMap和HashSet都使用哈希函数在Hashtable中存储数据?彻底的解释将非常有用.
如何知道什么已被淘汰HashSet?
我有一个阵列 int [] x = {2, 4, 4, 5};
当我隐身时, HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(x));
我怎么知道哪些元素被淘汰x了set?
给出以下代码:
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)