Dot product of two HashMaps in java? [closed] - java

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);
}
}

Related

Fetching immediate lower and higher value of a given number from a List<Integer> [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
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.

stuck with this while loop java [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 4 years ago.
Improve this question
I have been working with while loops however I cant seem to understand how the remainder works inside this code.
int a = 10;
while( a <= 1000 && a % 100 != 0){
System.out.println("a = " + a);
a = a + 10;
}
a & 100 != 0
Performs bitwise and , then compares the result to 0. It will be false even in the first iteration, since 10 & 100 = 0

Java 8 functional interfaces naming convention? [closed]

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;

I want to get each array values separately for calculation [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 4 years ago.
Improve this question
I'm java beginner, This is my code.
int [] arr = {1,3,4,5,5,6};
I want to get each array values separately for calculation. can you please help me.
For example: I want first two digits (array index 0 and 1) only how to get that.
to get the first and second item, do like this:
int [] arr = {1,3,4,5,5,6};
System.out.println("pos 0: "+arr[0]+" pos 1: "+arr[1]);
by doing arr[n-1], you select the 'n'th value in the array.
now, if you want to multiply these first two values..:
int result = arr[0] * arr[1]; // 1 * 3
System.out.println(result) // 3
For example: I want to multiply first two digit (1*3) after that (4*5) like that.. so I want to get these values separately.
To do this,
int [] arr = {1,3,4,5,5,6};
int ans;
for(int i=0;i<6;i+=2){
ans = arr[i]*arr[i+1];
System.out.println(""+ans);
}

How can I make all initialized integers (instance varibales) into an Array in Java? [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 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.

Categories