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 have more enums with some values and I want asked you what the method is good to cache enum values:
For example:
public enum Animal {
Dog, Cat, Cow;
static Animal[] values;
static EnumSet<Animal> cachedAnimalsEnumSet;
static List<Animal> cachedAnimalsList;
static {
values = values();
cachedAnimalsEnumSet = EnumSet.allOf(Animal.class);
cachedAnimalsList = Arrays.asList(Animal.values());
}
}
Which is the best way:
values, cachedAnimalsEnumSet or cachedAnimalsList ?
Assuming that your purpose of caching is to avoid creation of new array every time anyone calls Animal.values(), and instead just use the cached value. I'd recommend using EnumSet for following reasons:
All basic operations are constant time.
There is not point in caching duplicate enum values, and the Set implementation takes care of it for you.
However, couple things to consider are that null values are not allowed in EnumSet (highly doubt you want to cache nulls). And second thing to be careful about is that EnumSet is not synchronized. So if you are going to have multiple threads access, and modify this cache, you'd have to wrap it using Collections.synchronizedSet method.
Related
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<>());
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.
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 3 years ago.
Improve this question
I know the underlying difference between the remove() method of the traditional collection like hashMap and concurrent collection like concurrentHashMap. In concurrentHashmap, JVM will match the key and the value both before removing the key value object which is required for the multithreading environment.
Is there any other difference between them?
I think you are asking why is there a second remove method in the concurrent map.
Map has:
V remove(Object key)
ConcurrentMap has an additional method:
boolean remove(Object key, Object value)
(In fact, Map has this as a default method since 1.8.)
In a non-concurrent Map, the two-arg form can easily written by composing a get followed by remove, at the cost of two lookups. Concurrent operations, however, do not compose. For concurrent maps you may see remove used in a loop similar to how compareAndSet is typically used.
You could perform the remove operation in a single operation through the normal collections interfaces if you first used entrySet. It's just not very convenient or obvious.
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. :/
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.