Java 1.7 HashMap Integer Key [closed] - java

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 5 years ago.
Improve this question
I'm using an HashMap<Integer, MyObject> collection to store a list of few couples of values.
Is it safe use Integer as key? the behavior is the same both on put and get method?

yes it's safe. Integer is just a wrapper class for int.
i know this from own experience. created a captcha functionality that way. (user requests a random integer. my prog creates a captcha-pic and saves the answer[value] and random integer id[key] in a hashmap. btw if you want to create a "cache" use java's LinkedHashMap. it has a protected boolean method called "removeEldestEntry(Map.Entry eldest)" which is called during a put-event. if you override it with, eg "return size() > MAX_SIZE;" it will delete the oldest entry (or the entry based on oldest access - there's an option in the constructor) if the condition you provided returns true). hope this helped. :/

Related

How do I create a HashSet of unique values without comparison in Java [closed]

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 2 years ago.
Improve this question
I have a complex Java Bean with ~20 attributes.
In my business logic, I am generating around 10^5 unique instances of this Bean.
The bean has a complex and performance costly equals method.
My API signature is old and I can return the data only in a HashSet data structure.
I require to generate this HashSet from the unique instances without invoking the equals method of the bean to have the flow optimized.
Is it possible?
It is guaranteed, the data to be inserted in HashSet are unique beforehand.
IdentityHashMap uses == instead of equals() to compare keys when two of them have the same hash code.
You can create a set from it:
Set<E> set = Collections.newSetFromMap(new IdentityHashMap<>());

Collections.emptySet() vs new HashSet<>() [closed]

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 2 years ago.
Improve this question
How can I validate if the Set is empty set or new HashSet<>() ?
Tried two approaches -
size
CollectionUtils.isEmpty()
Both result in same value.
Of course, a fresh new HashSet<>() is empty, and so is Set.of() or Collections.emptySet(). The point is: Both are empty sets, I have no idea why you would want to tell the difference between these two.
The one difference is that new HashSet<>() is empty now but may not be empty later (it can be changed; you can add things to it), whereas as per the spec, the result of Set.of() or Collections.emptySet(), they are empty now and will be empty later: You can't add anything to them, calling .add on them will cause a runtime exception.
That's tantamount to asking: How do I know if it is immutable. You unfortunately basically can't, so that goes right back to: Why would you need to know?
Collections.emptySet() returns a static class EmptySetwithin java.util.Collections but new HashSet<>() returns a java.util.HashSet class. Both collections will be empty, i.e., size = 0 after instantiated but you can distinguish those two by calling .getClass() which will return:
class java.util.Collections$EmptySet
class java.util.HashSet
Use .getClass to see the different implementation that was used.
I got one utility method that can be used to perform the operation -
Collections.EMPTY_SET.equals(mySet)
Thanks all for your answers.

Why do we really need Comparator in Java? [closed]

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 5 years ago.
Improve this question
My questions is basically divided into two sub-questions:
Comparable talks about natural ordering. Who is stopping us from implementing a non-natural ordering login in the compareTo method ?
Comparator can do the same stuff as Comparable (ASC or DESC sort). So the only reason it exists is because that if we have a third party class which we cannot change (make it implement Comparable) then we can externalise the sorting logic using Comparator. Is this correct ?
A class can only have one compareTo method but you can define as many comparators as you like for it. This is useful to define different orderings which is not such an uncommon requirement.

LinkedHashMap order [closed]

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 9 years ago.
Improve this question
Is order still guaranteed when the map objects are accessed as shown below at location 1 and 2?
//....
public void firstMethod(){
Map<K,V> sortedMap=new LinkedHashMap<K,V>();
sortedMap.put(h,g);
//....
Map<K,V> anotherMap=someOtherMethod(sortedMap);
// order of anotherMap when read ...2
}
public Map<K,V> someOtherMethod(Map<K,V> someMap){
someMap.put(a,b);
//order of someMap when read ...1
//.....
return someMap;
}
//....
If the concrete instance of your Map object is a LinkedHashMap yes. It does not matter what you do with it. The object will keep it's data ordered and the implementation does not change if you cast to just Map or even Object or pass it to methods. It will stay internally a LinkedHashMap. You might no longer see that it is one if you cast it to Object.
Assuming that you don't know the source code, the only thing that is not guaranteed is that someOtherMethod returns your LinkedHashMap. It could also re-order it.
A method should not be trusted unless it specifies those things. But since you know the sourcecode here, you have the guarantee that it is your LinkedHashMap in perferct order.
As per the docs:
Hash table and linked list implementation of the Map interface, with predictable iteration order.
So even after things are inserted and removed, the order should persist through whatever you want to do to it.

What is data structure for storing object and counters of object [closed]

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 9 years ago.
Improve this question
I need to store some entity object + counter of each groups entities objects.
How can I do this?
If the counter variable is not a part of the object itself, you can use Map<Object,Integer> as other answers suggest. However, keep in mind that you can use any collection or list if the counter is a part of the object data. Then you will update the counter with setters. Alternatively, updating the counter within particular constructors of various classes may also be a preferred way.
class Data
{
int counter = 0;
Data()
{
counter++;
}
}
You can use a Map
Map<Object, Integer> map = new HashMap<Object, Integer>();
Here, Object is your key and Integer is your count .
Your keys should be unique in the HashMap.
As it uses hashing, it will help in efficient retrieval of your objects while searching.
Any Map will do. Alternatively if you're not interested in learning about new classes simply use a matrix.
int matrix[][]=new int[10][10];

Categories

Resources