Collections的常用方法
使用Java自带的方法可以极大的提升代码可读性、规范性。
-- 前言
1、新建一个list、set、map对象。
- Collections.emptyList();
- Collections.emptySet();
- Collections.emptyMap();
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
public static final <T> Set<T> emptySet() {
return (Set<T>) EMPTY_SET;
}
public static final <K, V> Map<K, V> emptyMap() {
return (Map<K, V>) EMPTY_MAP;
}
eg.
//创建list对象 List<Integer> list = Collections.emptyList(); //创建set对象 Set<Object> set = Collections.emptySet(); //创建map对象 Map<String, Object> map = Collections.emptyMap();
2、根据list元素某个字段排序
- Collections.sort(List list);
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
eg.
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(123);
person2.setAge(24);
person3.setAge(565);
list.add(person1);
list.add(person2);
list.add(person3);
Collections.sort(list, Comparator.comparing(Person::getAge));
}
3、根据list元素某个字段取出最小元素
- Collections.min(Collection coll, Comparator comp);
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
if(comp == null) {
return (T) min((Collection) coll, null);
}
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while(i.hasNext()) {
T next = i.next();
if(comp.compare(next, candidate) < 0) {
candidate = next;
}
}
return candidate;
}
eg.
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(123);
person2.setAge(24);
person3.setAge(565);
list.add(person1);
list.add(person2);
list.add(person3);
Person min = Collections.min(list, Comparator.comparing(Person::getAge));
}
4、根据list元素某个字段取出最大元素
- Collections.max(Collection coll, Comparator comp);
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
if(comp == null) {
return (T) max((Collection) coll, null);
}
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while(i.hasNext()) {
T next = i.next();
if(comp.compare(next, candidate) > 0) {
candidate = next;
}
}
return candidate;
}
eg.
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(123);
person2.setAge(24);
person3.setAge(565);
list.add(person1);
list.add(person2);
list.add(person3);
Person max = Collections.max(list, Comparator.comparing(Person::getAge));
}
5、替换全部元素
- Collections.replaceAll(List list, T oldVal, T newVal);
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) {
boolean result = false;
int size = list.size();
if(size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
if(oldVal == null) {
for(int i = 0; i < size; i++) {
if(list.get(i) == null) {
list.set(i, newVal);
result = true;
}
}
}else {
for(int i = 0; i < size; i++) {
if(oldVal.equals(list.get(i))) {
list.set(i, newVal);
result = true;
}
}
}
}else {
ListIterator<T> itr = list.listIterator();
if(oldVal == null) {
for(int i = 0; i < size; i++) {
if(itr.next() == null) {
itr.set(newVal);
result = true;
}
}
}else {
for(int i = 0; i < size; i++) {
if(oldVal.equals(itr.next())) {
itr.set(newVal);
result = true;
}
}
}
}
return result;
}
eg.
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
nums.add(123);
nums.add(23);
nums.add(23);
nums.add(23);
nums.add(356);
Collections.replaceAll(nums, 23, 44);
System.out.println(nums);
}
本站文章主要用于个人学习记录,可能对您有所帮助,仅供参考!

