1.学习api的目的是解决问题
Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。Collection 接口又有3种子类型,List、Set和Queue,再下面是一些抽象类,最后是具体实现类,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、HashMap、LinkedHashMap 等等。
2.Collection接口定义的标准为:无序、可重复的数据存储
Collection接口分为List接口和Set接口
List接口标准为:有序、可重复的数据存储
ArrayList底层是变长数组,长度到达阈值,自动扩容形成新数组来存储数据
ArrayList适合查询和重新赋值(查、改)
public class Demo1 {
public static void main(String[] args) {
//CRUD
ArrayList al = new ArrayList();
al.add("12345");
al.add(1);
System.out.println(al);
al.remove("12345");
System.out.println(al);
al.add(1,"12343");
System.out.println(al);
al.set(1,"agj");
ArrayList a2=new ArrayList();
a2.addAll(al);
System.out.println(a2);
System.out.println(al==a2);
System.out.println(al.indexOf("agj"));
System.out.println(al.contains("agj"));
// al.sort();//可以用来做排序,但必须自定义规则
//遍历
/* for (int i = 0; i < al.size(); i++) {
System.out.println(al.get(i));
}
for (Object o:al){
System.out.println(o);
}*/
/* Iterator iterator=al.iterator();
while(iterator.hasNext()){
//Object abc=iterator.next();
System.out.println(iterator.next());
}*/
al.forEach(new Consumer() {
@Override
public void accept(Object o) {
//o=o+"111";
System.out.println(o);
}
});
//list内部是对象时,才可以forEach(和在增强for类似)
/* al.forEach(o->{
o=o+"111";
});
System.out.println(al);
*/
}
}
LinkedList底层是双向链表,Node节点来存储上一个和下一个Node节点的地址,形成链条
LinkedList适合增、删
public class Demo4 {
public static void main(String[] args) {
LinkedList<Integer> l1=new LinkedList<>();
l1.add(112);
l1.addFirst(158);
System.out.println(l1);
}
}
Set接口标准为:无序、不可重复的数据存储
//Set 默认按照Hash值的顺序,没有插入顺序,因此程序员无法直接获取
//其存储的顺序,无法直接定位,所有没有get方法
//同样无法修改其中某个元素的值,但可以按值来删除
public class Demo6 {
public static void main(String[] args) {
HashSet<Integer> set=new HashSet<>();//Set默认按照Hash值的顺序
set.add(87);
set.add(91);
set.add(34);
set.add(67);
System.out.println(set);
System.out.println(set.contains(34));
set.add(87);
//重复值插入时,对底层的map重新赋值,而key不变,set的值也就不变
//遍历
//增强for 和forEach,迭代器
Iterator iterator=set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Map接口标准为:一对<key,value>构成的键值对映射 的数据存储
Map接口底层是Entry<k,v> -->映射 键是不可重复的(null值最多一个),值是可重复的
正常来说会把Map的key认为是Set集合,value认为是Collection集合
底层存储是数组+链表(8个元素以内),或者红黑树(8个元素以上)
/*
* 演示HashMap
* */
public class Demo5 {
public static void main(String[] args) {
HashMap map=new HashMap();
map.put("name","xiaohua");
map.put("age",31);
map.put(3,"agn");
map.put(1,"abc");
map.put(2,"123");
map.put("123","jaoe00");
// System.out.println(map.get(3));
map.replace(3,"abcdefghijklmnopqrstuvwxyz");
map.put("agg",null);
// System.out.println(map);
//遍历
/* Set keys=map.keySet();
for(Object key:keys){
System.out.print(key+":");
System.out.println(map.get(key));
}
Iterator iter=keys.iterator();
while(iter.hasNext()){
System.out.println(map.get(iter.next()));
}
Collection values=map.values();
Iterator iterator=values.iterator();
while(iterator.hasNext()){
System.out.println(map.get(iterator.next()));
}
*/
System.out.println("----------------");
Set entrySet=map.entrySet();
for(Object entry:entrySet){
System.out.println(entry);
if(entry instanceof Map.Entry){
Map.Entry e=(Map.Entry)entry;
System.out.print(e.getKey()+":");
System.out.println(e.getValue());
}
}
System.out.println("--------");
Set entry=map.entrySet();
Iterator it=entry.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
3.包装类
所谓包装类,是针对所有基本数据类型
//byte、short、int、long、float、double、char、boolean
装箱(装包):将基本数据类型转换成包装类类型
拆箱(拆包):将包装类类型转换成基本数据类型
/*
* 包装类
* */
public class Demo2 {
public static void main(String[] args) {
Byte b=new Byte("1");
Byte b2=1;//装箱 对应的基本数据类型,赋值给对应的包装类型时,会自动装箱
byte b3=new Byte("1");//拆箱,把包装类型,赋值给对应的基本数据类型,会自动拆箱
Integer i=new Integer(127);
Integer i3=127;//127以内的是byte值,在没有new的时候,不会直接装箱
Integer i4=127;//因此用双等比较时,是直接比的值,而超出范围时,比较的是地址
//System.out.println(i==i3);
System.out.println(i3==i4);
Integer i1=128;//127以内的是byte值,在没有new的时候,不会直接装箱
Integer i2=128;//因此用双等比较时,是直接比的值,而超出范围时,比较的是地址
System.out.println(i1==i2);
Character c=new Character('x');//Character没有(String s)的构造器,其他都有
//包装类的主要目的是:给泛型提供基本数据类型的使用方式,把基本类型对象化
//包装类提供了一些基本数据类型的转换方式
}
}
4.泛型:
在定义时指定泛型,代表可以接收多种类型,
在使用时指定具体类型,确定泛型的类型
/*
* 泛型
* */
public class Demo3 {
public static void main(String[] args) {
ArrayList<Integer> arrayList=new ArrayList();
/*Integer i=new Integer(154);
arrayList.add(i);
arrayList.add(123);
short s=10086;*/
//包装类型只能在对应的基本数据类新上自动装箱拆箱
//而基本数据类新的自动类型转换,在包装类上不能直接适用于其他的基本数据类新
// System.out.println(arrayList);
arrayList.add(123);
arrayList.add(488);
arrayList.add(128);
arrayList.add(328);
for (int i = 0; i < arrayList.size(); i++) {
}
for (Integer i:arrayList){}
Iterator<Integer> iterator=arrayList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//泛型:在定义时指定泛型,代表可以接收多种类型,在使用时指定具体类型,确定泛型的类型
}
}
5.Collections
/*
* Collection演示
* */
public class Demo7 {
public static void main(String[] args) {
ArrayList<Integer> aList=new ArrayList<>();
for (int i = 0; i < 10; i++) {
aList.add((int)(Math.random()*100));
}
System.out.println(aList);
Collections.sort(aList);
System.out.println(aList);
Student s1=new Student(1,"张三","1998-04-12");
Student s2=new Student(8,"王二","1994-04-12");
Student s3=new Student(13,"李华","1998-12-12");
Student s4=new Student(6,"李三","2001-04-12");
ArrayList<Student> arrayList1=new ArrayList<>();
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
arrayList1.add(s4);
Collections.sort(arrayList1);
for (Student s:arrayList1){
System.out.println(s.id+" "+s.name+" "+s.birthday);
}
Collections.reverse(arrayList1);
for (Student s:arrayList1){
System.out.println(s.id+" "+s.name+" "+s.birthday);
}
s1.printAll("");
}
}