java集合框架
Java集合框架提供了一套性能优良,使用方便的接口和类,他们位于java.util包中。
集合框架包含的内容
接口和具体类
Collection:接口
Collections:工具类(提供了对集合进行排序、遍历等多种算法实现)
-
Collection 接口存储一组不唯一,无序的对象
-
List 接口存储一组不唯一,有序(插入顺序)的对象
-
Set 接口存储一组唯一,无序的对象
-
Map接口存储一组键值对象,提供key到value的映射
注意:Map中的key为Set集合的对象
List接口
-
ArrayList实现了长度可变的数组,在内存中分配连续的空间。遍历元素和随机访问元素的效率比较高
-
LinkedList采用链表的存储方式。插入、删除元素时效率比较高
List接口常用方法
ArrayList
public class News {
private int order;
private String title;
private String author;
public News(int order, String title, String author) {
this.order = order;
this.title = title;
this.author = author;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
public class Student {
private int age;
private String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ListTest {
public static void main(String[] args) {
News news1 = new News(1, "21世纪", "江南");
News news2 = new News(2, "22世纪", "江南");
News news3 = new News(3, "23世纪", "江南");
News news4 = new News(4, "24世纪", "江南");
Student student = new Student(5, "张超");
// 向集合中添加对象
ArrayList arrayList = new ArrayList();
arrayList.add(news1); // 按照顺序添加
arrayList.add(news2);
arrayList.add(news3);
//arrayList.add(student); // 集合中可以加入不同的对象,一般不建议使用,不方便循环取数据
arrayList.add(3,news4); // 在指定下标处添加
int size = arrayList.size();
System.out.println("集合的长度为:====>"+size);
// 获取指定位置的对象
News o = (News) arrayList.get(1);
System.out.println("下标1出的对象为:"+o);
System.out.println("下标1处的新闻编号:"+o.getOrder()+"\t新闻标题:"+o.getTitle()+"\t新闻作者:"+o.getAuthor());
boolean contains = arrayList.contains(news4);
System.out.println("集合中是否包含指定对象?=====>"+contains);
boolean remove = arrayList.remove(news2); // 下标不可越界
System.out.println("是否成功删除指定对象:====>"+remove);
Object remove1 = arrayList.remove(0);
System.out.println("删除指定下标位置的对象:====>"+remove1);
System.out.println("操作完后集合的长度为:"+arrayList.size());
System.out.println("==========普通for循环============");
// 遍历List,获取每个新闻的属性
for (int i = 0; i < arrayList.size(); i++) {
// 返回的是Object类型,这里强转成News类型
News news = (News) arrayList.get(i);
System.out.println("新闻编号:"+news.getOrder()+"\t新闻标题:"+news.getTitle()+"\t新闻作者:"+news.getAuthor());
}
System.out.println("==========增强型for循环==============");
for(Object obj : arrayList){
// 返回的是Object类型,这里强转成News类型
News news = (News) obj;
System.out.println("新闻编号:"+news.getOrder()+"\t新闻标题:"+news.getTitle()+"\t新闻作者:"+news.getAuthor());
}
System.out.println("==========迭代器==============");
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
News news = (News) iterator.next();
System.out.println("新闻编号:"+news.getOrder()+"\t新闻标题:"+news.getTitle()+"\t新闻作者:"+news.getAuthor());
}
}
}
结果
集合的长度为:====>4
下标1出的对象为:com.xie.Array.Demo1.News@1540e19d
下标1处的新闻编号:2 新闻标题:22世纪 新闻作者:江南
集合中是否包含指定对象?=====>true
是否成功删除指定对象:====>true
删除指定下标位置的对象:====>com.xie.Array.Demo1.News@677327b6
操作完后集合的长度为:2
==========普通for循环============
新闻编号:3 新闻标题:23世纪 新闻作者:江南
新闻编号:4 新闻标题:24世纪 新闻作者:江南
==========增强型for循环==============
新闻编号:3 新闻标题:23世纪 新闻作者:江南
新闻编号:4 新闻标题:24世纪 新闻作者:江南
==========迭代器==============
新闻编号:3 新闻标题:23世纪 新闻作者:江南
新闻编号:4 新闻标题:24世纪 新闻作者:江南
注意:集合里不能添加基本数据类型,只能添加对象或引用数据类型。包装类属于对象
LinkedList
LinkedList的特殊方法
public class News {
private int order;
private String title;
private String author;
public News(int order, String title, String author) {
this.order = order;
this.title = title;
this.author = author;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
public class Application {
public static void main(String[] args) {
News news1 = new News(1, "21世纪", "江南");
News news2 = new News(2, "22世纪", "江南");
News news3 = new News(3, "23世纪", "江南");
News news4 = new News(4, "24世纪", "江南");
LinkedList linkedList = new LinkedList();
linkedList.add(news2);
linkedList.add(news3);
linkedList.addFirst(news1);
linkedList.addLast(news4);
News first = (News) linkedList.getFirst();
News last = (News) linkedList.getLast();
System.out.println("第一个对象为:"+first);
System.out.println("最后一个对象为:"+last);
System.out.println("最后一个对象的编号:"+last.getOrder());
System.out.println("==================================");
News o = (News) linkedList.get(3);
System.out.println("下标3处的对象为:"+o);
System.out.println("下标3处的新闻编号:"+o.getOrder());
System.out.println("==================================");
News o1 = (News) linkedList.removeFirst(); // 将第一个删除
News o2 = (News) linkedList.removeLast();
System.out.println(((News) linkedList.getFirst()).getOrder()); // 第一个编号变为了2
System.out.println("操作完后集合的长度为:"+linkedList.size());
System.out.println("=======增强型for循环==========");
for(Object obj: linkedList){
News news = (News)obj;
System.out.println("新闻编号:"+news.getOrder()+"\t新闻标题:"+news.getTitle()+"\t新闻作者:"+news.getAuthor());
}
System.out.println("=======迭代器==========");
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()){
News next = (News) iterator.next();
System.out.println("新闻编号:"+next.getOrder()+"\t新闻标题:"+next.getTitle()+"\t新闻作者:"+next.getAuthor());
}
}
}
结果
第一个对象为:com.xie.Array.Demo2.News@1540e19d
最后一个对象为:com.xie.Array.Demo2.News@677327b6
最后一个对象的编号:4
==================================
下标3处的对象为:com.xie.Array.Demo2.News@677327b6
下标3处的新闻编号:4
==================================
2
操作完后集合的长度为:2
=======增强型for循环==========
新闻编号:2 新闻标题:22世纪 新闻作者:江南
新闻编号:3 新闻标题:23世纪 新闻作者:江南
=======迭代器==========
新闻编号:2 新闻标题:22世纪 新闻作者:江南
新闻编号:3 新闻标题:23世纪 新闻作者:江南
Set接口
- Set接口存储一组唯一,无序的对象
- HashSet是Set接口常用的实现类
- Set中存放对象的引用(地址的意思)
Set中存放对象时会先判断之前是否已经存在,所以是唯一的。
-
采用的equals()方法比较两个对象是否相等
-
Set接口没有get(i)方法,因为它是无序的,没有索引(即不能使用下标的方式获取对象属性)
如何遍历Set集合呢?
- 增强型的for循环
- 通过迭代器实现遍历
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;
}
}
public class Application {
public static void main(String[] args) {
Student s1 = new Student("明华", 10);
Student s2 = new Student("张超", 15);
Student s3 = new Student("当当", 20);
HashSet hashSet = new HashSet();
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(s3);
System.out.println("set集合的总长度为:"+hashSet.size()); // 最后一条有重复,没有添加成功
System.out.println("============增强型的for循环=============");
for (Object obj : hashSet){
Student student = (Student) obj;
System.out.println("姓名:"+student.getName()+",\t年龄:"+student.getAge());
}
System.out.println("============迭代器=============");
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()){
Student next = (Student) iterator.next();
System.out.println("姓名:"+next.getName()+",\t年龄:"+next.getAge());
}
}
}
结果
set集合的总长度为:3
============增强型的for循环=============
姓名:张超, 年龄:15
姓名:明华, 年龄:10
姓名:当当, 年龄:20
============迭代器=============
姓名:张超, 年龄:15
姓名:明华, 年龄:10
姓名:当当, 年龄:20
Map接口
- Map接口专门处理键值映射数据的存储,可以根据键实现对值得操作。HashMap
Map接口的常用方法
遍历Map集合
- 通过迭代器实现遍历
- 增强型for循环
- 键值对
public class Country {
private String name;
private String area;
public Country(String name, String area) {
this.name = name;
this.area = area;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
public class Application {
public static void main(String[] args) {
Country c1 = new Country("中华人民共和国", "亚洲");
Country c2 = new Country("法兰西共和国", "欧洲");
Country c3 = new Country("美利坚合众国", "美洲");
HashMap hashMap = new HashMap();
hashMap.put("CH",c1);
hashMap.put("FR",c2);
hashMap.put("US",c3);
System.out.println(hashMap.size());
Country ch = (Country) hashMap.get("CH");
System.out.println("通过键获取到的国家为:"+ch.getName());
Country fr = (Country) hashMap.remove("FR");
System.out.println("通过键删除的国家为:"+fr.getName());
System.out.println("===============================");
boolean fr1 = hashMap.containsKey("FR");
System.out.println("是否包含FR?===>"+fr1);
boolean ch1 = hashMap.containsValue(c1); // 这里注意值是对象了
System.out.println("是否包含中华人名共和国?==>"+ch1);
System.out.println("====================遍历Map集合===================");
System.out.println("====================增强型的for循环===================");
Set keySet = hashMap.keySet(); // 返回键的集合
for (Object set : keySet){
System.out.println("当前键为:"+set.toString());
Country o = (Country) hashMap.get(set);
System.out.println("国家名称:"+o.getName()+"\t国家所在地区:"+o.getArea());
}
System.out.println("======================================");
Collection values = hashMap.values(); // 返回值的集合
for (Object obj : values){
Country value = (Country) obj;
System.out.println("国家名称:"+value.getName()+"\t国家所在地区:"+value.getArea());
}
System.out.println("====================迭代器===================");
Set keySet1 = hashMap.keySet();
Iterator iterator = keySet1.iterator();
while (iterator.hasNext()){
String next = (String) iterator.next();
Country o = (Country) hashMap.get(next);
System.out.println("国家名称:"+o.getName()+"\t国家所在地区:"+o.getArea());
}
// 清空集合
System.out.println("==============清空集合=============");
hashMap.clear();
if(hashMap.isEmpty()){
System.out.println("集合已被清空");
}
}
}
结果
3
通过键获取到的国家为:中华人民共和国
通过键删除的国家为:法兰西共和国
===============================
是否包含FR?===>false
是否包含中华人名共和国?==>true
====================遍历Map集合===================
====================增强型的for循环===================
当前键为:CH
国家名称:中华人民共和国 国家所在地区:亚洲
当前键为:US
国家名称:美利坚合众国 国家所在地区:美洲
======================================
国家名称:中华人民共和国 国家所在地区:亚洲
国家名称:美利坚合众国 国家所在地区:美洲
====================迭代器===================
国家名称:中华人民共和国 国家所在地区:亚洲
国家名称:美利坚合众国 国家所在地区:美洲
==============清空集合=============
集合已被清空
泛型集合
- List的get(int index)方法获取元素
- Map的get(Object key)方法获取元素
- Iterator的next()方法获取元素
如果不适用泛型,则List,Map,Iterator的get方法获的是Object类型的对象,需要强转,所以引入了泛型。
泛型
将对象的类型作为参数,指定到其他类或者方法上,从而保证类型转换的安全性和稳定性。
泛型集合可以约束集合内的元素类型:
典型泛型集合ArrayList< E>,HashMap<K,V>
< E>,<K,V>表示该泛型集合中的元素类型
泛型集合中的数据不再转换为Object
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;
}
}
public class Application {
public static void main(String[] args) {
Student s1 = new Student("张华",18);
Student s2 = new Student("艾希",20);
Map<String, Student> map = new HashMap();
map.put("S1",s1);
map.put("S2",s2);
Set<String> keys = map.keySet();
for (String str : keys){
Student student = map.get(str); // 没有了强制类型转换
System.out.println("姓名:"+student.getName()+"\t年龄:"+student.getAge());
}
}
}
结果
姓名:张华 年龄:18
姓名:艾希 年龄:20
Collections类
Collections类常用方法:
Collections和Collection不同,前者是集合的操作类,后者是集合的接口。
Collections提供的常用静态方法:
- sort()升序排序
- reverse()翻转排序(降序)
- binarySearch()查找
- max()/min() 最大值/最
public class Application {
public static void main(String[] args) {
ArrayList<String> objects = new ArrayList<>();
objects.add("s2");
objects.add("s3");
objects.add("s1");
System.out.println("=============降序===============");
Collections.reverse(objects);
for (String str : objects){
System.out.println(str);
}
System.out.println("=============升序===============");
Collections.sort(objects);
for (String str : objects){
System.out.println(str);
}
System.out.println("=============查找===============");
// 此方法在升序的情况下才能执行, 存在返回下标,不存在返回一个负数
int i = Collections.binarySearch(objects, "s3");
System.out.println("查找到的下标为:"+i);
System.out.println("=============最值===============");
String max = Collections.max(objects);
String min = Collections.min(objects);
System.out.println("最大值为:"+max);
System.out.println("最小值为:"+min);
}
}
结果
=============降序===============
s1
s3
s2
=============升序===============
s1
s2
s3
=============查找===============
查找到的下标为:2
=============最值===============
最大值为:s3
最小值为:s1
能执行, 存在返回下标,不存在返回一个负数
int i = Collections.binarySearch(objects, "s3");
System.out.println("查找到的下标为:"+i);
System.out.println("=============最值===============");
String max = Collections.max(objects);
String min = Collections.min(objects);
System.out.println("最大值为:"+max);
System.out.println("最小值为:"+min);
}
}
结果
=============降序===============
s1
s3
s2
=============升序===============
s1
s2
s3
=============查找===============
查找到的下标为:2
=============最值===============
最大值为:s3
最小值为:s1
完!!!后续再做补充。