Arrays

排序

  • Arrays.sort() 及其它的重载方法
  • 双轴快速排序
  1. 如果不指定外部比较器,则要求比较对象实现 Comparable 接口 ```java // 一个实现了 Comparable 接口的类 @AllArgsConstructor @NoArgsConstructor @Getter @Setter @ToString class Person implements Comparable { private Integer age;

    // 比较大小 @Override public int compareTo(Person otherPerson) {

    1. if (null == otherPerson) {
    2. return 1;
    3. }
    4. if (this == otherPerson) {
    5. return 0;
    6. }
    7. if (null == this.getAge()) {
    8. return -1;
    9. }
    10. if (this.getAge().equals(otherPerson.getAge()))
    11. return 0;
    12. return this.getAge() > otherPerson.getAge() ? 1 : -1;

    } }

private static void sourt1() { List people = new ArrayList<>(4); people.add(new Person(30)); people.add(new Person(20)); people.add(new Person(10)); people.add(new Person(40));

  1. Person[] personArrays = new Person[people.size()];
  2. people.toArray(personArrays);
  3. System.out.println("排序前" + Arrays.toString(personArrays));
  4. // 不指定外部比较器的用法,要求比较对象实现 Comparable 接口
  5. Arrays.sort(personArrays);
  6. System.out.println("排序后:" + Arrays.toString(personArrays));

}

  1. 2. **指定外部比较器**
  2. ```java
  3. // 如果没有实现 Comparable 接口
  4. @AllArgsConstructor
  5. @NoArgsConstructor
  6. @Getter
  7. @Setter
  8. @ToString
  9. class Person {
  10. private Integer age;
  11. }
  12. private static void sourt1() {
  13. List<Person> people = new ArrayList<>(4);
  14. people.add(new Person(30));
  15. people.add(new Person(20));
  16. people.add(new Person(10));
  17. people.add(new Person(40));
  18. Person[] personArrays = new Person[people.size()];
  19. people.toArray(personArrays);
  20. System.out.println("排序前" + Arrays.toString(personArrays));
  21. // 如果自定义类没有实现 Comparable 接口,那么 sort 需要指定下 Comparator
  22. Arrays.sort(personArrays, new Comparator<Person>() {
  23. /**
  24. * a negative integer, zero, or a positive integer as the
  25. * first argument is less than, equal to, or greater than the second.
  26. *
  27. * 负数则 o1 < o2
  28. * 0 则 o1 = o2
  29. * 正数则 o1 > o2
  30. *
  31. * @param o1
  32. * @param o2
  33. * @return
  34. */
  35. @Override
  36. public int compare(Person o1, Person o2) {
  37. if (o1 == o2) {
  38. return 0;
  39. }
  40. if (null == o1.getAge()) {
  41. return -1;
  42. }
  43. if (o1.getAge().equals(o2.getAge()))
  44. return 0;
  45. return o1.getAge() > o2.getAge() ? 1 : -1;
  46. }
  47. });
  48. System.out.println("排序后:" + Arrays.toString(personArrays));
  49. }

二分查找

  • 要求

    1. 已排序
    2. 如果没有指定比较器,则要求查找对象实现 Comparable 接口
      1. int result = Arrays.binarySearch(personArrays, new Person(20));
      2. if (result < 0) {
      3. System.out.println("未找到");
      4. } else {
      5. System.out.println("已找到,下标为: " + result);
      6. }
  • 看下底层源码

    1. // Like public version, but without range checks.
    2. // a:我们要搜索的数组
    3. // fromIndex:从那里开始搜索,默认是0
    4. // toIndex:搜索到何时停止,默认是数组大小
    5. // key:我们需要搜索的值
    6. // c:外部比较器
    7. private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
    8. T key, Comparator<? super T> c) {
    9. // 如果没有指定外部比较器,则使用 key 自带的比较器
    10. if (c == null) {
    11. return binarySearch0(a, fromIndex, toIndex, key);
    12. }
    13. int low = fromIndex;
    14. int high = toIndex - 1;
    15. while (low <= high) {
    16. // 中间坐标
    17. int mid = (low + high) >>> 1;
    18. T midVal = a[mid];
    19. int cmp = c.compare(midVal, key);
    20. // c > key
    21. if (cmp < 0)
    22. low = mid + 1;
    23. // c < key
    24. else if (cmp > 0)
    25. high = mid - 1;
    26. // c == 0
    27. else
    28. return mid; // key found
    29. }
    30. return -(low + 1); // key not found.
    31. }

拷贝

  • Arrays.copyOfRange()

    1. Person[] people1 = Arrays.copyOfRange(personArrays, 0, personArrays.length);
  • 源码,底层是调用 System.arraycopy()

    1. public static <T> T[] copyOfRange(T[] original, int from, int to) {
    2. return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
    3. }
  1. public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
  2. int newLength = to - from;
  3. if (newLength < 0)
  4. throw new IllegalArgumentException(from + " > " + to);
  5. @SuppressWarnings("unchecked")
  6. T[] copy = ((Object)newType == (Object)Object[].class)
  7. ? (T[]) new Object[newLength]
  8. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  9. System.arraycopy(original, from, copy, 0,
  10. Math.min(original.length - from, newLength));
  11. return copy;
  12. }

Collections

排序

二分查找

查最大、最小

  • 只看一种
  • Collections.max(), 提供两个重载方法
    1. // 利用泛型要求 比较对象
    2. // 1. 继承于 Objects
    3. // 2. 实现了 Comparable 接口
    4. public static <T extends Object & Comparable<? super T>>
    5. T max(Collection<? extends T> coll) {}
  1. public static <T>
  2. T max(Collection<? extends T> coll, Comparator<? super T> comp) {}

集合

线程安全

不可变

  • unmodifiable 开头的方法,将传入的集合类转变为不可变对象
    • 仅提供读取方法
    • 其实就是将 setadd 等修改类方法全体抛出异常 throw new UnsupportedOperationException();

**


Objects

判断相同

  • Objects.equals()

    • 比较对象
      1. public static boolean equals(Object a, Object b) {
      2. return (a == b) || (a != null && a.equals(b));
      3. }
  • Objects.deepEquals()

    • 比较数组
      1. public static boolean deepEquals(Object a, Object b) {
      2. if (a == b)
      3. return true;
      4. else if (a == null || b == null)
      5. return false;
      6. else
      7. return Arrays.deepEquals0(a, b);
      8. }