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 9 years ago.
Improve this question
Some programming languages such as python, Java and C++11 have hash tables (although sometimes under different names with extended functionality) as part of their standard library. I would like to understand from a high level algorithmic point of view what has been implemented. Specifically:
What function of the keys is used to give the location to place the data (i.e. what is the hash function that is used)?
Which algorithms do they use for resolving collisions? As an example, do any of them use simple chaining?
Is there any use of randomness to pick the hash functions?
For Java,
How are the hash functions themselves computed?
They are implemented by the class itself with int hashCode()
Which algorithms do they use for resolving collisions? As an example, do any of them use simple chaining?
Typically simple chaining. Java 8 will support trees for collisions of String.
Is there any use of randomness to pick the hash functions?
No, except for String elements/keys to avoid DOS attacks.
Related
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 3 years ago.
Improve this question
Why does Java not support a nice and readable direct print-out for arrays?
I do know that Arrays.toString can be used to get that, and I also know what the hashcode output means (as explained in Why does the toString method in java not seem to work for an array).
But I am asking myself why the developers did not choose to make arrays directly print the readable representation.
Is it technically impossible? Would it impact performance?
arrays in java are a special case. There is no java source code for an array class and arrays are implemented in the JVM directly.
Given that, arrays don't have any methods themselves and they do not override any of the default Object methods (Yeah they're treated as objects but they're handled with special bytecodes).
As for why the decided to do that it probably has to do with allowing more flexibility with creating arrays of different types and dimensions, maybe processing speed as well.
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 6 years ago.
Improve this question
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in Java?
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 8 years ago.
Improve this question
hi what is the actual meaning of utility function in java?
The term utility function was used by Joshua Bloch in the book Effective Java to describe the methods on classes such as Arrays, Objects and Math.
The approach predates Java, and is defined by Wikipedia - Utility Class as a set of methods that perform common, often reused functions. I would go on to also point out that the functions tend to require no Object level state, that is they tend to be global functions. And that is why, in Java they tend to become implemented as static methods on a container class. As that way there is no need to instantiate the class first, and it implies through convention that the method should not have any side effects. Of course counter examples of this probably exist, but not to my knowledge within the Java Core libraries.
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 8 years ago.
Improve this question
In many places Java utilizes the approach to have both, for example, Collection and Collections class.
Collection is interface, it defined some methods.
Collections class also provide some method.
Why didn't they choose to place all methods inside the interface?
because the names are too puzzled word. Collection is like Collections.
I know the historical reason. like interrupt() and interrupted() , because java must fit to old version, the methods' names are likely, make developer difficult to write and read.
But the collection framwork must have reason in this way.
For starters, an interface cannot have static methods. Note: until Java 8.
Arguably, some of the static methods of Collections should have been made instance methods of Collection, but that would create a lot of "clutter". Plus, extra work for implementations not derived from AbstractCollection etc.
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 9 years ago.
Improve this question
Kryo is really fast and small. what's the secret here?
I have diving into its code for a while, but still need some guidance.
Thanks.
From their page:
The 2.22 release fixes many reported issues and improves stability and
performance. It also introduces a number of new features, most notably
that it can use Unsafe to read and write object memory directly. This
is the absolute fastest way to do serialization, especially for large
primitive arrays.
It uses direct bytecode-level access to the fields - sun.misc.Unsafe or ASM library. Kryo was fast even before introducing unsafe usage. The general answer, I think, is that performance is their highest priority. Java's reflection is not that slow when used carefully - i.e. when the java.lang.Field and java.lang.Method are cached. I set up an experiment which sorted an array with two different comparators - one was using direct field access and the other was using cached fields. There was only 2x difference, which means unnoticeable in context with IO.
FieldSerializer:
By default, most classes will end up using FieldSerializer. It
essentially does what hand written serialization would, but does it
automatically. FieldSerializer does direct assignment to the object's
fields. If the fields are public, protected, or default access
(package private), bytecode generation is used for maximum speed (see
ReflectASM). For private fields, setAccessible and cached reflection
is used, which is still quite fast.