classSolution {publicint[] relativeSortArray(int[] arr1,int[] arr2) {// brute force iterate arr2 , each arr2 element will scan arr1 once, so O(n*m)// put arr2 to hash table, build key with empty list// iterate arr1, put value into key arraylist, can't get the key, put into others arraylist// finally, iterate arr2 again, bring the correspond arraylist from hash tableList<Integer> res =newArrayList<>();Map<Integer,ArrayList<Integer>> map =newHashMap<>();for (int ar2 : arr2) {map.put(ar2,newArrayList<>()); }List<Integer> others =newArrayList<>();for (int ar1 : arr1) {if (map.containsKey(ar1)) {List<Integer> list =map.get(ar1);list.add(ar1); } else {others.add(ar1); } }Collections.sort(others);for (int ar2 : arr2) {List<Integer> result =map.get(ar2);res.addAll(result); }res.addAll(others);returnres.stream().mapToInt(i -> i).toArray(); }}