Arrays
排序
Arrays.sort()
及其它的重载方法- 双轴快速排序
如果不指定外部比较器,则要求比较对象实现
Comparable
接口 ```java // 一个实现了 Comparable 接口的类 @AllArgsConstructor @NoArgsConstructor @Getter @Setter @ToString class Person implements Comparable{ private Integer age; // 比较大小 @Override public int compareTo(Person otherPerson) {
if (null == otherPerson) {
return 1;
}
if (this == otherPerson) {
return 0;
}
if (null == this.getAge()) {
return -1;
}
if (this.getAge().equals(otherPerson.getAge()))
return 0;
return this.getAge() > otherPerson.getAge() ? 1 : -1;
} }
private static void sourt1() {
List
Person[] personArrays = new Person[people.size()];
people.toArray(personArrays);
System.out.println("排序前" + Arrays.toString(personArrays));
// 不指定外部比较器的用法,要求比较对象实现 Comparable 接口
Arrays.sort(personArrays);
System.out.println("排序后:" + Arrays.toString(personArrays));
}
2. **指定外部比较器**
```java
// 如果没有实现 Comparable 接口
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
class Person {
private Integer age;
}
private static void sourt1() {
List<Person> people = new ArrayList<>(4);
people.add(new Person(30));
people.add(new Person(20));
people.add(new Person(10));
people.add(new Person(40));
Person[] personArrays = new Person[people.size()];
people.toArray(personArrays);
System.out.println("排序前" + Arrays.toString(personArrays));
// 如果自定义类没有实现 Comparable 接口,那么 sort 需要指定下 Comparator
Arrays.sort(personArrays, new Comparator<Person>() {
/**
* a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the second.
*
* 负数则 o1 < o2
* 0 则 o1 = o2
* 正数则 o1 > o2
*
* @param o1
* @param o2
* @return
*/
@Override
public int compare(Person o1, Person o2) {
if (o1 == o2) {
return 0;
}
if (null == o1.getAge()) {
return -1;
}
if (o1.getAge().equals(o2.getAge()))
return 0;
return o1.getAge() > o2.getAge() ? 1 : -1;
}
});
System.out.println("排序后:" + Arrays.toString(personArrays));
}
二分查找
要求
- 已排序
- 如果没有指定比较器,则要求查找对象实现 Comparable 接口
int result = Arrays.binarySearch(personArrays, new Person(20));
if (result < 0) {
System.out.println("未找到");
} else {
System.out.println("已找到,下标为: " + result);
}
看下底层源码
// Like public version, but without range checks.
// a:我们要搜索的数组
// fromIndex:从那里开始搜索,默认是0
// toIndex:搜索到何时停止,默认是数组大小
// key:我们需要搜索的值
// c:外部比较器
private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
T key, Comparator<? super T> c) {
// 如果没有指定外部比较器,则使用 key 自带的比较器
if (c == null) {
return binarySearch0(a, fromIndex, toIndex, key);
}
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
// 中间坐标
int mid = (low + high) >>> 1;
T midVal = a[mid];
int cmp = c.compare(midVal, key);
// c > key
if (cmp < 0)
low = mid + 1;
// c < key
else if (cmp > 0)
high = mid - 1;
// c == 0
else
return mid; // key found
}
return -(low + 1); // key not found.
}
拷贝
Arrays.copyOfRange()
Person[] people1 = Arrays.copyOfRange(personArrays, 0, personArrays.length);
源码,底层是调用
System.arraycopy()
public static <T> T[] copyOfRange(T[] original, int from, int to) {
return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
}
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
Collections
排序
二分查找
查最大、最小
- 只看一种
Collections.max()
, 提供两个重载方法// 利用泛型要求 比较对象
// 1. 继承于 Objects
// 2. 实现了 Comparable 接口
public static <T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll) {}
public static <T>
T max(Collection<? extends T> coll, Comparator<? super T> comp) {}
集合
线程安全
不可变
unmodifiable
开头的方法,将传入的集合类转变为不可变对象- 仅提供读取方法
- 其实就是将
set
、add
等修改类方法全体抛出异常throw new UnsupportedOperationException();
**
Objects
判断相同
Objects.equals()
- 比较对象
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
- 比较对象
Objects.deepEquals()
- 比较数组
public static boolean deepEquals(Object a, Object b) {
if (a == b)
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);
}
- 比较数组