Collections(集合的工具类)
来源:互联网 发布:机构交易内部软件 编辑:程序博客网 时间:2024/06/05 23:51
集合的工具类(Collections)
笔试题目:说出Collection与Collections的区别?
Collection是一个单例集合的根接口,Collections是操作集合对象的一个工具类
Collections:常见方法:
1, 对list进行二分查找:
前提该集合一定要有序。
int binarySearch(list,key);
//必须根据元素自然顺序对列表进行升级排序
//要求list 集合中的元素都是Comparable 的子类。
int binarySearch(list,key,Comparator);
2,对list集合进行排序。
sort(list);
//对list进行排序,其实使用的事list容器中的对象的compareTo方法
sort(list,comaprator);
//按照指定比较器进行排序
3,对集合取最大值或者最小值。
max(Collection)
max(Collection,comparator)
min(Collection)
min(Collection,comparator)
4,对list集合进行反转。
reverse(list);
5,可以将不同步的集合变成同步的集合。
Set synchronizedSet(Set<T> s)
Map synchronizedMap(Map<K,V> m)
List synchronizedList(List<T> list)
package com.cn.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Author:Liu Zhiyong
* Version:Version_1
* Date:2016年7月19日15:47:06
* Desc:集合的工具类(Collections)
笔试题目:说出Collection与Collections的区别?
Collection是一个单例集合的根接口,Collections是操作集合对象的一个工具类
Collections:常见方法:
1,对list进行二分查找:
前提该集合一定要有序。
int binarySearch(list,key);
//必须根据元素自然顺序对列表进行升级排序
//要求list 集合中的元素都是Comparable 的子类。
int binarySearch(list,key,Comparator);
2,对list集合进行排序。
sort(list);
//对list进行排序,其实使用的事list容器中的对象的compareTo方法
sort(list,comaprator);
//按照指定比较器进行排序
3,对集合取最大值或者最小值。
max(Collection)
max(Collection,comparator)
min(Collection)
min(Collection,comparator)
4,对list集合进行反转。
reverse(list);
5,可以将不同步的集合变成同步的集合。
Set synchronizedSet(Set<T> s)
Map synchronizedMap(Map<K,V> m)
List synchronizedList(List<T> list)
*/
class Person{
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[" + this.name + this.age + "]";
}
}
class MyComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
return o1.age - o2.age;
}
}
public class Demo1 {
public static void main(String[] args) {
/*ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(111);
arrayList.add(101);
arrayList.add(1);
arrayList.add(24);
arrayList.add(44);
arrayList.add(66);
arrayList.add(66);
arrayList.add(66);
Collections.sort(arrayList);//排序, 对指定列表按升序进行排序。
*/
ArrayList<Person> arrayList = new ArrayList<Person>();
arrayList.add(new Person("习近平", 33));
arrayList.add(new Person("李克强", 22));
arrayList.add(new Person("木丁西", 18));
arrayList.add(new Person("刘先森", 23));
Collections.sort(arrayList, new MyComparator());
System.out.println(arrayList);
System.out.println("找到的索引值:" + Collections.binarySearch(arrayList, new Person("木丁西", 18), new MyComparator()));;
System.out.println("最大值" + Collections.max(arrayList, new MyComparator()));
System.out.println("最小值" + Collections.min(arrayList, new MyComparator()));
Collections.reverse(arrayList); //翻转,升序变成降序
System.out.println(arrayList);
List<Person> synchronizedList = Collections.synchronizedList(arrayList);//返回指定列表支持的同步(线程安全的)列表。
System.out.println(synchronizedList + " *************************");
}
}
0 0
- Collections(集合的工具类)
- 集合工具类Collections
- 集合工具类Collections
- Collections集合工具类
- 集合工具类 Collections
- (集合框架)collections工具类的常用方法
- Collections 操作集合的工具类
- 操作集合的工具类 collections
- 操作集合的工具类:Collections
- 操作集合的工具类:Collections
- Java 操作集合的工具类:Collections
- 集合框架的工具类----Collections
- 操作集合的工具类:Collections
- 操作集合的工具类:collections
- Java_集合的工具类—Collections
- 操作集合的工具类:Collections
- 操作集合的工具类:Collections
- Java 集合,Collections工具类的用法
- Unity中Generic动画类型的相关设置
- javascript 数组的splice方法
- <Android开源库> Realm For Android~ Scheme & JSON & Notification(译文)
- UML(一) 类图详解
- js中height、clientHeight、scrollHeight、offsetHeight区别
- Collections(集合的工具类)
- UNIX常用命令行指令(5)(访问权限和进程操作)
- 基数排序
- 【BZOJ1007】【HNOI2008】水平可见直线(计算几何 凸壳)
- Linux常用命令
- C语言--计算程序执行时间
- Arrays(数组的工具类)
- IIS7 运行Thinkphp,支持rewrite
- javascript打开新窗口的几种方式