Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Is it possible to Hash using bcrypt in java and again unhash it? Is it even possible? I was trying something new but I do not know anything about Unhashing (if it's possible). Can someone give me an example of somekind if it's possible?
Is there a hashing algorithm which is reversible if the key is known?
Hashing is a one-way operation by definition. You cannot retrieve source value from its cache. Actually there can be many values that produce the same hash but algorithm that translates hash to value should not exist theoretically (by definition).
If you're talking about overriding hashCode(), if your object can have more than 2^32 states then it's impossible to make any reversible hash. There are only 2^32 possible hash values (because hashCode() returns int), so only 2^32 different states can be represented.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm using Point3D.
I have a list full of Point3Ds. I need to create a NEW Point3D at some point, and check if the list contains it.
Will this list ever contain it as it is technically a different reference/object BUT has identical values?
I understand this is a lack of fundamental knowledge in my Java.
A List doesn't care what you put in it - put ten things in, get ten things out.
A Set however will generally only have unique values in it (using .equals() and hashCode() and in some sets other things - e.g. Comparator).
Normally I'd say "to the javadoc" however in this case it has a case of cut/paste'ism from Point3D JavaDoc
equals
public boolean equals(Object obj)
Returns a hash code value for the point.
Overrides:
equals in class Object
Returns:
a hash code value for the point.
So assuming it does what most people expect if you have two points generated with identically valued doubles then yes it'll be as you expect.
HOWEVER, floating point numbers you can easily be "very very close" when you expect to be identical (due to the representation of the value as FP), and remember there are 3 doubles in a Point3D [so 3 potential lots of small error] - so to be safe typically you might decide things are the same within some small distance see javadoc for distance rather than relying on exact matching.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is there any Similarities between data type Dictionary and data type Array. If there are any what are them?
A dictionary maps strings to values.
An array maps integers to values.
That is about it!
So, seriously: in the end, both data structures map a "key set" to values. For dictionaries, the keys can of (almost) arbitrary type, without any constraint on them (besides being "hash able"). Whereas an array maps a consecutive range of ints to values.
From that point of view, arrays and dicts/maps are doing the same thing, but in the end, how you use them is very different.
And just for completeness: of course, the "underlying" similarity is that both are "containers": objects that "own" multiple other objects.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
My question is, if I want to check a character (only one character to check) of a string in a particular index which method is very efficient charAt() or startsWith(). I mean in comparison of time complexity as far I can guess startsWith() gets more time than charAt(). Because startsWith() needs to check a set of characters, but charAt() just need to check only one character.
Now tell me your opinion… what you think about which is efficient to use to check only one character?
Both methods can be used to check a specific character for its value.
charAt() directly returns the char at the requested index, startsWith(prefix, index) will return true if you provide corresponding arguments.
The major difference is that the second approach has a bit more of overhead.
So, theoretically option 1 has slightly better performance. But beyond that, you rather pick option 1 because that just does what you want in the most clear way.
The really important difference is not about performance, but about your code communicating your intent. So, albeit is possible to use startsWith() to do what you want, it is simply counterintuitive to use it that way.
if I want to check a character(Only one character to check) of a string in a particular index
You answered by yourself. If you need to check that in a particular index, you can't use startsWith() , because you can't choose the index.
They do different jobs, so , based on your question, always use charAt()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm getting the Md5 of a file using Apache DigestUtils as follows:
public static String getMd5(File file) throws Exception
{
FileInputStream fis = null;
String md5 = "";
fis = new FileInputStream(file);
md5 = DigestUtils.md5Hex(fis)
IOUtils.closeQuietly(fis);
return md5;
}
This Md5 is being used as a key. I am doing a check for uniqueness (because of possible collisions), however, if it is not unique, how do I make it unique?
Thanks in advance!
Actually there is nothing you can do to make a hash function unique (obvious, because it maps large data to small one). For MD5, these collisions don't happen by chance for a reasonable number of files, but someone who wants to break your program can construct files with same MD5 hash (see for example http://www.mathstat.dal.ca/~selinger/md5collision/). If you want to avoid this, I would suggest that you use a hash functions that is considered more secure, like SHA-256. If you really have to deal with a hash function with collisions, your data structure that uses this hash as a key needs mechanisms to handle this situation (e.g. secondary hashing or using lists to store items with same hash).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Googling didn't help me out so I came here. What is the use of int hash32() method in String? As the name suggest it looks like for some hashing but how and where exactly it is used?
The answer is right here. Here are some choice snippets:
Java SE 7u6 introduces an improved, alternative hash function...
The alternative hash function improves the performance of these map implementations when a large number of key hash collisions are encountered.
The alternative hash function is only applied to keys of type String.
As stated by #Slanec, Java 8 made some changes to HashMap that reduced the performance drop for heavy collisions to O(lg n) instead of O(n) for Comparable keys, so there was no longer a need for alternative String hashing.