```
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Map<Integer, Integer> m = new HashMap<>();
// go through each element in nums and set its location in HashMap
for(int i =0;i<nums.length;++i)
m.put(nums[i],i); //since every element is unique, there is no need (getOrDefault)
//scan each element in the first array
for(int i=0;i<findNums.length;++i)
{
int minIndex =-1; //initially, set the finding index to be -1
int index = m.get(findNums[i]); //findout the corresponding index in the second (nums) array.
while(++index < nums.length)
{
if(nums[index]>findNums[i])
{
minIndex =index;
break;
}
}
if(minIndex ==-1) findNums[i] = -1;
else findNums[i] = nums[minIndex];
}
return findNums;
}
```