Java book was unclear on these two topics [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 9 years ago.
Improve this question
The basic components of a class:
Is that they can be thought of as templates from which objects can be made?
Three things you can assign to a numeric variable:
Are they: Constants, numbers other variables.
Thanks! I'm trying to pick up the basics, but these two topics from my book have no clear cut answer just a header with an example.

1)I think you have the right idea, but really bad terminology. A class is an object that consists of some data and some code. You can create an instance of a class (called instantiating the class). Each instance has its own copy of all non-static variables in the class.
2)You can assign a number to a numeric variable. Yes, that number could be a constant, another variable, or some mathematical combination of other variables (like x+y, or 5+10, or 15*z). Or a numeric result of a function (say Math.max(x,y) which returns the bigger of x and y).

Related

Caching enum values [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 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.

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.

Java 1.7 HashMap Integer Key [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 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. :/

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.

Categories

Resources