Advertisement
selebry

выфвфы

Jan 24th, 2023 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. package ru.minusd.ticket06;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.TreeMap;
  7.  
  8. public class ToGuavaSort4 {
  9. public static void main(String[] args) {
  10. String[] arr = {"Farm", "Zoo", "Car", "Apple", "Bee", "Dog", "Golf", "Zoo", "Zoo", "Bee", "Apple"};
  11. System.out.println(Arrays.toString(guavaSort(arr)));
  12.  
  13. }
  14.  
  15. public static String[] guavaSort(String[] arr) {
  16. TreeMap<String, Integer> map = new TreeMap<>();
  17. for (String elem : arr) {
  18. if (map.containsKey(elem)) {
  19. map.put(elem, map.get(elem)+1);
  20. } else {
  21. map.put(elem, 1);
  22. }
  23. }
  24.  
  25. String[] result = new String[arr.length];
  26. int i = 0;
  27. for (String elem : map.keySet()) {
  28. for (int j = 0; j < map.get(elem); j++) {
  29. result[i] = elem;
  30. i++;
  31. }
  32. }
  33. return result;
  34. }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement