集合框架
List:有序
ArrayList:底层是长度可变的数组,便于查找和随机访问
LinkedList(Node):底层是双向链表,便于插入和删除
Set:不重复
HashSet:底层是HashMap的key的hashCode()和equals()方法共同保证不重复
Map:底层是Entry<k,v>键值对
遍历方式:List:fori、增强for、迭代器Iterator
Set:增强for、迭代器Iterator
Map:entrySet()、keySet()、values()
泛型:限定了集合类型的输入和输出
常用方法:add()/put()增
remove()删
set()/put()改
查:get()(Set接口没有get()方法)
Collections:CompareAble/new CompareTo{int compare()}
注:
内部比较器:
类要实现Comparable接口
如:
public class News implements Comparable<News>{
@Override
public int compareTo(News o) {
if (o instanceof News){
News obj=(News) o;
//自定义比较
return obj.newsId-this.newsId;//降序
// return this.newsId-obj.newsId;//升序
}
return 0;
}
}
测试类中代码:
Collections.sort(newsList);
System.out.println(newsList);
外部比较器
测试类中代码:
newsList.sort(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof News && o2 instanceof News){
News n1=(News) o1;
News n2=(News) o2;
return n2.getNewsId()-n1.getNewsId();//降序
}
return 0;
}
});
System.out.println(newsList);
实用类
包装类:方便泛型使用,与String类型的转换
装箱:把基本数据类型变为包装类型
拆箱:把包装类型转为基本数据类型
Integer a=1;//自动装箱
int b=new Integer(12);//自动拆箱
Math类
Random类
枚举类:特殊的类,私有构造,直接写的值都是当前类型
String:底层是字符数组常量,不可改变
indexOf(找某个字符的下标),split(根据分隔符分隔),charAt,substring(截取字符串,左闭右开)
toCharArry(含有中文情况下),getBytes
StringBuffer:线程安全,较慢
常用方法:append(),toString()
Stringbuilder:单线程的(线程不安全),较快
Date:UTC时间
SimpleDateFormat