Advertisement
CR7CR7

CheetSheetCollections

Nov 30th, 2022
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.BigInteger;
  3. import java.text.DecimalFormat;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class CheatSheet {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String[] array = new String[2];
  12.         Collections.reverse(Arrays.asList(array));
  13.  
  14.         Arrays.sort(array);                       // Sorts arr[] in ascending order
  15.         System.out.println("Modified arr[] : " + Arrays.toString(array));
  16.  
  17.         Arrays.sort(array, Collections.reverseOrder());     // Sorts arr[] in descending order
  18.  
  19.         System.out.print(array.toString().replace("[", "").replace("]", "")
  20.                 .trim().replaceAll(",", "").replaceAll("\\s+", " "));
  21.  
  22.         int[] arr = new int[2];
  23.         int number  = Integer.parseInt(Arrays.toString(arr).replaceAll("[\\[\\], ]", ""));
  24.  
  25.         List<Integer> list = new ArrayList<Integer>();
  26.         Collections.sort(list);
  27.         Collections.sort(list, Collections.reverseOrder());
  28.         System.out.println(list.stream().map(String::valueOf).collect(Collectors.joining(" ")));
  29.         System.out.println(String.join(" ", list.stream().map(String::valueOf).collect(Collectors.toList())));
  30.         System.out.println(list.toString().replaceAll("[\\[\\],]", ""));
  31.         System.out.println(String.join(System.lineSeparator(), array));
  32.  
  33.         int one = 1;
  34.         System.out.printf("%03d", one);   /// 001
  35.  
  36.         double n = Double.parseDouble(scanner.nextLine());
  37.         System.out.print(new DecimalFormat("0.####").format(n)+ " ");
  38. //0003.147896325
  39.         DecimalFormat decimalFormat = new DecimalFormat("#0.00");
  40.         DecimalFormat decFormat = new DecimalFormat("#.#####");
  41.         DecimalFormat deciFormat = new DecimalFormat("##.00");
  42.         DecimalFormat result1 = new DecimalFormat("0.##");
  43.  
  44.  
  45.             Map<String, List<Integer>> companyList = new LinkedHashMap<>();
  46.             companyList.entrySet().stream().forEach(company -> {System.out.println(company.getKey());
  47.             company.getValue().forEach(employee -> System.out.println("-- " + employee));});
  48.  
  49.             Map<String, Integer> mapList = new LinkedHashMap<>();
  50.             mapList.entrySet().forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
  51.  
  52.             Map<String, Integer> topThree =
  53.                 mapList.entrySet().stream()
  54.                         .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
  55.                         .limit(3)
  56.                         .collect(Collectors.toMap(
  57.                                 Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  58.  
  59. //        bestCandidate.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(1)
  60. //                .forEach(entry -> System.out.printf("Best candidate is %s with total %d points.\n", entry.getKey(), entry.getValue()));
  61.  
  62.             List<String> sortedPlayers = new ArrayList<>();
  63.             mapList.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(a -> sortedPlayers.add(a.getKey()));
  64.             Collections.reverse(sortedPlayers);
  65.  
  66.             List<StringBuilder> SBlist = new ArrayList<>();
  67.         Collections.sort(SBlist, new Comparator<StringBuilder>() {
  68.             @Override
  69.             public int compare(StringBuilder o1, StringBuilder o2) {
  70.                 return (o1).compareTo(o2);
  71.             }
  72.         });
  73.  
  74. //        String output = arraylist. toString(). replaceAll(“(^\\[|\\]$)”, “”);
  75. //        first we are removing first square bracket then we are removing the second square bracket
  76. //        and replacing i with empty string.
  77.  
  78.  
  79.         int m = Integer.parseInt(scanner.nextLine());
  80.         BigInteger bigInt = new BigInteger(String.valueOf(1));
  81. //        при m = 5, String.valueOf(m) и в multiply String.valueOf(6) result 5 * 6(на 6-та)
  82.         for (int i = 1; i <= m ; i++) {
  83.             bigInt = bigInt.multiply(BigInteger.valueOf(Integer.parseInt(String.valueOf(i))));
  84.         }
  85. //            bigInt = bigInt.multiply(BigInteger.valueOf(Integer.parseInt(String.valueOf(6)))); 6 на степен m
  86.  
  87.             BigDecimal sum = new BigDecimal("0");
  88.             String num = scanner.nextLine();
  89.             BigDecimal bdNUm = new BigDecimal(num);
  90.             sum = sum.add(bdNUm);
  91.  
  92. //        employeeList.sort(Comparator.comparing(CompanyRoster.Employee::getSalary).reversed());
  93. //        peopleList.sort(Comparator.comparing(OrderByAge.People::getAge));
  94. //        System.out.println(team.getUser().stream().collect(Collectors.joining(System.lineSeparator())));
  95.  
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement