Java集合框架的学习
Java集合框架主要学习:
1.集合的概念
2.Collection体系集合
3.List接口与实现类
4.Set接口与实现类
5.Map接口与实现类
6.泛型集合与工具类
集合
一:集合的概念
对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能
二:集合与数组的区别
1. 数组长度固定,集合的长度不固定
2. 数组可以存储基本类型和引用类型,集合只能存储引用类型
Collection体系集合
Collection父接口
- 特点:代表一组任意类型的对象,无序、无下标、不能重复。
- 方法:
- boolean add(Object obj):添加一个对象
- boolean addAll(Collection c):将一个集合中的所有对象添加到此集合中
- viod clear():清空此集合中的所有对象
- boolean contains(Object o):检查此集合中是否包含o对象
- boolean equals(Object o):比较此集合是否与指定对象相等
- boolean isEmpty():判断此集合是否为空
- boolean remove(Object o):在此集合中移除o对象
- int size():返回此集合中的元素个数
- Object[] toArray():将此集合转换成数组
public static void main(String[] args) {
//创建集合
Collection collection=new ArrayList();
//1.添加元素
collection.add("苹果");
collection.add("芒果");
collection.add("西瓜");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
//2.删除元素
collection.remove("西瓜");
System.out.println("删除之后的元素个数:"+collection.size());
//collection.clear();清空此集合中的所有对象
//3.遍历元素【重点】
//3.1使用增强for
System.out.println("------------3.1使用增强for--------------");
for(Object object:collection){
System.out.println(object);
}
//3.2使用迭代器(迭代器专门用来遍历集合的一种方式),迭代过程中不能使用collection的删除方法
//hashNext();有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
System.out.println("------------3.2使用迭代器--------------");
Iterator it = collection.iterator();
while (it.hasNext()){
String s=(String)it.next();//我们已经它是一个字符串了 所以进行强转
System.out.println(s);
//不能使用collection删除方法
//collection.remove(s); 并发修改异常
//it.remove();可以用这个来删除
}
//4.判断
System.out.println(collection.contains("苹果"));
System.out.println(collection.isEmpty());//判断是否为空
}
学生信息的修改
/**
* 学生类
*/
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name,int age){
super();
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
/**
* 保存学生信息
*/
public class Demo02 {
public static void main(String[] args) {
//新建Collection对象
Collection collection=new ArrayList();
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",21);
Student s3 = new Student("王五",22);
//1.添加数据
collection.add(s1);
collection.add(s2);
collection.add(s3);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//2.删除
collection.remove(s1);
System.out.println("删除之后的元素个数:"+collection.size());
//3.遍历
//3.1增强for
System.out.println("------------3.1使用增强for--------------");
for (Object object:collection) {
Student s=(Student)object;
System.out.println(s.toString());
}
System.out.println("------------3.2使用迭代器--------------");
//3.2迭代器:hasNext() next(); remove();迭代过程中不能使用collection的删除方法
Iterator it = collection.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();//我们已经它是一个字符串了 所以进行强转
System.out.println(s.toString());
}
//判断
System.out.println(collection.contains(s1));
System.out.println(collection.isEmpty());
}
}
List集合
- 特点:有序、有下标、元素可以重复使用
- 方法:
- void add(int index,Object o);再index位置插入对象o
- boolean addAll (int index,Collection c);将一个集合中的元素添加到此集合中的index位置
- Object get(int index);返回集合中指定位置的元素
- List subList(int fromIndex,int toIndex);返回fromIndex和toIndex之间的集合元素
public static void main(String[] args) {
//创建集合对象
List list = new ArrayList<>();
//1.添加元素
list.add("苹果");
list.add("香蕉");
list.add("葡萄");
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//2.删除元素
//list.remove("苹果");
System.out.println("删除之后的元素个数:"+list.size());
System.out.println(list.toString());
//3.遍历
//3.1使用for遍历
System.out.println("------------3.1使用for遍历--------------");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//3.2使用增强for
System.out.println("------------3.2使用增强for--------------");
for (Object object:list) {
System.out.println(object);
}
//3.3使用迭代器
System.out.println("------------3.3使用迭代器--------------");
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//3.4使用列表迭代器,和Iterator的区别,ListIterator可以向前或向后遍历,添加、删除、修改元素
System.out.println("------------3.4使用列表迭代器--------------");
ListIterator lit=list.listIterator();
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());//从前往后
}
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());//从后往前打印
}
//4.判断
System.out.println(list.contains("苹果"));
System.out.println(list.isEmpty());//判断是否为空
//5.获取
System.out.println(list.indexOf("苹果"));//获取脚标的位置
}
数字演示如下:
public static void main(String[] args) {
//创建集合
List list = new ArrayList<>();
//1.添加数字数据(自动装箱)
list.add("20");
list.add("30");
list.add("40");
list.add("50");
list.add("60");
System.out.println("元素个数:" + list.size());
System.out.println(list.toString());
//2.删除操作
//list.remove(0);根据脚标删除
//list.remove(0);
System.out.println("删除之后的元素个数:"+list.size());
System.out.println(list.toString());
//3.subList,返回子集合,含头不含尾
List subList = list.subList(1, 3);
System.out.println(subList.toString());
}
List实现类
list接口贴点:有序有下标,可以重复
- ArrayList【重点】:
a. 数组结构实现,查询快、增删慢;
b. JDK1.2版本,运行效率快,线程不安全 - Vector:
a. 数组结构实现,查询快、增删慢;
b. JDK1.0版本,运行效率慢,线程安全 - LinkedList:【链表】
a. 链表结构实现,增删快,查询慢;
b. JDK1.0版本,运行效率慢,线程安全
ArrayList的使用
/**
* ArrayList的使用
* 存储结构:数组,查找遍历速度快、增删慢
*/
public class Demo05 {
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList<>();
//1.添加元素
Student s1 = new Student("刘德华", 20);
Student s2 = new Student("郭富城", 22);
Student s3 = new Student("梁朝伟", 25);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//2.删除元素
arrayList.remove(s1);
//arrayList.remove(new Student("刘德华", 20)); //如果要用这个方法就的重写,equals(this==obj)
/**
* 重写方法
* @Override
* public boolean equals(Object obj) {
* //1判断是否是同一个对象
* if (this == obj) {
* return true;
* }
* //2判断是否为空
* if (obj == null) {
* return false;
* }
* //3判断是否是Student类型
* if (obj instanceof Student) {
* Student s = (Student) obj;
* //4比较属性
* if (this.name.equals(s.getName()) && this.age == s.getAge()) {
* return true;
* }
* }
* //5不满足条件返回false
* return false;
* }
*/
System.out.println("删除之后:"+arrayList.size());
System.out.println(arrayList.toString());
//3.遍历【重点】
//3.1使用迭代器
System.out.println("------------3.3使用迭代器--------------");
Iterator it = arrayList.iterator();
while (it.hasNext()){
Student s=(Student) it.next();
System.out.println(s.toString());
}
//3.2使用列表迭代器
System.out.println("------------3.4使用列表迭代器--------------");
ListIterator lit=arrayList.listIterator();
while (lit.hasNext()){
Student s=(Student) lit.next();
System.out.println(s.toString());
}
// System.out.println("------------3.4使用列表迭代器逆序--------------");
//while (lit.hasPrevious()){
// Student s=(Student) lit.previous();
// System.out.println(s.toString());
// }
//4.判断
System.out.println(arrayList.contains(s2));
System.out.println(arrayList.isEmpty());
//5.查找
System.out.println(arrayList.indexOf(s2));
}
}
Vector的使用
public static void main(String[] args) {
//创建集合
Vector vector = new Vector<>();
//1.添加元素
vector.add("苹果");
vector.add("香蕉");
vector.add("草莓");
System.out.println("元素个数:"+vector.size());
System.out.println(vector.toString());
//2.删除元素
// vector.remove(0);
// vector.remove("香蕉");
// vector.clear();//清空
//3.遍历
//使用枚举器
System.out.println("-------------使用枚举器-----------------");
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
String s=(String) elements.nextElement();
System.out.println(s);
}
//4.判断
System.out.println( vector.contains("苹果"));
System.out.println(vector.isEmpty());
//5.其他方法
//firsetElement、lastElement、elementAt();
}
LinkedList的使用
存储结构:双向链表
public static void main(String[] args) {
//创建集合
LinkedList linkedList = new LinkedList<>();
//1.添加元素
Student s1 = new Student("刘德华", 20);
Student s2 = new Student("郭富城", 22);
Student s3 = new Student("梁朝伟", 25);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//2.删除元素
linkedList.remove(s1);
System.out.println("删除后的元素个数:"+linkedList.size());
//3.遍历
//3.1for遍历
System.out.println("-----------for遍历-----------");
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
//3.2增强for
System.out.println("-----------增强for-----------");
for (Object object:linkedList) {
Student s=(Student) object;
System.out.println(s.toString());
}
//3.3使用迭代器
System.out.println("-----------使用迭代器-----------");
Iterator it = linkedList.iterator();
while (it.hasNext()){
Student s=(Student)it.next();
System.out.println(s);
}
//3.4使用列表迭代器
System.out.println("-----------使用列表迭代器-----------");
ListIterator lit=linkedList.listIterator();
while (lit.hasNext()) {
Student s = (Student) lit.next();
System.out.println(s.toString());
}
//4.判断
System.out.println(linkedList.contains(s1));
System.out.println(linkedList.isEmpty());
//5.获取
System.out.println(linkedList.indexOf(s2));
}
ArrayList和LinkedList的区别
ArrayList:必须开辟连续空间,查询快,增删慢
LinkedList:无需开辟连续空间,查询慢,增删快
泛型的概述
- Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
- 常见的形式有:泛型类、泛型接口、泛型方法
- 语法<T,…> T称为类型占位符,表示一种引用类型
- 好处:
1. 提高代码的重用性
2. 防止类型转换异常,提高代码的安全性
1、泛型类
/**
* 泛型类
* 语法:类型<T>
* T是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开
*/
public class MyGeneric<T> {
//使用泛型T
//1.创建变量
T t;
//2.作为方法的参数
public void show(T t){
System.out.println(t);
}
//3.使用泛型作为方法的返回值
public T getT(){
return t;
}
}
//泛型类的使用
public class TestGeneric {
public static void main(String[] args) {
//使用泛型类创建对象
//注意点:1.泛型只能使用引用类型 2.不同泛型类型之间不能相互赋值
MyGeneric<String> myGeneric=new MyGeneric<String>();
myGeneric.t="hello";
myGeneric.show("java真香");
String string=myGeneric.getT();//java真香
MyGeneric<Integer> myGeneric2=new MyGeneric<Integer>();
myGeneric2.t=100;
myGeneric2.show(200);
Integer integer=myGeneric2.getT();//200
}
}
2、泛型接口
/**
* 泛型接口
* 语法,接口名<T>
* 注意:不能泛型静态常量
* @param <T>
*/
public interface MyInterface<T> {
String name="张三";
T server(T t);
}
//新建一个类
public class interfaceImpl implements MyInterface<String> {
@Override
public String server(String t) {
System.out.println(t);
return null;
}
//最后测试泛型接口
public static void main(String[] args) {
interfaceImpl impl = new interfaceImpl();
impl.server("1000");
}
}
3、泛型方法
/**
* 泛型方法
* 语法:<T> 返回值类型
*/
public class MyGenericMethod {
//泛型方法
public <T> T show(T t){
System.out.println("泛型方法"+t);
return t;
}
}
//最后测试泛型方法
public static void main(String[] args) {
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show("java真香");//泛型方法java真香
myGenericMethod.show(200);
myGenericMethod.show(3.14);
//可以传任何数据类型
}
泛型集合
public static void main(String[] args) {
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("xxx");
arrayList.add("yyy");
for (String string: arrayList) {
System.out.println(string);
}
}
Set子接口以及Set接口的使用
- 特点:无序、无下标、元素不可重复
- 方法:全部继承自Collection中的方法
/**
* 测试Set接口的使用
* */
public class Demo02 {
public static void main(String[] args) {
//创建集合
Set<String> set=new HashSet<>();
//1.添加数据
set.add("小米");
set.add("苹果");
set.add("华为");
System.out.println("数据个数:"+set.size());
System.out.println(set.toString());
//2.删除数据
// set.remove("小米");
// System.out.println(set.toString());
//3.遍历【重点】
//3.1使用增强for
System.out.println("------------3.2使用增强for--------------");
for (String string:set) {
System.out.println(string);
}
//3.2使用迭代器
System.out.println("------------3.3使用迭代器--------------");
Iterator iterator= set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.判断
System.out.println(set.contains("苹果"));
System.out.println(set.isEmpty());
}
Set实现类
- HashSet【重点】:
存储结构:哈希表(数组+链表+红黑树)
存储过程:
1. 根据hashcode计算保存位置,如果此位置为空,则直接保存,如果不为空执行第二步。
2. 再执行equals方法,如果equals方法为true,则认为是重复的,否则形成链表
1.基于HashCode计算元素存放位置
2.当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
- TreeSet:
存储结构:红黑树
要求:元素必须要实现Comparable接口,CompareTo()方法返回值为0,则认为是重复元素
1.基于排列顺序实现元素不重复(重复不添加)
2.实现了SortSet接口,对集合元素自动排序
3.元素对象的类型必须实现Comparable接口,指定排序规则(也可以用匿名内部类来实现:Comparator实现定制比较:比较器)
4.通过CompareTo方法确定是否为重复元素
/**
* 测试Set接口的使用
*
*/
public class Demo02 {
public static void main(String[] args) {
//创建集合
Set<String> set=new HashSet<>();
//1.添加数据
set.add("小米");
set.add("苹果");
set.add("华为");
System.out.println("数据个数:"+set.size());
System.out.println(set.toString());
//2.删除数据
// set.remove("小米");
// System.out.println(set.toString());
//3.遍历【重点】
//3.1使用增强for
System.out.println("------------3.2使用增强for--------------");
for (String string:set) {
System.out.println(string);
}
//3.2使用迭代器
System.out.println("------------3.3使用迭代器--------------");
Iterator iterator= set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.判断
System.out.println(set.contains("苹果"));
System.out.println(set.isEmpty());
}
类对象进行使用
public class Person {
private String name;
private int age;
public Person(){
}
public Person(String name,int age){
super();
this.name=name;
this.age=age;
}
//不重写toString的话 输出的是哈希码值
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//新建一个Demo类进行测试
public class Demo04 {
public static void main(String[] args) {
//新建集合
HashSet<Person> persons = new HashSet<>();
//1.添加元素
Person p1 = new Person("胡歌",20);
Person p2 = new Person("刘德华",21);
Person p3 = new Person("周润发",22);
Person p4 = new Person("梁朝伟",23);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
//也可以这样添加
persons.add(new Person("梁朝伟",22));
System.out.println("元素个数:"+persons.size());
System.out.println(persons.toString());
TreeSet案例如下:
/**
* 要求:使用TreeSet集合实现字符串按照长度进行排序
* Comparator接口实现定制比较
*/
public class Demo05 {
public static void main(String[] args) {
//创建集合
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {//new Comparator<String>()相当于匿名内部类
@Override
public int compare(String o1, String o2) {
int n1=o1.length()-o2.length();
int n2=o1.compareTo(o2);
return n1==0?n2:n1;//n1等于0则用n2的规则进行比较,否则就是n1条件比较
}
});
//添加元素
treeSet.add("helloword");
treeSet.add("apple");
treeSet.add("tomato");
treeSet.add("banana");
treeSet.add("cat");
treeSet.add("zhangsan");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
}
}
Map集合
Map(interface)分为:
- HashMap(Class)
- SortedMap(interface)包含–>TreeMap(class)
Map接口的特点:
1.用于存储任意键值对(Key-Value)
2.键:无序、无下标、不允许重复(唯一)
3.值:无序、无下标、允许重复
Map父接口
- 特点:存储一对数据(Key-Value),无序、无下标、键不可以重复,值可以重复
- 方法:
- V put(K key,V value);//将对象存入到集合中,关联键值,key重复则覆盖原值
- Object get(Object key);//根据键获取对应的值
- Set< K > //返回所有Key
- Collection values();//返回包含所有值的Collection集合
- Set<Map.Entry<K,V>> //键值匹配的Set集合
Map接口的使用
/**
* Map接口的使用
* 特点:(1)存储键值对(2)键不能重复、值可以重复(3)无序
*/
public class Demo06 {
public static void main(String[] args) {
//创建集合
Map<String,String> map = new HashMap<>();
//1.添加元素
map.put("cn","中国");
map.put("usa","美国");
map.put("uk","英国");
System.out.println("元素个数:"+map.size());
System.out.println(map.toString());
//2.删除
// map.remove("cn");
// System.out.println("删除之后的元素个数:"+map.size());
// System.out.println(map.toString());
//3.遍历
//3.1使用KeySet()
System.out.println("-----------使用KeySet-----------");
//Set<String> keySet = map.keySet();
for (String key:map.keySet()) {//上面一行可以不要,把keySetg改成map.keySet()
System.out.println(key+","+map.get(key));//map.get(key)获取value
}
//3.2使用EntrySet()
System.out.println("-----------使用EntrySet-----------");
//Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry:map.entrySet() //上面一行可以不要,把entries改成map.entrySet()
) {
System.out.println(entry.getKey()+","+entry.getValue());
}
//判断
System.out.println(map.containsKey("cn"));
System.out.println(map.containsValue("中国"));
}
}
HashMap使用
HashMap集合的使用
/**
* HashMap集合的使用
* 存储结构 :哈希表(数组+链表+红黑树)
* 使用key的hashcode和equals作为依据
*/
public class Demo07 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> students=new HashMap<Student,String>();
//1.添加元素
Student s1 = new Student("张三", 30);
Student s2 = new Student("李四", 31);
Student s3 = new Student("王五", 32);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"杭州");
students.put(new Student("张三", 30),"非洲");//重写hashcode方法后就添加不进去了
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//2.删除
// students.remove(s1);
// System.out.println("删除之后的元素个数:"+students.size());
// System.out.println(students.toString());
//3.遍历
//3.1使用KeySet()
System.out.println("-----------使用KeySet-----------");
for (Student key:students.keySet()) {
System.out.println(key+","+students.get(key));//map.get(key)获取value
}
//3.2使用EntrySet()
System.out.println("-----------使用EntrySet-----------");
for (Map.Entry<Student, String> entry:students.entrySet()
) {
System.out.println(entry.getKey()+","+entry.getValue());
}
//4.判断
System.out.println(students.containsKey(s1));
System.out.println(students.containsValue("上海"));
}
}
Student类:
public class Student {
private String name;
private int stuNo;
public Student() {//无参构造,"Alt+Ins"选择Constructor 选择Select None就是无参构造
}
public Student(String name, int stuNo) {//有参构造,"Alt+Ins"选择Constructor 选择OK就是有参构造
this.name = name;
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
@Override
public String toString() {//重写方法 为了好打印
return "Student{" +
"name='" + name + '\'' +
", stuNo=" + stuNo +
'}';
}
}
Map集合得实现类
- HashMap【重点】:
JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value - Hashtable:
JDK1.0版本,线程安全,运行效率慢;不允许用null作为key或是value - Properties:
Hashtable得子类,要求key和value都是String。通常用于配置文件得读取 - TreeMap:
实现了SortedMap接口(是Map得子接口),可以对key自动升序
TreeMap的使用
public static void main(String[] args) {
//创建集合(定制比较)
TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
//treeMap要去重写Student
/**
* 类 implements Comparable<Student>
* @Override
* public int compareTo(Student o) {
* int n2=this.stuNo-o.getStuNo();
* return n2;
* }
*/
//1.添加元素
Student s1 = new Student("张三", 30);
Student s2 = new Student("李四", 31);
Student s3 = new Student("王五", 32);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"杭州");
treeMap.put(new Student("张三", 30),"非洲");//重写hashcode方法后就添加不进去了
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//2.删除
treeMap.remove(s1);
System.out.println("删除之后的元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//3.遍历
//3.1使用KeySet()
System.out.println("-----------使用KeySet-----------");
for (Student key:treeMap.keySet()) {
System.out.println(key+","+treeMap.get(key));//map.get(key)获取value
}
//3.2使用EntrySet()
System.out.println("-----------使用EntrySet-----------");
for (Map.Entry<Student, String> entry:treeMap.entrySet()
) {
System.out.println(entry.getKey()+","+entry.getValue());
}
//4.判断
System.out.println(treeMap.containsKey(s1));
System.out.println(treeMap.containsValue("上海"));
}
.
Colletions工具类
- 概念:集合工具类,定义了除了存取意外得集合常用方法
- 方法:
- public static void reverse(List<?> list) //反转集合中元素得顺序
- public static void shuffle(List<?> list) //随机重置集合元素得顺序
- public static void sort(List list) //升序排序(元素类型必须实现Comparable)
/**
* Collections工具类的使用
*/
public class Demo09 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(20);
list.add(5);
list.add(12);
list.add(30);
list.add(6);
//sort排序
System.out.println("排序之前:"+list.toString());
Collections.sort(list);
System.out.println("排序之后:"+list.toString());
//binarySearch 二分查找
int i=Collections.binarySearch(list,12);
System.out.println(i);//2
//copy复制
List<Integer> dest = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
dest.add(0);
}
Collections.copy(dest,list);//list大小为5 但是dest初始为0 直接添加会报错,此时需要添加循环
System.out.println("复制后的:"+dest.toString());
//reverse反转
Collections.reverse(list);
System.out.println("反转之后:"+list);
//shuffle打乱
Collections.shuffle(list);
System.out.println("打乱之后:"+list);
//补充:将list转成数组
System.out.println("----------将list转成数组------------");
Integer[] array = list.toArray(new Integer[0]);//Integer[0] 长度为0时 此时list数组为多长array就是多长。
//当Integer[n]长度大于list时 此时输出的array长度为Integer[n]的长度
System.out.println(array.length);
System.out.println(Arrays.toString(array));
//数组转成集合
System.out.println("----------数组转成集合------------");
String[] names={"张三","李四","王五"};
//集合是一个受限集合,不能添加和删除
List<String> list2 = Arrays.asList(names);
System.out.println(list2);
//把基本类型数组转成集合时,需要修改成包装类
Integer[] nums={100,200,300,400,500};
List<Integer> list3 = Arrays.asList(nums);
System.out.println(list3);
}
}
.
.
.
集合的总结
集合的概念:对象的容器,和数组类似,定义了对多个对象进行操作的常用方法
List集合:有序、有下标、元素可重复。(ArrayList、LinkedList、Vector)
ArrayList【重点】:数组结构实现,查询快、增删慢;运行效率快,线程不安全
存储结构:数组,查找遍历速度快、增删慢
.
Vector: 数组结构实现,查询快、增删慢;JDK1.0版本,运行效率慢,线程安全
遍历时使用枚举器
.
LinkedList:链表结构实现,增删快,查询慢;JDK1.0版本,运行效率慢,线程安全
存储结构:双向链表
.
ArrayList:必须开辟连续空间,查询快,增删慢
LinkedList:无需开辟连续空间,查询慢,增删快
.
Set集合:无序、无下标、元素不可重复。(HashSet、TreeSet)
特点:无序、无下标、元素不可重复
方法:全部继承自Collection中的方法
.
HashSet【重点】:
存储结构:哈希表(数组+链表+红黑树)
存储过程:
1. 根据hashcode计算保存位置,如果此位置为空,则直接保存,如果不为空执行第二步。
2. 再执行equals方法,如果equals方法为true,则认为是重复的,否则形成链表
1.基于HashCode计算元素存放位置
2.当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
.
TreeSet:
存储结构:红黑树
要求:元素必须要实现Comparable接口,CompareTo()方法返回值为0,则认为是重复元素
1.基于排列顺序实现元素不重复(重复不添加)
2.实现了SortSet接口,对集合元素自动排序
3.元素对象的类型必须实现Comparable接口,指定排序规则(也可以用匿名内部类来实现:Comparator实现定制比较:比较器)
4.通过CompareTo方法确定是否为重复元素
.
Map集合:存储一对数据,无序、无下标,键不可重复,值可以重复。(HashMap、HashTable、TreeMap)
Map(interface)分为:
HashMap(Class)
SortedMap(interface)包含–>TreeMap(class)
.
Map接口的特点:
1.用于存储任意键值对(Key-Value)
2.键:无序、无下标、不允许重复(唯一)
3.值:无序、无下标、允许重复
.
Map父接口 特点:存储一对数据(Key-Value),无序、无下标、键不可以重复,值可以重复
方法:
- V put(K key,V value);//将对象存入到集合中,关联键值,key重复则覆盖原值
- Object get(Object key);//根据键获取对应的值
- Set< K > //返回所有Key
- Collection values();//返回包含所有值的Collection集合
- Set<Map.Entry<K,V>> //键值匹配的Set集合
.HashMap集合【重点】:
存储结构 :哈希表(数组+链表+红黑树)
使用key的hashcode和equals作为依据
JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value
.
Hashtable:
JDK1.0版本,线程安全,运行效率慢;不允许用null作为key或是value
.
Properties:
Hashtable得子类,要求key和value都是String。通常用于配置文件得读取
.
TreeMap:
实现了SortedMap接口(是Map得子接口),可以对key自动升序
.
Collerctions:集合工具类,定义了除了存取意外得集合常用方法
概念:集合工具类,定义了除了存取意外得集合常用方法
.
方法:
- public static void reverse(List<?> list) //反转集合中元素得顺序
- public static void shuffle(List<?> list) //随机重置集合元素得顺序
- public static void sort(List list) //升序排序(元素类型必须实现Comparable)