1. Collections集合工具类
Collections类是java.util包中的一个集合的工具类,此类中提供了一些操作集合的静态方法,扩展了集合本身的功能。
这个类中的方法都是静态的,不需要创建对象。
Collections和Collection的区别:
1,Collections是java.util包中的一个类,是针对集合的一个工具类,提供一些静态方法对集合操作,实现集合功能的扩展。
2,Collection是java.util包中的一个接口,是所有集合的父接口,继承于它的主要有Set集合和List集合,Collection中提供集合共性方法声明。
1.1 Collections工具类部分方法
Collections工具类中的部分方法:
sort(list):sort方法对List集合进行排序(自然排序)。
sort(list, comparator):用指定比较器,对List集合进行排序。
max(collection):根据自然顺序,返回集合的最大值。
max(collection, comparator):根据比较器顺序,返回集合的最大值。
binarySearch(list, key):使用二分查找法搜索List集合,返回元素在集合中的位置,若不包含返回插入点,若超出则返回集合长度。
Collections工具类的代码示例:
-
import java.util.*;
-
-
class CollectionsDemo{
-
public static void main(String[] args){
-
binarySearchDemo();
-
}
-
-
public static void binarySearchDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
Collections.sort(list);
-
-
sop(list);
-
-
int index1 = Collections.binarySearch(list,"aaa");
-
sop("index1: "+index1);
-
int index2 = Collections.binarySearch(list,"aaaa");
-
sop("index2: "+index2);
-
int index3 = Collections.binarySearch(list,"zzz");
-
sop("index3: "+index3);
-
-
Collections.sort(list,new StrCom());
-
sop(list);
-
-
int index = Collections.binarySearch(list,"aaa",new StrCom());
-
sop("index: "+index);
-
}
-
-
public static void maxDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
-
sop(list);
-
-
String max = Collections.max(list);
-
-
sop("max: "+max);
-
-
String max1 = Collections.max(list,new StrCom());
-
-
sop("max1: "+max1);
-
}
-
-
public static void sortDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
-
sop(list);
-
-
Collections.sort(list);
-
-
sop(list);
-
-
Collections.sort(list,new StrCom());
-
-
sop(list);
-
-
}
-
-
public static void sop(Object obj){
-
System.out.println(obj);
-
}
-
}
-
class StrCom implements Comparator<String> {
-
public int compare(String s1,String s2){
-
if(s1.length()>s2.length())
-
return 1;
-
if(s1.length()<s2.length())
-
return -1;
-
return 0;
-
}
-
}
1.2 Collections工具类部分方法
Collections工具类的部分方法:
fill(list, A)方法:把集合中的所有元素,替换成指定元素A。
replaceAll(List, A, B)方法:把集合中,所有的旧的指定元素A,替换成新的指定元素B。
reverse(list)方法:反转集合中元素的顺序。
swap(list, i, j)方法:交换List集合中,位置i和位置j的两个元素。
shuffle(list)方法:对集合进行重新随机排序。 shuffle:洗牌。
代码示例:
-
import java.util.*;
-
-
class CollectionsDemo2 {
-
public static void main(String[] args){
-
shuffleDemo();
-
}
-
-
public static void shuffleDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
sop(list);
-
-
Collections.shuffle(list);
-
sop(list);
-
}
-
-
public static void reverseDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
sop(list);
-
-
Collections.reverse(list);
-
sop(list);
-
}
-
-
public static void replaceAllDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
sop(list);
-
-
Collections.replaceAll(list,"aaa","pp");
-
sop(list);
-
}
-
-
public static void fillDemo(){
-
List<String> list = new ArrayList<String>();
-
-
list.add("abcd");
-
list.add("aaa");
-
list.add("z");
-
list.add("kkkkk");
-
list.add("qq");
-
-
sop(list);
-
Collections.fill(list,"pp");
-
sop(list);
-
}
-
-
public static void sop(Object obj){
-
System.out.println(obj);
-
}
-
}
1.3 Collections工具类部分方法
Collections工具类的部分方法:
reverseOrder():返回一个比较器Comparator,此比较器的顺序与自然顺相反,即反转了自然顺序。
reverseOrder(cmp):返回一个比较器,此比较器的顺序与指定比较器cmp的顺序相反,即反转指定比较器的顺序。
synchronizedCollection(coll)方法:返回指定集合coll的线程同步(线程安全的)集合。
synchronizedList(list)方法: 返回指定集合list的线程同步(线程安全的)集合。
synchronizedMap(map)方法: 返回指定集合map的线程同步(线程安全的)集合。
synchronizedSet(set)方法: 返回指定集合set的线程同步(线程安全的)集合。
原理:将集合的所有方法加同一把锁后返回,实现了线程同步。
代码示例:
-
import java.util.*;
-
-
class CollectionsDemo3 {
-
public static void main(String[] args){
-
reverseOderDemo_2();
-
}
-
-
public static void reverseOderDemo(){
-
TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());
-
-
ts.add("a");
-
ts.add("bb");
-
ts.add("ccc");
-
ts.add("dddd");
-
ts.add("eeeee");
-
-
sop(ts);
-
}
-
-
public static void reverseOderDemo_2(){
-
TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new StrCom()));
-
-
ts.add("a");
-
ts.add("bb");
-
ts.add("ccc");
-
ts.add("dddd");
-
ts.add("eeeee");
-
-
sop(ts);
-
}
-
-
public static void sop(Object obj){
-
System.out.println(obj);
-
}
-
}
-
-
class StrCom implements Comparator<String> {
-
public int compare(String s1,String s2){
-
if(s1.length()<s2.length())
-
return -1;
-
if(s1.length()>s2.length())
-
return 1;
-
return 0;
-
}
-
}
2. Arrays工具类
java.util包中的Arrays类是操作数组的工具类,提供了一些静态方法。
数组变集合:
asList():将数组变成List集合。
其他方法还有:equals()、deepEquals()、binarySearch()、copyOf()、copyOfRange()、fill()、hashCode()、sort()、toString()等等。
代码示例:
-
import java.util.*;
-
-
class ArraysDemo{
-
public static void main(String[] args){
-
-
-
-
-
String[] arr = {"abc","cc","kkkk"};
-
-
List<String> list = Arrays.asList(arr);
-
sop("contains: "+list.contains("cc"));
-
-
-
sop(list);
-
-
-
Integer[] nums = {4,5,3};
-
List<Integer> li = Arrays.asList(nums);
-
-
sop(li);
-
-
}
-
-
public static void sop(Object obj){
-
System.out.println(obj);
-
}
-
}
集合变数组:Collection接口中的toArray()方法。
代码示例:
-
import java.util.*;
-
-
class ArraysDemo2{
-
public static void main(String[] args){
-
ArrayList<String> al = new ArrayList<String>();
-
-
al.add("abc01");
-
al.add("abc02");
-
al.add("abc03");
-
-
-
-
-
-
-
-
-
-
-
String[] arr = al.toArray(new String[al.size()]);
-
-
System.out.println(Arrays.toString(arr));
-
}
-
}
3. 其他常用类
3.1 System类
System类在java.lang包中,描述系统的一些信息。
System:类中的方法和属性都是静态的。
System.out:标准输出,默认是控制台。
System.in:标准输入,默认是键盘。
Properties getProperties():获取系统属性信息。
Properties是Map集合的子类,通过调用System的getProperties方法返回。
getProperties()方法:确定当前的系统属性,返回 Properties。
getProperty(key)方法:获取指定键指示的系统属性。
代码示例:
-
import java.util.*;
-
-
class SystemDemo{
-
public static void main(String[] args){
-
Properties prop = System.getProperties();
-
-
-
-
-
-
-
-
System.setProperty("myKey","myValue");
-
-
-
-
-
-
-
-
-
-
-
String value = System.getProperty("os.name");
-
System.out.println("value="+value);
-
-
-
-
-
String v = System.getProperty("haha");
-
System.out.println("v="+v);
-
}
-
}
3.2 Runtime类
Runtime类在java.lang包中。
该类中并没有提供构造方法。
说明不可以new对象,那么会想到该类中的方法都是静态的。
但是发现该类中还有非静态方法。
说明该类中肯定还会提供方法获取本类对象,而且该方法是静态的,并返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式来完成。
该方法是 static Runtime getRuntime(); //返回Runtime类型的本类对象。
调用Runtime类型对象的exec()方法,返回一个Process进程对象,exec()方法是非静态的。
Process对象的静态方法destroy()可以杀掉进程。
代码示例:
-
import java.util.*;
-
-
class RuntimeDemo {
-
public static void main(String[] args) throws Exception {
-
Runtime r = Runtime.getRuntime();
-
Process p = r.exec("calc.exe");
-
-
Thread.sleep(2000);
-
p.destroy();
-
-
Process pp = r.exec("notepad.exe SystemDemo.java");
-
-
}
-
}
3.3 Date类
Date类在java.util包中,Date类表示特定的瞬间,精确到毫秒。
SimpleDateFormat类对Date返回的日期进行格式化,便于观看。
调用SimpleDateFormat的format(date)方法对Date格式化。
SimpleDateFormat类 在text包中。
代码示例:
-
import java.util.*;
-
import java.text.*;
-
-
class DateDemo{
-
public static void main(String[] args){
-
Date d = new Date();
-
System.out.println(d);
-
-
-
SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日E hh:mm:ss");
-
-
-
String time = sdf.format(d);
-
-
System.out.println("time= "+time);
-
}
-
}
3.4 Calendar类
Calendar在java.util包中,通过指定字段,获取相应时间,如果只想获取年份,则只需要YEAR字段。
getInstance():获取一个Calendar实例,一个默认日历。
get(指定字段):根据指定字段,获取时间。
set(日期):设置当前日历为指定日期。
add(指定字段,增减量):把日历中的指定字段,增加或删除指定时间。
代码示例:
-
import java.util.*;
-
import java.text.*;
-
-
class CalendarDemo{
-
public static void main(String[] args) {
-
Calendar c = Calendar.getInstance();
-
-
sop(c.get(Calendar.YEAR)+"年");
-
-
sop((c.get(Calendar.MONTH))+1+"月");
-
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
-
sop("星期"+c.get(Calendar.DAY_OF_WEEK));
-
-
-
String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月"
-
,"十月","十一月","十二月"};
-
int index = c.get(Calendar.MONTH);
-
sop(mons[index]);
-
-
c.set(2014,2,17);
-
print(c);
-
-
c.add(Calendar.YEAR,-1);
-
print(c);
-
-
-
-
-
-
-
}
-
-
public static void print(Calendar c){
-
sop(c.get(Calendar.YEAR)+"年");
-
sop((c.get(Calendar.MONTH))+1+"月");
-
sop(c.get(Calendar.DAY_OF_MONTH)+"日");
-
}
-
-
public static void sop(Object obj){
-
System.out.println(obj);
-
}
-
}
3.5 Math类和Random类
Math类在java.lang包中。
Math类中提供了一些常用的方法,
例如,random方法产生随机数,ceil方法返回大于指定数的最小整数,floor方法返回小于指定数的最大整数,round方法返回指定数的四舍五入,pow方法进行幂运算。
代码示例:
-
class MathDemo{
-
public static void main(String[] args){
-
for(int x=0; x<10; x++){
-
double d3 = Math.random();
-
sop(d3);
-
}
-
}
-
-
public static void show(){
-
double d = Math.ceil(-16.34);
-
double d1 = Math.floor(12.34);
-
-
long l = Math.round(12.54);
-
sop("d="+d);
-
sop("d1="+d1);
-
sop("l="+l);
-
-
double d2 = Math.pow(2,3);
-
sop("2的3次幂:"+d2);
-
}
-
-
public static void sop(Object obj){
-
System.out.println(obj);
-
}
-
}
Random类在java.util包中,Random类的nextInt(num)可以返回一个0到num之间的随机数,包括首不包括尾。
代码示例:
-
import java.util.*;
-
class RandomDemo{
-
public static void main(String[] args){
-
Random r = new Random();
-
-
for(int x=0; x<10; x++){
-
int d = r.nextInt(10)+1;
-
sop(d);
-
}
-
}
-
}