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 4 years ago.
Improve this question
Does such convention exist?
For example, I have a predicate: Predicate<Integer> predicate = i -> (i < 1); or BiFunction<String, String, String> function = (x, y) -> x + y;. How should I name it so the variable's type and sense would be clear?
There are no specific naming conventions for functional interfaces. Just name them according to what they do:
Predicate<Integer> smallerThanOne = i -> (i < 1);
BiFunction<String, String, String> concatenate = (x, y) -> x + y;
Related
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 a number with precision (9,4) ex:4.0000 //4 but that comes on a string without decimal point (40000). So how on java take that string and convert to 4.0000 in double or Bigdecimal?
You can use the BigDecimal(BigInteger, int) constructor. The second argument indicates the scale of the number:
public static BigDecimal convert(String num, int scale) {
BigInteger bi = new BigInteger(num);
return new BigDecimal(bi, scale);
}
Which you can then call like this:
BigDecimal bd = convert("40000", 4);
System.out.println(bd.toPlainString()); // 4.0000
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
List<Integer> list = Arrays.asList(1, 2, 4, 5,7, 8, 9);
int n = 3;
When user input integer as 3 then program should result as 2 and 4 in an above given list.
You can use Collectors.partitioningBy:
Map<Boolean, List<Integer>> m = list.stream()
.filter(i -> i != n)
.collect(Collectors.partitioningBy(i -> i > n));
Integer lower = m.get(false).get(m.get(false).size() - 1);
Integer higher = m.get(true).get(0);
Additional check whether both m.get(false) and m.get(true) are not empty might be needed.
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 3 years ago.
Improve this question
Suppose there are two HashMaps as follows:
HashMap<String, Integer> h1 = [{"a":1}, {"b":2}, {"c":3}];
HashMap<String, Integer> h2 = [{"k": 1}, {"f": 4}, {"g":5}, {"a":10}]
The multiplication is just like a simple vector multiplication, in this case it will return
1*10 + 2*0 + 3*0 = 10.
That is if the keys are same, then only multiply the two respective values.
Result -> It should return an integer.
int result = 0;
for(String s : h1.keySet()){
if(h2.containsKey(s)){
result = result + h2.get(s) * h1.get(s);
}
}
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 4 years ago.
Improve this question
Given this code:
int p,k=8;
p=k*(++k-8);
System.out.println(p);
when ++k is evaluated k=9 and then that becomes k*(9-8) giving 9*1
int p,k=8;
p=(++k-8)*k;
System.out.println(p);
But this gives 9 as output
You have a multiplication with
left side: k
right side: (++k-8)
As you correctly stated, braces have precedence. However, your program still runs "from left to right". So first the left side is evaluated, which is k = 8. Afterwards the right side is evaluated, which is (++k-8) = 1.
Now we have determined both sides and can multiply them together: 8*1 = 8.
this is the class file your code compiled:
int k = 8;
byte var10000 = k;
int k = k + 1;
int p = var10000 * (k - 8);
System.out.println(p);
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
If I have some initialized integers which are instance variables, for example:
int x = 0;
int y = 0;
Is there a way I could create an array with all of the integers without doing:
int[] array = {x, y};
I would like to not have to add the integers into the array manually.
If the variables are initialized to 0, then just create the array with the appropriate size:
int[] array = new int[10];
All elements in the array are initialized to 0.