Build an intent An Intent is an object that provides runtime binding between separate components, such as two activities. The Intent represents an app’s intent to do something. You can use intents for a wide variety of tasks, but in this lesson, your intent starts another activity. In MainActivity, add the EXTRA_MESSAGE constant and the …
Why do we should use intValue() to compare?
The Integer.intValue() is used to get the primitive int value of Integer. The other test cases pass for you because the int values are low and the auto-unboxing feature of java does the value comparison for you. But for this one test case the int values are pretty large and hence it leads to object …
Continue reading “Why do we should use intValue() to compare?”
HashMap computeIfAbsent() method in Java with Examples
The computeIfAbsent(Key, Function) method of HashMap class is used to compute value for a given key using the given mapping function, if key is not already associated with a value (or is mapped to null) and enter that computed value in Hashmap else null. If mapping function of this method returns null, then no mapping …
Continue reading “HashMap computeIfAbsent() method in Java with Examples”
Why use Deque over Stack in Java
From JavaDoc for Stack, we could see: "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:" Deque<Integer> stack = new ArrayDeque<>(); Here are some answers from Why should I use Deque over Stack?. Inconsistency: …
How to get random number in a range in Java
Let’s use the Math.random method to generate a random number in a given range: public int getRandomNumber(int min, int max) { return (int) ((Math.random() * (max – min)) + min); } Why does that work? Well, let’s look at what happens when Math.random returns 0.0, it’s the lowest possible output: 0.0 * (max – min) …
Continue reading “How to get random number in a range in Java”
How to sort 2D array in Java
My code : Arrays.sort(intervals, new Comparator<int[]>(){ @Override public int compare( int[] a, int[] b ){ return a[0]==b[0] ? a[1] – b[1] : a[0] – b[0]; // return Integer.compare( a[0], b[0] ); } }); And here is an simple way: Arrays.sort(myArr, (a, b) -> a[0] – b[0]);
How to find Maximum AND XOR value of two elements in array?
I always feel confused when I face bit manipulation algorithm problems. Even the solution post in discussion forum is hard for me to understand. When I was trying to figure out the daily challenge, ofc it is a bit manipulation problem, I do more research because I still can’t understand the solution post in discussion …
Continue reading “How to find Maximum AND XOR value of two elements in array?”
Comparator Interface in Java
Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. Method 1: One obvious approach is to write our own sort() function using one of the standard algorithms. This solution requires rewriting the whole sorting code for different criterion like Roll …
Conversion between String and Integer in java
From String to Integer Integer.parseInt(my_string); From Integer to String Integer.toString(my_integer);
Difference between == and .equals() method in Java
In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two: Main difference between .equals() method and == operator is that one is method and other is operator. We can use == operators for reference comparison (address comparison) and …
Continue reading “Difference between == and .equals() method in Java”