集合
集合概念:
对象的容器,实现了对对象常用的操作,类似数组功能。
数组和集合的区别:
- 数组的长度固定,集合长度不固定
- 数组可以存储基本类型和引用类型,集合只能引用类型
Collection父接口
特点:代表一组任意的类型,无序、无下标、不能重复。(实现类ArrayList有些可重复,有下标,有序)
方法:API文档
iterator:
- boolean hasnext();判断下一个元素是否存在
- next();返回迭代中的下一个元素 引用类型 Object
- remove();删除元素void
- 注意:在迭代器中不能用collection去删除元素,只能用iterator.remove()方法去删除。
collection应用1:
package Demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args) {
Collection collection=new ArrayList();
/**
* 1. 添加
* 2. 删除
* 3. 遍历
* 4. 包含
*/
//添加
collection.add("1");
collection.add("2");
collection.add("3");
collection.add("4");
collection.add("5");
//遍历
//1.增强for循环for each
System.out.println(collection.size());
int i=0;
for (Object o:collection) {
System.out.println(i+" "+o);
i++;
}
System.out.println("------------------增强for循环for each-------------");
//迭代器
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
String s= (String)iterator.next();
System.out.println(s);
// iterator.remove();
// collection.remove(s);
//注意:不能在迭代器里面使用collection的删除方法
//会报错ConcurrentModificationException
}
System.out.println("集合长度 "+collection.size());
//包含:
System.out.println( collection.contains("1"));
System.out.println( collection.contains(1));
System.out.println(collection.isEmpty());//判断是否为空
}
}
package demo02;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student(String name,int age) {
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 +
'}';
}
}
class StudentTest{
public static void main(String[] args) {
Student aaa = new Student("aaa", 15);
Student bbb = new Student("bbb", 20);
Student ccc = new Student("ccc", 25);
//添加
Collection collection=new ArrayList();
collection.add(aaa);
collection.add(bbb);
collection.add(ccc);
System.out.println(collection.toString());
//清除
// collection.clear();
// System.out.println(collection.toString());
//迭代
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
Object o= iterator.next();
System.out.println(o.toString());
}
//包含
System.out.println("------------------------------");
boolean b= collection.contains(aaa);
System.out.println(b);
}
}
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之间的集合元素。
-
package demo03; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class TestList { public static void main(String[] args) { List list=new ArrayList(); //一:添加元素 list.add("苹果"); list.add("华为"); list.add("小米"); list.add("魅族"); System.out.println("元素个数"+list.size()); System.out.println(list.toString()); //二:删除 list.remove(0);//根据下标的形式 list.remove("苹果"); System.out.println("元素个数"+list.size()); System.out.println(list.toString()); //三:遍历 //3.1for循环 System.out.println("-------------- //3.1for循环--------------"); for (int i = 0; i <list.size(); i++) { System.out.println("第"+i+"个元素:"+list.get(i)); } //3.2foreach循环 System.out.println(" ----------------3.2foreach循环----------"); for (Object o :list) { System.out.println(o); } //3.3迭代器 System.out.println("--------------迭代器------------------"); Iterator iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //3.4列表迭代器 ListIterator listIterator = list.listIterator(); System.out.println("-------------从前往后遍历------------"); while(listIterator.hasNext()){ System.out.println(listIterator.next()); } System.out.println("------------从后往前遍历-------------"); while(listIterator.hasPrevious()){ System.out.println(listIterator.previous()); } //4包含 System.out.println(list.isEmpty()); System.out.println(list.contains("华为")); } }
test2:
package demo03; import java.util.ArrayList; import java.util.List; public class TestList01 { public static void main(String[] args) { List list=new ArrayList(); //添加 list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); list.add(360); System.out.println("元素个数:"+list.size()); System.out.println(list.toString()); //删除:注意在这里有一个自动装箱的过程所以不能直接进行删除,利用下角标,或者new Integer对象删除 //Integer对象注意缓冲区 //list.remove(0); list.remove(new Integer(20)); list.remove(new Integer(360)); System.out.println("元素个数:"+list.size()); System.out.println(list.toString()); //总结只要存储的数据一致就可以进行删除操作 List list1=list.subList(1,3); System.out.println(list1);//含头不含尾 } }
List接口的实现类
- ArrayList
-
- 数组接口实现,查询块,增减慢
- jdk1.2版本,运行效率块、线程不安全。
- Vector
-
- 数组结构实现,查询块,增减慢
- jdk1.0版本,运行效率慢,线程安全
- LinkedList
-
- 链表实现结构,查询慢,增减块
ArrayList案例:
package demo03;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class TestArrayList {
public static void main(String[] args) {
List list=new ArrayList();
//1添加
list.add(new Student("刘德华",20));
list.add(new Student("李连杰",18));
list.add(new Student("范冰冰",20));
list.add(new Student("赵丽颖",19));
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//删除
//两种方式
//1.利用下角标
//2.重写equals
// list.remove(0);
// list.remove(new Student("刘德华",20));
// System.out.println("元素个数:"+list.size());
// System.out.println(list.toString());
//遍历
//1.迭代器
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Student s = (Student) iterator.next();
System.out.println(s.toString());
}
System.out.println("------------------------------------");
//2.列表迭代器
ListIterator listIterator = list.listIterator();
while(listIterator.hasNext()){
Student s =(Student) listIterator.next();
System.out.println(s.toString());
}
System.out.println("-------------------------");
while(listIterator.hasPrevious()){
Student s =(Student) listIterator.previous();
System.out.println(s.toString());
}
//判断
System.out.println(list.contains(new Student("赵丽颖",19)));
System.out.println(list.isEmpty());
//查找
System.out.println(list.indexOf(new Student("赵丽颖",19)
));
System.out.println(new Student("撒谎比",19));
}
}
class Student{
private String name;
private int 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;
}
public Student(String name,int age) {
this.name = name;
this.age=age;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object obj) {
if (this==obj){
//判断是不是一个对象
return true;
}
if (this==null){
//是否为空
return false;
}
if(obj instanceof Student){
//判断是否为Student类型
Student s=(Student) obj;
if(this.getName().equals(s.getName())&&this.getAge()==s.getAge()){
return true;
}
}
return false;
}
}
arrayList:源码分析
- 默认容量:DEFAULT_CAPACITY 10
-
- 注意:没有向集合中添加任何元素是0
- elementDate:实际存放数组
- size实际元素个数
- add方法
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
分析:填充元素,size进行扩容
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
分析:刚开始空数组已经赋值给默认数组,条件成立。返回最大值,size+1=1(size刚开始是0)
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
分析:进行扩容
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
分析:后移1位,相当于oldCapacity除以2
最后newCapacity=10,默认数组
分析:如果大于10,继续执行,并且每次扩容是原来的1.5倍
vectortest:
package demo03;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;
public class TestVector {
public static void main(String[] args) {
Vector vector = new Vector<>();//创建对象
//添加元素
vector.add(20);
vector.add(30);
vector.add(40);
vector.add(50);
vector.add(60);
System.out.println(vector.toString());
//删除remove
//迭代
System.out.println("------------------------for");
for (int i = 0; i < vector.size(); i++) {
System.out.println(vector.get(i));
}
System.out.println("--------------------------foreach");
for (Object o :vector) {
System.out.println(o);
}
System.out.println("---------------迭代器");
Iterator iterator = vector.iterator();
while(iterator.hasNext()){
Object next = iterator.next();
System.out.println(next);
}
System.out.println("----------------列表迭代器");
ListIterator list= vector.listIterator(vector.size());
while(list.hasPrevious()){
int i = (int )list.previous();
System.out.println(i);
}
System.out.println(vector.contains(20));
System.out.println(vector.isEmpty());
System.out.println(vector.elementAt(0));
//枚举
Enumeration enumeration=vector.elements();
while(enumeration.hasMoreElements()){
Object o = enumeration.nextElement();
System.out.println(o);
}
//最后一个和首个组件
System.out.println(vector.lastElement());
System.out.println(vector.firstElement());
}
}
说明:列表迭代器的位置主义一下
linkedlist
package demo03;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* 1. 添加
* 2. 删除
* 3. 迭代for foreach 迭代器 列表迭代器
* 4. 包含
* 5. 查找
*/
public class TestLinkedList {
public static void main(String[] args) {
LinkedList linkedList=new LinkedList();
linkedList.add(new Student("刘德华",20));
linkedList.add(new Student("李连杰",18));
linkedList.add(new Student("范冰冰",20));
linkedList.add(new Student("赵丽颖",19));
//3
for (int i = 0; i <linkedList.size() ; i++) {
System.out.println(linkedList.get(i));
}
for (Object o :linkedList) {
System.out.println(o);
}
Iterator iterator = linkedList.iterator();
while(iterator.hasNext()){
Student s =(Student) iterator.next();
System.out.println(s.toString());
}
System.out.println("------------------------" +
"");
ListIterator listIterator = linkedList.listIterator(linkedList.size());
while(listIterator.hasPrevious()){
Student student =(Student) listIterator.previous();
System.out.println(student.toString());
}
System.out.println("-----------------");
while(listIterator.hasNext()){
Student s=(Student) listIterator.next();
System.out.println(s.toString());
}
//4
System.out.println(linkedList.isEmpty());
System.out.println(linkedList.contains(new Student("刘德华",20)));//重写了equals
//5
System.out.println(linkedList.indexOf(new Student("李连杰",18)));
}
}
源码分析:
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
arraylist和linkedlist区别
- arraylist :必须开辟连续的空间,查询块,增删慢;
- linkedlist:无需开辟连续的空间,查询慢,增删快
泛型
- Java泛型是jdk1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
- 常见形式有:泛型类、泛型接口、泛型方法
- 语法:
t称为类型占用符,表示一种引用类型。
- 好处:
-
-
- 提高代码的重用性
- 防止类型转换异常,提高代码的安全性
-
泛型类
- 创建变量
- 作为方法参数
- 泛型作为方法返回值
- 不能实例化,无法保证一定有无参构造和构造方法的使用
注意:1. 泛型只能使用引用类型,2. 不同泛型对象之间不能相互赋值
package demo04;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
public class MyGeneric<E> {
//1
public E e;
//2
public void show(E e){
System.out.println("you are active");
}
//3
public E getE(){
return e;
}
}
class TestGeneric{
public static void main(String[] args) {
MyGeneric <String> myGeneric1=new MyGeneric();
myGeneric1.getE();
myGeneric1.show("1615");
myGeneric1.e="15645";
}
}
泛型接口
注意:不能使用泛型创建静态常量
test:
package demo04;
public interface MyInterface <E>{
public E show(E e);
}
class TestMyInterface<T>implements MyInterface<T>{
@Override
public T show(T t ) {
System.out.println("you are active");
System.out.println(t);
return t;
}
}
class A{
public static void main(String[] args) {
TestMyInterface<String> stringTestMyInterface = new TestMyInterface<>();
String s= stringTestMyInterface.show("test");
System.out.println(s);
}
}
泛型方法
语法:方法返回值的前面
注意作用域
package demo04;
public class TestMethod {
public static <E> E show(E e){
System.out.println("you are active");
return e;
}
public static void main(String[] args) {
int i=TestMethod.show(20);
System.out.println(i);
}
}
ps:直接根据参数的类型自动改变泛型类型
泛型集合
概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致
特点:1. 编译器即可检查,而非运行时抛出异常
- 访问时,不必强制类型转换
- 不同泛型之间不同引用不能相互赋值,泛型不存在多态
Set接口
跟以上类类类似
package demo05;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
Set<String > set=new HashSet<>();
//添加
set.add("苹果");
set.add("华为");
set.add("魅族");
set.add("黑纱");
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
//删除
//略
//遍历,没有列表迭代器
System.out.println("--------------------------------foreach");
for (String s:
set) {
System.out.println(s.toString());
}
System.out.println("-------------------------迭代器");
Iterator<String> iterator = set.iterator();
while(iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
}
//判断
System.out.println(set.isEmpty());
System.out.println(set.contains("华为"));
//没有查找
}
}
HashSet
-
基于HashCode计算元素存放位置
-
当存入元素的哈希码相同时,会调用equals方法进行确认,如结果为true,则拒绝存入。
-
存储结构:哈希表(数组+链表+红黑树)
-
存储过程:
-
- 根据hashcode计算保存的位置,如果此位置为空,则直接保存,,如果不为空执行第二步
- 在执行equals方法,如果equals方法为true,则认为重复,否则形成链表。
-
test:
Student类
package demo02;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student(String name,int age) {
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 +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name.equals(student.name);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + age;
return result;
}
}
TestHashSet
package demo05;
import demo02.Student;
import java.util.HashSet;
import java.util.Iterator;
public class TestHashSet1 {
public static void main(String[] args) {
Student s1 = new Student("刘德华",15);
Student s2 = new Student("梁朝伟",16);
Student s3 = new Student("周瑞发",17);
Student s4 = new Student("郭富强",18);
HashSet<Student> set = new HashSet<>();
//1.
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
set.add(new Student("刘德华",15));
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
set.remove(new Student("刘德华",15));
System.out.println("删除之后:"+set.size());
System.out.println(set.toString());
//遍历
for (Student s:set
) {
System.out.println(s);
}
System.out.println("-------------------------------------");
Iterator<Student> iterator = set.iterator();
while (iterator.hasNext()){
Student next = iterator.next();
System.out.println(next);
}
//
// System.out.println(set.isEmpty());
boolean contains = set.contains(s1);
System.out.println(contains);
}
}
补充
- 31是一个质数,减少散列冲突
- 提高执行效率,计算机是一个二进制操作的,通过位移运算减少计算机负担
TreeSet(红黑树)
-
存储结构:红黑树
-
基于排列顺序实现元素不重复
-
实现了sortedset接口,对集合元素自动排序
-
元素对象的类型必须实现Comparabale接口,指定排序规则
-
通过Comparable方法确定是否为重复元素。
Student
package demo02;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
public class Student implements Comparable{
private String name;
private int age;
public Student(String name,int age) {
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 +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name.equals(student.name);
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + age;
return result;
}
@Override
public int compareTo(Object o) {
Student student=(Student) o;
int n1=this.getName().compareTo(student.getName());
int n2=this.getAge()-student.getAge();
return n1==0?n2:n1;
}
}
testTreeSet
package demo05;
import demo02.Student;
import java.util.Iterator;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>();
Student s1 = new Student("刘德华",15);
Student s2 = new Student("梁朝伟",16);
Student s3 = new Student("周瑞发",17);
Student s4 = new Student("郭富强",18);
Student s5 = new Student("郭富强",19);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
//删除
set.remove(new Student("郭富强",18));
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
//foreach
for (Student s:set
) {
System.out.println(s);
}
System.out.println("****************************************");
//diedaiqi
Iterator<Student> iterator = set.iterator();
while(iterator.hasNext()){
Student next = iterator.next();
System.out.println(next);
}
System.out.println(set.contains(new Student("刘德华",15)));
System.out.println(set.isEmpty());
}
}
Comparator:实现定制比较规则(比较器),直接内部类实现方法重写
Comparable:可比较的
package demo05;
import demo02.Student;
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet2 {
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1= o1.getAge()- o2.getAge();
int n2=o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
Student s1 = new Student("刘德华",15);
Student s2 = new Student("梁朝伟",16);
Student s3 = new Student("周瑞发",17);
Student s4 = new Student("郭富强",18);
Student s5 = new Student("郭富强",19);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
System.out.println("元素个数:"+set.size());
System.out.println(set.toString());
}
}
小demo:
package demo05;
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet3 {
public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>(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;
}
});
set.add("aaaaaaaaaaaaaaa");
set.add("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
set.add("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
set.add("bbbbbbbbbbbbbbbbbbbb");
set.add("bbbbbbbbbbbbbbbb");
set.add("mmmmmmmmmmmmmmmm");
System.out.println(set.toString());
}
}
Map集合
特点:
-
- 用于存储任意键值对
- 键:无序,无下标,不允许重复(唯一)
- 值:无序,无下标,允许重复
map父接口的特点:存储一对数据,无序,无下标,键不可以重复,值可以重复
HashMap
默认初始容量16默认加载因子0.75
存储结构:数组+链表+红黑树
类TestHashMap:
package demo06;
import java.util.HashMap;
import java.util.Map;
public class TestHashMap {
public static void main(String[] args) {
HashMap<Student,String > students=new HashMap<>();
Student s1 = new Student("孙悟空", 101);
Student s2 = new Student("猪八戒", 102);
Student s3 = new Student("沙和尚", 103);
//添加
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"杭州");
students.put(new Student("孙悟空",101),"南京");
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//删除
students.remove(s1);
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//遍历
//1--KeySet
for (Student keySet: students.keySet()) {
System.out.println(keySet+"=============="+students.get(keySet));
}
System.out.println("=========================");
//2---entrySet
for (Map.Entry<Student,String> entry:students.entrySet()) {
System.out.println(entry+"=================="+entry.getValue());
}
//判断
System.out.println(students.containsKey(new Student("孙悟空",101)));
System.out.println(students.containsKey(s2));
System.out.println(students.containsValue(101));
}
}
类Student:
package demo06;
public class Student {
private String name;
private int stuNo;
public Student(String name,int stuNo) {
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 +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (stuNo != student.stuNo) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + stuNo;
return result;
}
}
源码分析总结:
- HashMap刚创建时,table是null,为了节省空间,当添加第一个元素时,table调整为16.
- 当元素个数大于阈值(16*0.75=12)时,会进行扩容,扩容后大小是原来的2倍。目的减少调整元素的个数
- jdk1.8,当每个链表长度大于8时,并且数组元素个数大于等于64时,会调整为红黑树,目的调高代码的执行效率
- jdk1.8,当链表长度小于6时,调整为链表
- jdk1.8以前,链表是头插入,jdk1.8之后是尾插入
HashTable
jdk1.0版本,线程安全,运行效率慢;不允许null作为key或者是value
Properties
- Hashtable的子类,要求key和value都是String,通常配置文件的读取
TreeMap
- 存储结构:红黑树
package demo06;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class TestTreeMap {
public static void main(String[] args) {
TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getStuNo() - o2.getStuNo();
return n1;
}
});
Student s1 = new Student("孙悟空", 101);
Student s2 = new Student("猪八戒", 102);
Student s3 = new Student("沙和尚", 103);
map.put(s1,"北京");
map.put(s2,"上海");
map.put(s3,"深圳");
map.put(new Student("刘德华",100),"杭州");
System.out.println("元素个数:"+map.size());
System.out.println(map.toString());
//remove
map.remove(new Student("刘德华",100),"杭州");
System.out.println("元素个数:"+map.size());
System.out.println(map.toString());
//遍历
System.out.println("--------------------------keySet");
for (Student key: map.keySet() ) {
System.out.println(key+"======================"+map.get(key));
}
System.out.println("------------------------entry");
for (Map.Entry<Student,String >entry: map.entrySet() ) {
System.out.println(entry+"---------------------------"+entry.getValue());
}
//判断
System.out.println(map.containsKey(new Student("刘德华",100)));
System.out.println(map.containsValue("上海"));
}
}
Collections工具类
package demo06;
import java.util.*;
public class TestCollections {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
list.add(20);
list.add(5);
list.add(1);
list.add(9);
System.out.println("反转之前:"+list.toString());
Collections.reverse(list);
System.out.println("反转之后:"+list.toString());
Collections.sort(list);
System.out.println("排序之后:"+list.toString());
Collections.shuffle(list);
System.out.println("重新洗牌之后:"+list.toString());
//copy
ArrayList<Integer> list1=new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
list1.add(0);
}
Collections.copy(list1,list);
System.out.println(list1.toString());
//list转数组
Integer[] integers = list.toArray(new Integer[0]);
System.out.println(integers.length);
System.out.println(Arrays.toString(integers));
String []s={"张三","李四","王五"};
List<String> strings = Arrays.asList(s);
System.out.println(strings.toString());
///不允许添加或者删除
}
}