//例如 创建一个 Play 数组
Play[] p = new Play[1]; //大小是1
p[0] = new Play();
//增加新的Play对象
Play[] p2 = new Play[p.length+1];//新建数组
//然后再用循环拷贝p数组的元素到p2中,很繁琐
这个时候就需要用一个比数组要“高级的容器”来解决,这就是集合:
Java的集合类有很多,主要分为两大类:
单列集合:
双列集合:
//单列集合
ArrayList arrayList = new ArrayList();
arrayList.add("Jack");
arrayList.add("Tom");//双列集合
HashMap hashMap = new HashMap();
hashMap.put("No.1","我");
hashMap.put("No.2","你");
Collection接口实现类的特点:
public interface Collection extends Iterable
(----> 这些方法是 List 和 Set 都有的)
以 ArrayList 实现类来演示:
import java.util.ArrayList;
import java.util.List;public class CollectionMethod {public static void main(String[] args) {//1.add:添加单个元素List list = new ArrayList();list.add("字符串");list.add(128);//list.add(new Integer(10))list.add(true);System.out.println("list="+list);//list=[字符串, 128, true]//2.remove:删除指定元素//list.remove(0);//删除第一个元素//System.out.println("list="+list);//list=[128, true]list.remove("字符串");//指定删除某个元素System.out.println("list="+list);//list=[128, true]//3.contains:查找某个元素是否存在System.out.println(list.contains(128));//true//4.size:返回元素个数System.out.println(list.size());//2//5.isEmpty:判断是否为空System.out.println(list.isEmpty());//false//6.clear:清空list.clear();System.out.println("list= "+list);//list= []//7.addAll:添加多个元素ArrayList list2 = new ArrayList();list2.add("开心");list2.add("每");list2.add(1);list2.add("天");list.addAll(list2);//传入一个集合System.out.println("新的list:"+list);//新的list:[开心, 每, 1, 天]//8.containsAll:查找多个元素是否存在System.out.println(list.containsAll(list2));//true//9.removeAll:删除多个元素list.removeAll(list2);System.out.println("list="+list);//list=[]}
}
迭代器执行原理:
Iterator iterator = new coll.iterator(); 得到一个集合迭代器
hasNext() :判断是否还有下一个元素
while(iterator.hasNext()){next()作用:指针下移,将下移后以后集合位置上的元素返回System.out.println(iterator.next());
}
每 next() 一次,箭头下移一次:
注意:在调用 iterator.next( ) 方法之前,必须要调用iterator.hasNext( ) 进行检测;若不调用,且下一条记录无效,直接调用 iterator.next( ) 会抛出 NoSuchElementException异常。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class CollectionIterator {public static void main(String[] args) {Collection col = new ArrayList();col.add(new Book("三国演义","罗贯中",10.1));col.add(new Book("红楼梦","曹雪芹",34.6));col.add(new Book("西游记","吴承恩",28.8));System.out.println("col : "+col);
//col : [Book{name='三国演义', author='罗贯中', price=10.1}, Book{name='红楼梦', author='曹雪芹', price=34.6}, Book{name='西游记', author='吴承恩', price=28.8}]//遍历 col//1.先得到col集合对应的迭代器Iterator iterator = col.iterator();//2.使用while循环遍历//快捷键快速生成while循环 输入itit回车即可//crtl+j 可以查看当前所有快捷键while(iterator.hasNext()){ //判断是否还有数据//next()返回下一个元素,类型是ObjectObject obj = iterator.next();System.out.println(obj);//Book{name='三国演义', author='罗贯中', price=10.1}//Book{name='红楼梦', author='曹雪芹', price=34.6}//Book{name='西游记', author='吴承恩', price=28.8}//3.当退出while循环后,此时iterator迭代器指向最后的元素//iterator.next(); --> NoSuchElementException//4.若还要使用迭代器,需要重置迭代器iterator = col.iterator(); }}
}class Book{private String name;private String author;private double price;public Book(String name, String author, double price) {this.name = name;this.author = author;this.price = price;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +'}';}
}
增强for循环,可以代替 iterator迭代器,特点:增强for循环就是简化版的iterator,本质一样,只能用于遍历集合或数组;
基本语法:
for(元素类型 元素名 : 集合名或数组名){访问元素
}
import java.util.ArrayList;
import java.util.Collection;public class CollectionIterator {public static void main(String[] args) {Collection col = new ArrayList();col.add(new Book("三国演义","罗贯中",10.1));col.add(new Book("红楼梦","曹雪芹",34.6));col.add(new Book("西游记","吴承恩",28.8));//增强for循环 不仅可以用于集合,数组也同样适用//底层仍然是迭代器iterator 相当于简化版迭代器//快捷键 输入I后回车for (Object book:col) {System.out.println(book);}//Book{name='三国演义', author='罗贯中', price=10.1}//Book{name='红楼梦', author='曹雪芹', price=34.6}//Book{name='西游记', author='吴承恩', price=28.8}}
}class Book{private String name;private String author;private double price;public Book(String name, String author, double price) {this.name = name;this.author = author;this.price = price;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +'}';}
}
//1.List集合类中元素有序(即添加和取出顺序一致),且可重复List list = new ArrayList();list.add("Jack");list.add("Tom");list.add("Marry");list.add("Marry");System.out.println(list);//[Jack, Tom, Marry, Marry]取出输出顺序和存放顺序一致,且可重复//2.List集合中的每个元素都有其对应的顺序索引,即支持索引System.out.println(list.get(2));//Marry//3.List容器中的元素都对应一个整数型的序号记其在容器中的位置,可以根据序号存取容器中的元素
更多方法可以自行JDK API在线查询 或 下载中文版JavaAPI帮助文档【免费0积分下载】
//向上转型,用List来接收ArrayList
List list = new ArrayList();//1. void add (int index,Object ele) :在index位置插入ele元素;
list.add("开心的你");
list.add(0,"帅气的我");//在0位置插入
System.out.println(list);//[帅气的我, 开心的你]//2. boolean addAll (int index,Collection eles) :从index位置开始将eles集合中的所有元素添加进来;
List list1 = new ArrayList();
list1.add("Jack");list1.add("Tom");list1.add("Marry");
list.addAll(1,list1);
System.out.println(list);//[帅气的我, Jack, Tom, Marry, 开心的你]//3. Object get (int index) :获取指定index位置的元素;
System.out.println(list.get(0));//帅气的我//4. int indexOf (Object obj) :返回obj在集合中首次出现的位置;
System.out.println(list.indexOf("开心的你"));//4//5. int lastIndexOf (Object obj) :返回obj在集合中末次出现的位置;
list.add("Jack");
System.out.println(list.lastIndexOf("Jack"));//5//6. Object remove (int index) :移除指定index位置的元素,并返回此元素;
System.out.println(list.remove(5));//Jack
System.out.println(list);//[帅气的我, Jack, Tom, Marry, 开心的你]//7. Object set (int index,Object ele) :设置指定index的位置的元素为ele,相当于是替换;
list.set(1,"!!!");
System.out.println(list);//[帅气的我, !!!, Tom, Marry, 开心的你]//8. List subList (int fromIndex,int toIndex) :返回从fromIndex到toIndex位置的子集合;
//返回的子集合: [fromIndex,toIndex) 左闭右开
System.out.println(list.subList(2,4));//[Tom, Marry]
import java.util.*;public class ListFor {public static void main(String[] args) {//List的实现接口子类ArrayList LinkedList Vector//List list = new ArrayList();//List list = new LinkedList();List list = new Vector();list.add("熊大");list.add("熊二");list.add("光头强");//迭代器iterator遍历Iterator iterator = list.iterator();while(iterator.hasNext()){Object next = iterator.next();System.out.println(next);}//增强for遍历for (Object o:list) {System.out.println(o);}//普通遍历for (int i=0;iSystem.out.println(list.get(i));}}
}
import java.util.ArrayList;
import java.util.List;public class Demo {public static void main(String[] args) {//List list = new ArrayList();//List list = new LinkedList();List list = new Vector();list.add(new Book("红楼梦", 36.4f, "曹雪芹"));list.add(new Book("水浒传", 19.9f, "施耐庵"));list.add(new Book("西游记", 28.8f, "吴承恩"));//遍历for(Object o:list){System.out.println(o);}//冒泡排序sort(list);System.out.println("---- 排序后 ----");for(Object o:list){System.out.println(o);}}//静态方法:冒泡排序//要求价格从小到大public static void sort(List list){for (int i=0;ifor (int j=0;j//取出对象bookBook book1 = (Book) list.get(j);Book book2 = (Book) list.get(j+1);if(book1.getPrice()>book2.getPrice()){//交换list.set(j,book2);list.set(j+1,book1);}}}}
}
class Book{private String name;private float price;private String author;@Overridepublic String toString() {return "书名: "+name+" 价格: "+price+" 作者: "+author;}public Book(String name, float price, String author) {this.name = name;this.price = price;this.author = author;}public void setName(String name) {this.name = name;}public void setPrice(float price) {this.price = price;}public void setAuthor(String author) {this.author = author;}public String getName() {return name;}public float getPrice() {return price;}public String getAuthor() {return author;}
}
ArrayList list = new ArrayList();
list.add(null);
list.add("OK");
list.add(null);
System.out.println(list);
//[null,OK,null]
双向链表的模拟:
public class TestLinkedList01 {public static void main(String[] args) {//模拟一个简单的双向链表Node jack = new Node("Jack");Node tom = new Node("Tom");Node marry = new Node("Marry");//连接三个节点,形成双向链表//jack -> tom -> marryjack.next = tom;tom.next = marry;//jack <- tom <- marrymarry.pre = tom;tom.pre = jack;Node first = jack;//让first引用指向jack,就是双向链表的首节点Node last = marry;//让last引用指向marry,就是双向链表的尾节点//演示 从头到尾 遍历System.out.println("--------- 从头到尾的遍历 --------");while(true){if(first == null){break;}//输出first信息System.out.println(first);first = first.next;//输出完以后,first指向下一个/*Node name = JackNode name = TomNode name = Marry进程已结束,退出代码0*/}//从尾到头的遍历System.out.println("--------- 从尾到头遍历 --------");while(true){if(last == null){break;}//输出last信息System.out.println(last);last = last.pre;//输出完以后,first指向下一个/*Node name = MarryNode name = TomNode name = Jack*/}//演示链表的添加对象/数据//在tom和marry之间插入一个对象//1.先创建一个Node节点,name为smithNode smith = new Node("Smith");//2.把smith加入双向链表smith.next = marry;smith.pre = tom;marry.pre = smith;tom.next = smith;//3.让first再次指向jackfirst = jack;//演示 从头到尾 遍历System.out.println("--------- 插入smith后 从头到尾的遍历 --------");while(true){if(first == null){break;}//输出first信息System.out.println(first);first = first.next;//输出完以后,first指向下一个}/*Node name = JackNode name = TomNode name = SmithNode name = Marry*/}}//定义一个Node类,Node对象表示双向链表的一个节点
class Node{public Object item;//真正存放数据的地方public Node next;//指向下一个节点public Node pre;//指向前一个节点public Node(Object name){this.item = name;}public String toString(){return "Node name = "+item;}
}
LinkedList的增删改查案例:
import java.util.Iterator;
import java.util.LinkedList;public class LinkListCRUD {public static void main(String[] args) {LinkedList linkedList = new LinkedList();//增linkedList.add(1);//size=0添加一个新节点,首尾指针都指向这个新节点linkedList.add(2);//last指向新节点,first还是指向第一个节点,next指向新节点linkedList.add(3);System.out.println("增后: "+linkedList);//删linkedList.remove();//默认删除第一个System.out.println("删后: "+linkedList);//就是去掉指针//改linkedList.set(1,999);System.out.println("改后: "+linkedList);//查//get(1) 得到双向链表的第二个对象Object o = linkedList.get(1);System.out.println(o);//999//因为LinkedList是实现了List接口,所以遍历方式:Iterator iterator = linkedList.iterator();while (iterator.hasNext()) { //快捷输入ititObject next = iterator.next();System.out.println(next);}//还有增强for 和普通for 遍历}
}
(可以自行debug看一下调用方法的实现)
集合 | 底层结构 | 增删的效率 | 改查的效率 |
---|---|---|---|
ArrayList | 可变数组 | 较低,数组扩容 | 较高 |
LinkedList | 双向链表 | 较高,通过链表追加 | 较低 |
如何选择 ArrayList 和 LinkedList :
多线程的情况还是考虑 Vector ,因为它是线程安全的
Set 接口介绍:
Set 接口的常用方法
Set 接口的遍历方式
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;public class SetMethod {public static void main(String[] args) {//以Set接口的实现类 HashSet 来演示//Set接口的实现类对象(Set接口对象),不能存放重复元素//Set接口对象存放和读取数据无序//取出的顺序虽然不是添加的顺序,但是,是固定有序的Set set = new HashSet();set.add("John");set.add("Lucy");set.add("Jack");set.add(null);set.add(null);System.out.println(set);//[null, John, Lucy, Jack] 执行多遍都是这个结果set.remove(null);//等常用方法可以依照Colleciotn常用方法,是一致的//遍历:迭代器Iterator iterator = set.iterator();while(iterator.hasNext()){Object o = iterator.next();System.out.println(o);}//遍历:增强for (底层还是迭代器)for(Object o:set){System.out.println(o);}//不能索引遍历,且set接口对象没有get()方法}
}
import java.util.HashSet;public class HashSet01 {public static void main(String[] args) {HashSet hashSet = new HashSet();//1.在执行add方法后,会返回一个boolean值//2.如果添加成功,返回true,否则返回falseSystem.out.println(hashSet.add("john"));//trueSystem.out.println(hashSet.add("lucy"));//trueSystem.out.println(hashSet.add("john"));//falseSystem.out.println(hashSet.add("jack"));//trueSystem.out.println(hashSet.add("rose"));//truehashSet.remove("john");//指定删除某对象System.out.println("hashset = "+hashSet);//hashset = [rose, lucy, jack]hashSet = new HashSet();//HashSet不能添加相同的元素、数据hashSet.add("lucy");//添加成功hashSet.add("lucy");//加入不了hashSet.add(new Dog("tom"));//OKhashSet.add(new Dog("tom"));//也能加入System.out.println("hashset = "+hashSet);//hashset = [Dog{name='tom'}, lucy, Dog{name='tom'}]//经典面试题hashSet.add(new String("ok"));//可以加入hashSet.add(new String("ok"));//无法加入System.out.println("hashset = "+hashSet);//hashset = [Dog{name='tom'}, ok, lucy, Dog{name='tom'}]//看源码 add到底发生了什么 --》底层机制}
}
class Dog{private String name;public Dog(String name) {this.name = name;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +'}';}
}
HashSet 底层其实是HashMap,HashMap底层是(数组+链表+红黑树)
模拟数组+链表结构:
public class HashSetStructure {public static void main(String[] args) {//模拟一个HashSet的底层(HashMap)//1.创建一个数组,数组的类型是Node[]//2.Node[] 也称为一个表Node[] table = new Node[16];//3.创建一个节点Node john = new Node("john", null);table[2] = john;Node jack = new Node("jack", null);john.next = jack;//将节点挂载到johnNode rose = new Node("rose",null);jack.next = rose;//将rose节点挂载到jackNode lucy = new Node( "lucy",null);table[3] = lucy;//把lucy放到table表的索引为3的位置System.out.println("table = "+table);}
}
class Node{//节点,存储数据,可以指向下一个节点,从而形成链表Object item;//存放数据Node next;//指向下一个节点public Node(Object item, Node next) {this.item = item;this.next = next;}
}
debug后解读:
HashSet底层机制:
用例:定义一个Employee类,该类包含:private成员属性name,age
要求:1.创建3个Employee对象放入HashSet中;2.当name和age的值相同时,认为是相同员工,不能添加到HashSet集合中;
import java.util.HashSet;
import java.util.Objects;public class HashSet_Exercise {/*** 定义一个Employee类,该类包含:private成员属性name,age* 1.创建3个Employee对象放入HashSet中;* 2.当name和age的值相同时,认为是相同员工,不能添加到HashSet集合中;*/public static void main(String[] args) {HashSet hashSet = new HashSet();hashSet.add(new Employee("jack",18));hashSet.add(new Employee("tom",28));hashSet.add(new Employee("rose",18));//加入了三个成员System.out.println(hashSet);//[Employee{name='jack', age=18}, Employee{name='rose', age=18}, Employee{name='tom', age=28}]}
}//创建Employee
class Employee{private String name;private int age;public Employee(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;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", age=" + age +'}';}//如果name和age相同,则返回相同的hash值@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Employee employee = (Employee) o;return age == employee.age && Objects.equals(name, employee.name);}//name和age相同,hashcode相同@Override public int hashCode() {return Objects.hash(name, age);}
}
import java.util.LinkedHashSet;
import java.util.Set;public class LinkedHashSetSource {public static void main(String[] args) {//LinkedHashSet底层机制Set set = new LinkedHashSet();set.add(new String("OK"));set.add(128);set.add(128);set.add(new Customer("靳",1201));set.add("JinYu");System.out.println(set);//[OK, 128, com.study.set_.Customer@677327b6, JinYu]/*1.添加元素和取出顺序一致2.LinkedHashSet底层维护的是一个LinkedHashMap(是HashMap的子类)3.LinkedHashSet底层结构:数组table+双向链表4.添加第一次时,直接将数组table扩容到16,存放的结点类型是LinkedHashSetMap$Entry5.数组是HashMap$Node[] 存放的元素/数据是LinkedHashSetMap$Entry类型*/}
}
class Customer{private String name;private int id;public Customer(String name, int id) {this.name = name;this.id = id;}
}
示例:Car类(属性name,price),如果name和price一样,则认为是相同元素,就不能添加
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;public class LinkedHashSetExercise {public static void main(String[] args) {Set set = new LinkedHashSet();set.add(new Car("奥拓",1000));set.add(new Car("奥迪",300000));set.add(new Car("法拉利",9000000));set.add(new Car("奥迪",300000));set.add(new Car("保时捷",1000));set.add(new Car("奥迪",300000));System.out.println(set);/* 未重写equals和hashCode方法:[Car{name='奥拓', price=1000.0}, Car{name='奥迪', price=300000.0}, Car{name='法拉利', price=9000000.0}, Car{name='奥迪', price=300000.0}, Car{name='保时捷', price=1000.0}, Car{name='奥迪', price=300000.0}]*//* 重写equals和hashCode方法后:[Car{name='奥拓', price=1000.0}, Car{name='奥迪', price=300000.0}, Car{name='法拉利', price=9000000.0}, Car{name='保时捷', price=1000.0}]*/}
}
class Car{private String name;private double price;public Car(String name, double price) {this.name = name;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Car{" +"name='" + name + '\'' +", price=" + price +'}'+"\n";}//重写equals方法和hashCode方法//当name和price相同时,返回相同的hashCode值@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Car car = (Car) o;return Double.compare(car.price, price) == 0 && Objects.equals(name, car.name);}@Overridepublic int hashCode() {return Objects.hash(name, price);}
}
TreeSet的独特之处在于它的构造器可以传入比较器,所以TreeSet常用来排序,
TreeSet 底层是 TreeMap
import java.util.Comparator;
import java.util.TreeSet;public class TreeSet_ {public static void main(String[] args) {TreeSet treeSet = new TreeSet();//无参构造,默认排序//添加数据treeSet.add("Jack");treeSet.add("Tom");treeSet.add("Ayo");treeSet.add("Luck");System.out.println(treeSet);//默认排序:首字母ASCII由小到大//[Ayo, Jack, Luck, Tom]//如果我们想按字符串大小排序//使用TreeSet提供的一个构造器,传入一个比较器(匿名内部类)指定排序规则treeSet = new TreeSet(new Comparator() {@Overridepublic int compare(Object o1, Object o2) {return ((String)o2).compareTo((String)o1);//利用String类的compareTo方法,由大到小//如果是按照长度由大到小:return ((String)o1).length()-((String)o2).length();}//构造器把传入的比较器对象,赋给了TreeSet的底层的TreeMap的属性this.comparator});treeSet.add("Jack");treeSet.add("Tom");treeSet.add("Ayo");treeSet.add("Luck");System.out.println(treeSet);//[Tom, Luck, Jack, Ayo]}
}
Map为双列集合,Set集合的底层也是Map,只不过有一列是常量所占,只使用到了一列。
import java.util.HashMap;
import java.util.Map;public class Map_ {//分析Map接口实现类的特点public static void main(String[] args){//1. Map 与 Collection 并列存在,用于保存具有映射关系的数据:Key - Value;Map map = new HashMap();map.put("No.1","我");//Key-Valuemap.put("No.2","你");// K-Vmap.put("No.3","他");// K-VSystem.out.println(map);//{No.2=你, No.1=我, No.3=他}//2. Map 中的 Key 和 Value 可以是任何引用类型的数据,会封装到 HashMap$Node对象中//3. Map中的 Key 不允许重复,原因和HashSet一样//4.Map 中的 Value 可以重复map.put("No.2","X"); //替换机制map.put("No.4","他");System.out.println(map);//{No.2=X, No.1=我, No.4=他, No.3=他}//5. Map 的 Key 可以为 null,value 也可以为 null,但 key 为 null 只能有一个;map.put("null","1");map.put("null","2");map.put("No.2","null");map.put("No.3","null");System.out.println(map);//{No.2=null, No.1=我, No.4=他, No.3=null, null=2}//6. 常用 String 类作为 Map 的 key,当然,其他类型也可以,但不常用;//7. Key 和 Value 之间存在单向一对一关系,即通过指定的 Key 总能找到对应的 Value;//通过get方法,传入key,会返回对应的valueSystem.out.println(map.get("No.1"));//我}
}
import java.util.HashMap;
import java.util.Map;//演示 Map 接口常用方法
public class MapMethod {public static void main(String[] args) {Map map = new HashMap();//put方法:添加元素map.put("海绵宝宝","章鱼哥");map.put("海绵宝宝","派大星");map.put("熊大","熊二");map.put("大头儿子","小头爸爸");map.put("黑猫警长",null);map.put(null,"奥特曼");System.out.println(map);//{黑猫警长=null, null=奥特曼, 大头儿子=小头爸爸, 熊大=熊二, 海绵宝宝=派大星}//remove方法:根据键删除映射关系map.remove(null);System.out.println(map);//{黑猫警长=null, 大头儿子=小头爸爸, 熊大=熊二, 海绵宝宝=派大星}//get方法:根据键获取System.out.println(map.get("海绵宝宝"));//派大星//size方法:获取元素个数System.out.println(map.size());//4//isEmpty方法:判断个数是否为0System.out.println(map.isEmpty());//false//containsKey方法:查找键是否存在System.out.println(map.containsKey("黑猫警长"));//true//clear方法:清空map.clear();System.out.println(map);//{}}
}
import java.util.*;public class MapFor {public static void main(String[] args) {Map map = new HashMap();map.put("海绵宝宝","派大星");map.put("熊大","熊二");map.put("大头儿子","小头爸爸");map.put("黑猫警长",null);map.put(null,"奥特曼");//第一种:先取出所有的Key,通过Key取出对应的valueSet keySet = map.keySet();//(1)增强forfor(Object key : keySet){System.out.println(key+" - "+map.get(key));}//(2)迭代器Iterator iterator = keySet.iterator();while (iterator.hasNext()) {Object key = iterator.next();System.out.println(key+" - "+map.get(key));}//第二种:把所有的value取出Collection values = map.values();//然后遍历Collection就行//(1)增强forfor(Object value : values){System.out.println(value);}//(2)迭代器Iterator iterator1 = values.iterator();while (iterator1.hasNext()) {Object value = iterator1.next();System.out.println(value);}//第三种:通过EntrySet来获取Set entrySet = map.entrySet();//(1)增强forfor(Object entry : entrySet){//将entry转成map.EntryMap.Entry m = (Map.Entry) entry;System.out.println(m.getKey()+" - "+m.getValue());}//(2)迭代器Iterator iterator2 = entrySet.iterator();while (iterator2.hasNext()) {Object next = iterator2.next();//向下转型 Map.EntryMap.Entry m = (Map.Entry) next;System.out.println(m.getKey()+" - "+m.getValue());}}
}
使用 HashMap 添加3个员工对象,要求:
键:员工id
值:员工对象
并遍历显示工资 > 18000的员工
(员工类:姓名,工资,员工id)
import java.util.*;public class MapExercise {public static void main(String[] args) {//创建、添加HashMap hashMap = new HashMap();hashMap.put(1,new Emp("Jack",30000,1));hashMap.put(2,new Emp("Tom",20000,2));hashMap.put(3,new Emp("Milan",12000,3));//遍历一:使用keySet -> 增强forSet keySet = hashMap.keySet();for(Object key : keySet){//先获取valueEmp emp = (Emp) hashMap.get(key);//薪水大于18000就打印if(emp.getSal() > 18000){System.out.println(emp);}}//遍历二:使用EntrySet -> 迭代器Set entrySet = hashMap.entrySet();Iterator iterator = entrySet.iterator();while (iterator.hasNext()) {Map.Entry entry = (Map.Entry)iterator.next();//通过entry取得key和valueEmp emp = (Emp) entry.getValue();if(emp.getSal() > 18000){System.out.println(emp);}}}
}
class Emp{private String name;private double sal;private int id;public Emp(String name, double sal, int id) {this.name = name;this.sal = sal;this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "Emp{" +"name='" + name + '\'' +", sal=" + sal +", id=" + id +'}';}
}
Hashtable的基本介绍:
Hashtabel table = new Hashtable();
table.put("John",100);//OK
table.put(null,100);//异常 NullPointerException
table.put("",null);//异常
table.put("John",128);//替换
Hashtable的底层原理:
1.底层有数组 Hashtables$Entry[] 初始化大小为1;
2.临界值 threshold 8 = 11 * 0.75;
3.扩容机制:执行方法 addEntry(hash,key,value,index);添加 K-V,封装到Entry;
4.当 if(count >= threshold) 满足就扩容;
5.按照 int newCapacity = (oldCapacity << 1)+1; 扩容
对比 | 线程安全(同步) | 效率 | 允许 null 键 null 值 |
---|---|---|---|
HashMap | 不安全 | 高 | 可以 |
Hashtable | 安全 | 较低 | 不可以 |
TreeMap 构造器可以传入比较器,所以TreeMap常用来排序,可以自定义存放数据顺序。
import java.util.Comparator;
import java.util.TreeMap;public class TreeMap_ {public static void main(String[] args) {TreeMap treeMap = new TreeMap();//默认构造器,默认比较:自然排序treeMap.put("Jack","杰克");treeMap.put("Tom","汤姆");treeMap.put("Smith","史密斯");System.out.println(treeMap);//{Jack=杰克, Smith=史密斯, Tom=汤姆}由小到大排序//如果按照传入的key由大到小排序:treeMap = new TreeMap(new Comparator() {@Overridepublic int compare(Object o1, Object o2) {return ((String)o2).compareTo((String)o1);//如果是按照长度由大到小:return ((String)o1).length()-((String)o2).length(); }});treeMap.put("Jack","杰克");treeMap.put("Tom","汤姆");treeMap.put("Smith","史密斯");System.out.println(treeMap);//{Tom=汤姆, Smith=史密斯, Jack=杰克} 由大到小排序}
}
👉 Properties博客介绍
均为static方法:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;public class Collections_ {public static void main(String[] args) {//创建ArrayList集合,用于测试List list = new ArrayList();list.add("Jack");list.add("Tom");list.add("Smith");list.add("Rose");//reverse(List):反转List集合中的元素顺序Collections.reverse(list);System.out.println(list);//[Rose, Smith, Tom, Jack]//shuffle(List):对List集合进行随机排序Collections.shuffle(list);System.out.println(list);//[Rose, Tom, Smith, Jack]//sort(List):根据元素的自然顺序对指定的List集合元素按升序排序Collections.sort(list);System.out.println(list);//[Jack, Rose, Smith, Tom]//sort(List,Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序;Collections.sort(list,new Comparator(){@Overridepublic int compare (Object o1,Object o2){//根据字符串长度大小排序return ((String)o1).length()-((String)o2).length();}});System.out.println(list);//[Tom, Jack, Rose, Smith]//swap(List,i,j): 将指定List集合中的i处元素和j处元素进行交换Collections.swap(list,1,2);System.out.println(list);//[Tom, Rose, Jack, Smith]}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;public class Collections__ {public static void main(String[] args) {//创建ArrayList集合,用于测试List list = new ArrayList();list.add("Jack");list.add("Tom");list.add("Smith");list.add("Rose");// Object max ( Collection ) :根据元素的自然顺序,返回给定集合中的最大元素;System.out.println("自然顺序最大值 = "+ Collections.max(list));//Tom// Object max ( Collection , Comparator ) :根据 Comparator 指定的顺序,返回给定集合中的最大元素;//返回长度最大的元素:Collections.max(list, new Comparator() {@Overridepublic int compare(Object o1 , Object o2){return ((String)o1).length()-((String)o2).length();}});// Object min ( Collection )// Object min ( Collection , Comparator)// int frequency ( Collection , Object ) : 返回指定集合中指定元素的出现次数;System.out.println(Collections.frequency(list,"Rose"));//1list.add("Rose");System.out.println(Collections.frequency(list,"Rose"));//2// void copy ( List dest , List src ) : 将 src 中的内容复制到 dest 中;//为了完成一个完整拷贝,需要先给dest赋值,大小和list.size()一样ArrayList dest = new ArrayList();for(int i=0;idest.add(null);}Collections.copy(dest,list);System.out.println(dest);//[Jack, Tom, Smith, Rose, Rose]// boolean replaceAll ( List list , Object oldVal , Object newVal ) : 使用新值替换List对象的所有旧值;//如果list中有tom,就替换为汤姆Collections.replaceAll(list,"Tom","汤姆");System.out.println(list);//[Jack, 汤姆, Smith, Rose, Rose]}
}
在实际开发中,选择什么集合实现类,主要取决于业务操作的特点,然后根据集合实现类特性进行选择:
先判断存储的类型(一组对象或一组键值对):