使用Python和Django,我会让我的用户给他们的朋友提供基于pdf的礼物,所述朋友可以通过电子邮件链接进入我的网站来申请pdf.
这是计划
用户给他的朋友一个给,给朋友发电子邮件
在后台,将保存一个礼品模型,该模型将在保存时包含唯一生成的哈希代码.
朋友收到电子邮件,提供下载pdf的链接,就像(www.mydomain.com/gift/<hash code here>)
单击邮寄链接时,系统会检查是否存在具有给定哈希码的礼品模型.
如果是这样下载开始,否则404.
这是解决这个问题的聪明方法吗?如果是这样,你会推荐什么样的散列函数?有趣的是,/ gift /对公众开放,如果以某种方式幸运地找到链接,任何人都可以声称它.我打算用接收器的姓氏加上礼物模型的pk来提供哈希函数
嗨我正在使用一个由HashMap支持的Set来跟踪我已经在图表中遍历的边缘.我计划通过添加存储在每个边缘的数据的哈希码的结果来键入该集合.
v.getData().hashCode() + wordV.getData().hashCode()
Run Code Online (Sandbox Code Playgroud)
但是当使用contains来检查边缘是否在集合中时,这有多可靠?我不能假设得到误报吗?无论如何要克服这个?
引起我关注的确切陈述是:
edgeSet.contains(v.getData().hashCode() + wordV.getData().hashCode())
Run Code Online (Sandbox Code Playgroud)
谢谢!
哦顺便说一句,我正在使用Java.
编辑:
我应该在这个问题上明确这一点.在我的图形中没有边缘对象,有顶点对象,每个顶点对象包含更多顶点对象的列表,即边缘.因此,我认为结合您的回答后面的问题是:
我可以使用Set来存储信息的引用而不是对象....?即我可以存储为顶点的数据对象添加两个哈希码的结果吗?
EDIT2:
我确实使用Java库作为我的hashmap,我将其声明如下:
Set<Integer> edgeSet = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
Run Code Online (Sandbox Code Playgroud) 我遵循以下Vertex类,它实现了equals,hashCode和compareTo方法.即使这样,我的HashMap也会返回null.我不知道为什么?
public class Vertex implements Comparable<Vertex> {
int id;
public Vertex(int number) {
id = number;
}
public boolean equals(Object other) {
if (other == null)
return false;
else if (other.getClass() != this.getClass())
return false;
else {
Vertex copy = (Vertex) other;
if (copy.id == this.id)
return true;
else
return false;
}
}
public int hasCode() {
int prime = 31;
int smallPrime = 3;
int hashCode = this.id ^ smallPrime - prime * this.hasCode();
return hashCode;
}
public int …Run Code Online (Sandbox Code Playgroud) 我查看源代码Arrays.hashCode(char[] c)
我不是很确认它适用的算法在所有情况下都能很好地工作.
public static int hashCode(int a[]) {
if (a == null)
return 0;
int result = 1;
for (int element : a)
result = 31 * result + element;
return result;
}
Run Code Online (Sandbox Code Playgroud)
这里实现的散列函数是否真正均匀地分配了所有输入数组.为什么我们在这里使用prime 31.
我想知道为什么new Integer(i).hashCode()或new Long(i).hashCode()返回,i但是当hashCode()被其他一些对象调用时new Double(345).hashCode(),它会返回一个随机数.为什么?
我有以下对象集合:
Set<MyClass> test = new LinkedHashSet<MyClass>();
Run Code Online (Sandbox Code Playgroud)
但MyClass不会覆盖hashcode和equals方法.
上面的集合只能有唯一的对象,即使MyClass没有覆盖hashCode和equals方法吗?
I have read that to calculate the hashcode for multidimensional array, one has to use Arrays.deepHashCode() method instead of Arrays.hashCode() method, but I don't really understand the technical reason behind it. Could someone please explain it to me?
Object[][] one = {
{1, 2, 3},
{11, 22, 33}
};
int two = Arrays.deepHashCode(one);
int three = Arrays.hashCode(one);
System.out.println("two " + two);
System.out.println("three " + three);
Run Code Online (Sandbox Code Playgroud)
Result:
two 997365
three 312355675
Run Code Online (Sandbox Code Playgroud) Geohash字符串是我的稀疏逻辑回归模型中的一个特征.所以我使用java字符串hashCode在geohash字符串上生成int值以获取功能ID.但我发现hashCode方法在类似的geohash字符串上表现不佳.它导致不同的功能具有相同的功能ID,即使功能类似,模型优化也可能不好.例如,那些类似的geohash字符串对具有相同的hashCode.
<"wws8vw", "wws8x9">
"wws8vw".hashCode() = -774715770
"wws8x9".hashCode() = -774715770
<"wmxy0", "wmxwn">
"wmxy0".hashCode() = 113265337
"wmxwn".hashCode() = 113265337
Run Code Online (Sandbox Code Playgroud)
我猜它在geohash生成器方法和java hashCode方法之间有一些关系.那么,任何人都可以向我解释真正的原因以及如何减少geohash字符串上的冲突?
我有一个枚举类如下.
public enum Item {
COKE("Coke", 25), PEPSI("Pepsi", 35), SODA("Soda", 45);
private String name;
private int price;
private Item(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public long getPrice() {
return price;
}
}
Run Code Online (Sandbox Code Playgroud)
以及如下定义的散列映射
private Map<Item, Integer> inventory = new HashMap<Item, Integer>();
Run Code Online (Sandbox Code Playgroud)
我是否需要重写了hashCode和枚举等于Item类inventory.put()和inventory.get()正常工作?如果没有,为什么?
public class Lemon{
public int Ounces;
public string Color;
public override int GetHashCode() => (Ounces, Color).GetHashCode();
}
Run Code Online (Sandbox Code Playgroud)
我很好奇它是如何工作的。该(Ounces, Color)类似于一个匿名类型,但不共享相同的语法。而且,如果它是匿名类型,那么我仍然不确定如何获得唯一的哈希。
到相关的.net源代码的链接将很棒。由于我不确定(Ounces, Color)最终被编译成哪种类型,因此很难发现。