泛型
简介
将数据类型当作参数进行传递
jdk1.5以后出现
在那使用
类
接口
方法
变量
怎么使用
语法:<T,...>
在方法中使用:
访问权限修饰符 <T> T test01(T t){
return t;
}
在类中使用:
访问权限修饰符 class 类名<T> {
T t;
public TestFanXing(T t){
this.t = t;
}
}
在接口中使用:
public interface TestInterface<T> {
/**
* 不能使用泛型作为接口中的静态常量
*/
void test(T t);
}
class Test02<T> implements TestInterface<T> {
@Override
public void test(T t) {
// TODO Auto-generated method stub
}
}
集合
简介
存放一组数据类型相同的数据的容器
集合和数组的区别:
集合:长度不定,只能存储引用数据类型
数组:长度固定。可以存储基本数据类型和引用数据类型
体系
Collection(接口)
--List(接口):有序,有下标
--ArrayList(类,掌握)
--Vector(类,了解)
--LinkedList(类,了解)
--Set(接口):无序,无下标,元素不可重复
--HashSet(类.掌握)
--LinkedHashSet(类,了解)
--SortedSet(接口)
--TreeSet(类,掌握)
Collection常用方法
增加:
add:添加单个
addAll:添加多个
删除:
remove
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class Demo01 {
public static void main(String[] args) {
Collection<Integer> collection = new HashSet<Integer>();
collection.add(10);
collection.add(1);
collection.add(2);
collection.add(5);
collection.add(7);
collection.add(10);
System.out.println(collection);
Collection<Integer> collection2 = new ArrayList<Integer>();
collection2.add(3);
collection2.add(4);
collection.addAll(collection2);
System.out.println(collection);
boolean b = collection.remove(1);
System.out.println(collection);
collection.removeAll(collection2);
System.out.println(collection);
System.out.println(collection.size());
System.out.println(collection.isEmpty());
System.out.println(collection.contains(3));
boolean c = collection.equals(collection);
System.out.println(c);
Object[] array = collection.toArray();
for (Object object : array) {
System.out.println(object);
}
}
}
特点:无序,无下标
List与Set的区别
List:有序,有下标,元素允许重复
Set:无序,无下标,元素不可以重复
List
常用方法:
import java.util.ArrayList;
import java.util.List;
public class Demo03 {
public static void main(String[] args) {
List<Dog> dogs = new ArrayList<Dog>();
dogs.add(new Dog("旺财01"));
dogs.add(new Dog("旺财02"));
dogs.add(new Dog("旺财03"));
dogs.add(new Dog("旺财04"));
dogs.add(new Dog("旺财05"));
dogs.add(0,new Dog("旺财00"));
ArrayList<Dog> list = new ArrayList<Dog>();
list.add(new Dog("大黄01"));
list.add(new Dog("大黄02"));
dogs.addAll(0,list);
System.out.println(dogs);
Dog dog = dogs.get(0);
System.out.println(dog);
dogs.set(0, new Dog("大黑"));
System.out.println(dogs);
List<Dog> list2 = dogs.subList(0, 3);
System.out.println(list2);
}
}
class Dog{
private String name;
public Dog(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog [name=" + name + "]";
}
}
List的常用子类:
ArrayList
数据结构:数组
特点:查询速度快,增删速度慢,jdk1.2出现,线程不安全
Vector
数据结构:数组
特点:查询速度快,增删速度慢,jdk1.0出现,线程安全
LinkedList
数据结构:链表
特点:查询速度慢,增删速度快
Set(接口)
常用方法:无
Set常用子类:
HashSet(类)
数据结构:
1.8以前,数组+链表
1.8以后,数组+红黑树
特点:
1、判断对象的哈希码值是否相同(hashCode)
2、如果对象的哈希码值相同,调用对象的equals方法,如果相同舍弃,如果不相同以链表的形式添加
LinkedHashSet(类)
数据结构:
1.8以前,数组+链表+链表
1.8以后,数组+红黑树+链表
特点:
存入顺序与取出顺序一致,多出的链表就是为了排序
1、判断对象的哈希码值是否相同(hashCode)
2、如果对象的哈希码值相同,调用对象的equals方法,如果相同舍弃,如果不相同以链表的形式添加
TreeSet(类)
数据接口:红黑树
特点:
存储的对象要么有比较性,要么TreeSet设定比较规则
判断是否重复:
方案1、让存入的数据具有比较性
方案2、指定比较器
比较性:
1、实现Comparable接口
2、重写compareTO方法
比较器:
1、创建TreeSet时,传入实现了Comparator的子类对象
2、重写compare方法