今日目标:
1.List集合
2.Set集合
3.Map集合
1.为什么要使用集合
思考:1.集合是取代数组的吗,二者有啥区别?
数组特点:类型固定,长度固定
类的特点:不同类型的成员属性,共同描述一个实体类
集合特点:类型不固定,长度也不固定,随意存放任何数据
2.认识集合框架的继承结构
3.ArrayList
集合没有固定长度:
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = Arrays.copyOf(arr1, 5);
int[] arr3 = Arrays.copyOf(arr1, 10);
for(int i = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
System.out.println();
for(int i = 0; i < arr3.length; i++)
System.out.print(arr3[i] + " ");
运行结果:
1 2 3 4 5
1 2 3 4 5 0 0 0 0 0
常用方法
ArrayList a1 = new ArrayList();
a1.add(5);
a1.add("hello world");
a1.add(5.3);
System.out.println(a1);
a1.add(0,100);
System.out.println(a1);
System.out.println(a1.get(2));
System.out.println(a1.indexOf(5.3));
a1.set(0,111);
System.out.println(a1);
System.out.println(a1.remove(3));
System.out.println(a1);
System.out.println(a1.isEmpty());
System.out.println(a1.contains("hello world"));
System.out.println(a1.contains(111));
System.out.println("--------------");
ArrayList a2 = new ArrayList();
a2.add("你好");
System.out.println("a2="+a2);
a1.addAll(2,a2);
System.out.println("a2="+a2);
System.out.println("a1="+a1);
a1.addAll(a2);
System.out.println("a2="+a2);
System.out.println("a1="+a1);
a1.removeAll(a2);
System.out.println("a1="+a1);
补充:
1.集合和数组的相互转换
2.遍历集合
ArrayList list=new ArrayList();
list.add("大家好");
list.add(666);
System.out.println(list);
Object[] o=list.toArray();
System.out.println(Arrays.toString(o));
Object[]objs=new Object[10];
Object[] objs2=list.toArray(objs);
System.out.println(Arrays.toString(objs2));
Object[] objs = new Object[]{"hello",666,36.9};
List list= Arrays.asList(objs);
System.out.println(list);
List list = new ArrayList<>();
list.add("hello");
list.add(666);
System.out.println(list);
for (int i = 0; i <list.size() ; i++) {
System.out.println(list.get(i));
}
for (Object o:list){
System.out.println(o);
}
Iterator it=list.iterator();//迭代器
while (it.hasNext()){
System.out.println(it.next());
}
案例:创建一个集合,存入两个元素,“曹操” “貂蝉” ,遍历集合,如果发现遍历的元素是貂蝉,就向集合中添加一个“吕布”
public static void main(String[] args) {
ArrayList a1=new ArrayList();
a1.add("曹操");
a1.add("貂蝉");
for (int i=0;i<a1.size();i++){
System.out.println(a1.get(i));//a1.get(i) 对指定位置进行打印
if (a1.get(i).equals("貂蝉")){
a1.add("吕布");
}
}
System.out.println(a1);
a1.set(2,"hh");//对指定位置进行一个修改
System.out.println(a1);
/* for (Object o:a1) {
System.out.println(o);
}
Iterator it=a1.iterator();
while (it.hasNext()){
System.out.println(it.next());
}*/
}
4.LinkedList
存储原理是一个链表,在元素的前后分别有一个前置结点和后置结点,用于连接集合中的上一个元素和下一个元素,依次“手拉手”,构成一条链式数据的集合。
特有的方法
LinkedList li = new LinkedList();
li.add("Hello");
li.add("Word");
System.out.println(li);
li.addFirst(123);
li.addLast("456");
System.out.println(li);
System.out.println("---------");
System.out.println(li.getFirst());
System.out.println(li.getLast());
System.out.println(li);
System.out.println("---------");
System.out.println(li.removeFirst());
System.out.println(li.removeLast());
System.out.println(li);
理解集合数据存储
5.两个List集合的对比
1.List 接口存储一组不唯一(可以重复),有序(插入顺序)的对象
2.ArrayList实现了长度可变的Object类型数组,在内存中分配连续的空间,遍历元素和随机访问元素的效率比较高
3.LinkedList采用链表存储方式,插入、删除元素时效率比较高
4.通过查看添加方法的源码,比较二者结构的区别。链式存储和数组存储
效率不同的原因:
Linkedlist在执行删除和添加操作时,只会对相邻的两个元素有影响,
Arraylist由于它的本质是一个数组,在执行删除和添加操作时,后面所有的元素的地址值都将出现移位。
6.Set集合
1.Set接口特点:
存储一组唯一(不允许出现重复的元素),无序(没有index下标)的对象,HashSet是Set接口常用的实现类。
2.如何操作Set集合的数据呢?
(1)新增元素(set.add())
(2)删除元素(set.remove())
(3)修改元素(不能修改)
(4)获取元素(不能获取)
3.如何遍历Set集合呢?(for循环,迭代器)
(1)While和for可以吗
(2)增强for呢
(3)还有其它方式吗?迭代器(跟while循环是固定搭配)
课堂练习:
1.金角大王有一个紫金葫芦,里面装进了“孙行者”,“行者孙”,“者行孙”,现在来了一个“孙悟空”,他说:先放出我的朋友“孙行者”,再把我装进去。
请用HashSet和Monkey类实现以上需求。
public class Monkey {
private String name;
@Override
public String toString() {
return name;
}
public Monkey() {
}
public Monkey(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class TestMonkey {
public static void main(String[] args) {
Monkey m1 = new Monkey("孙行者");
Monkey m2 = new Monkey("行者孙");
Monkey m3 = new Monkey("者行孙");
HashSet h1 = new HashSet();
h1.add(m1);
h1.add(m2);
h1.add(m3);
System.out.println(h1);
h1.remove(m1);
h1.add("孙悟空");
System.out.println(h1);
/*for (Object o:h1){
System.out.println(o);
}
Iterator it=h1.iterator();
while (it.hasNext()){
System.out.println(it.next());
}*/
}
}
2.定义一个字符串“我爱北京天安门,天安门上太阳升”,要求使用HashSet元素不重复的特点,过滤掉字符串中重复出现的文字。可以结合StringBuilder实现。
public static void main(String[] args) {
String s="我爱北京天安门,天安门上太阳升";
StringBuilder sbu=new StringBuilder();
char a[]=s.toCharArray();
HashSet h1 = new HashSet();
for (int i = 0; i <s.length() ; i++) {
if (h1.add(a[i])){
sbu.append(a[i]);
}
}
System.out.println(sbu);
}
课堂案例
新闻管理
1.创建多个新闻标题对象NewsTitle(id,name,isHot) 编号,标题名称,是否热门
2.分别使用ArrayList,LinkedList,HashSet存储5个新闻标题
3.获取第1个和第3个,最后一个新闻标题
4.删除第4个新闻标题
5.使用尽可能多的循环遍历方式,分别遍历输出3个集合中的新闻信息
public class Title {
private int id;
private String name;
private String isHot;
@Override
public String toString() {
return "Title{" +
"id=" + id +
", name='" + name + '\'' +
", isHot='" + isHot + '\'' +
'}';
}
public Title() {
}
public Title(int id, String name, String isHot) {
this.id = id;
this.name = name;
this.isHot = isHot;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsHot() {
return isHot;
}
public void setIsHot(String isHot) {
this.isHot = isHot;
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
public class TestTitle {
public static void main(String[] args) {
Title t1 = new Title(001, "关注", "热门");
Title t2 = new Title(002, "推荐", "热门");
Title t3 = new Title(003, "视频", "不热门");
Title t4 = new Title(004, "好友", "不热门");
Title t5 = new Title(005, "小说", "不热门");
ArrayList list = new ArrayList();
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
System.out.println(list);
LinkedList link = new LinkedList();
link.add(t1);
link.add(t2);
link.add(t3);
link.add(t4);
link.add(t5);
System.out.println(link);
HashSet set = new HashSet();
set.add(t1);
set.add(t2);
set.add(t3);
set.add(t4);
set.add(t5);
System.out.println(set);
System.out.println("3----------------");
System.out.println(link.getFirst());
System.out.println(link.get(2));
System.out.println(link.getLast());
System.out.println("4----------------");
list.remove(3);
link.remove(3);
set.remove(t4);
System.out.println(list);
System.out.println(link);
System.out.println(set);
System.out.println("5----------------");
for (int i = 0; i <list.size() ; i++) {
System.out.println(list.get(i));
}
for(Object o:list){
System.out.println(o);
}
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
System.out.println("-------------");
for (int i = 0; i <link.size() ; i++) {
System.out.println(link.get(i));
}
for(Object o:link){
System.out.println(o);
}
Iterator it1 = link.iterator();
while (it1.hasNext()){
System.out.println(it1.next());
}
System.out.println("-------------");
for(Object o:set){
System.out.println(o);
}
Iterator it2 = set.iterator();
while (it2.hasNext()){
System.out.println(it2.next());
}
}
}
7.Map集合
1.Map接口专门处理键值映射的数据,可以根据键实现对值的操作
2.创建Map接口的常用实现类HashMap对象,存入数据
3.删除数据
4.修改数据
5.遍历map集合的数据:
(1).遍历所有的键
(2).遍历所有的值
(3).遍历所有的键值对
常用方法总结:
import java.util.HashMap;
public class Fun {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("关羽","樊城");
map.put("张飞","新野");
map.put("赵子龙","徐州");
map.put("刘备","荆州");
System.out.println(map);
System.out.println(map.remove("关羽"));
System.out.println(map);
System.out.println(map.get("张飞"));
System.out.println(map.containsKey("刘备"));
System.out.println(map.containsValue("新野"));
System.out.println(map.isEmpty());
System.out.println(map.size());
map.clear();
System.out.println(map.isEmpty());
System.out.println(map);
}
}
public static void main(String[] args) {
HashMap map = new HashMap();
map.put(1,23);
map.put(2,3);
System.out.println(map);
map.replace(2,10);//修改
System.out.println(map);
}
**练习1:**根据学员英文名找到学员对象
两个学员应聘至外企工作,每个学员都会有一个英文名称,对应该学员对象。请实现通过英文名称,获得该学员对象的详细信息
学员属性包括姓名以及性别。
**练习2:**三国时期,刘备派关羽守樊城,张飞守新野,赵子龙守徐州,他自己坐镇荆州。
使用HashMap和City类实现,并打印输出,张飞守的是那座城?所有人员守城信息。
public class City {
private String city;
@Override
public String toString() {
return "City{" +
"city='" + city + '\'' +
'}';
}
public City() {
}
public City(String city) {
this.city = city;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TestCity {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("关羽",new City("樊城"));
map.put("张飞",new City("新野"));
map.put("赵子龙",new City("徐州"));
map.put("刘备",new City("荆州"));
System.out.println(map.get("张飞"));
System.out.println(map);
Set keys = map.keySet();
for (Object o:keys){
System.out.println(o+"-------"+map.get(o));
}
Collection values = map.values();
for (Object o:values){
System.out.println(o);
}
Set entrySet = map.entrySet();
for (Object o:entrySet){
Map.Entry entry = (Map.Entry) o;
System.out.println(entry.getKey()+"----"+entry.getValue());
}
}
}
作业
1.使用HashMap存储狗狗信息,
(1)“豆豆”,对应年龄2岁,白色哈士奇;
(2)“毛毛”,对应年龄3岁,黄色拉布拉多;
(3)“皮皮”,对应年龄1岁,棕色泰迪。
(4)分别输出所有狗狗的名称,颜色和种类。
public class Dog {
private String name;
private int age;
private String color;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
public Dog() {
}
public Dog(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
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 String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
import java.util.HashMap;
public class TestDog {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("狗狗1",new Dog("豆豆",2,"白色哈士奇"));
map.put("狗狗2",new Dog("毛毛",3,"黄色拉布拉多"));
map.put("狗狗3",new Dog("皮皮",1,"棕色泰迪"));
System.out.println(map);
}
}
2.金刚葫芦娃大战蛇妖:
蛇妖吞进了金刚葫芦1娃,3娃和6娃,这时4娃和5娃前来救出了1娃和3娃,结果自己却被蛇妖吞了进去。请问,7娃在不在蛇妖肚子里?
使用HashSet实现以上需求。创建葫芦类Gourd。
public class Gourd {
private String name;
@Override
public String toString() {
return name ;
}
public Gourd() {
}
public Gourd(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.HashSet;
public class TestGourd {
public static void main(String[] args) {
Gourd g1 = new Gourd("大娃");
Gourd g2 = new Gourd("二娃");
Gourd g3 = new Gourd("三娃");
Gourd g4 = new Gourd("四娃");
Gourd g5 = new Gourd("五娃");
Gourd g6 = new Gourd("六娃");
Gourd g7 = new Gourd("七娃");
HashSet h1 = new HashSet();
h1.add(g1);
h1.add(g3);
h1.add(g6);
System.out.println(h1);
h1.remove(g1);
h1.remove(g3);
h1.add(g4);
h1.add(g5);
System.out.println(h1);
}
}