Java中的集合
集合类概述
为什么出现集合类?
我们学习的是面向对象的编程语言,面向对象的编程语言对事物的描述都是通过对象体现的,
为了方便对多个对象进行操作,我们就必须把这多个对象进行存储,而要想存储多个对象,就不能
是基本的变量了,应该是一个容器类型的变量。
回顾我们学习过的知识,有哪些容器类型的呢?
数组,StringBuilder
首先说StringBuilder,它的结果是一个字符串,不一定满足我们的需求
数组的长度
而数组的长度固定,不能适应变化的需求,在这种情况下Java提供集合类给我们使用
由此可见集合类的长度是可变的。
集合类的特点:长度可变。
Collection:是单列集合的顶层接口。
Collection表示一组对象,这些对象也称为collection的元素。
一些colloection允许有重复的元素,而另一些则是无序的。
JDK 不提供此接口的任何实现:它提供更加具体的子接口(Set,List)
创建Collection集合的对,我们采用的是多态的方式,使用的是具体的ArrayList类
因为这个类是最常用的集合类
ArrayList()
Collection<E>:
<E>:是一种特殊的数据类型,泛型,这里我们会使用就可以了
如何使用?
在出现E的地方使用引用数据类型替换即可’、
举例Collection<String>/Collection<Student>
集合类体系结构
Collection与迭代器
Collection集合概述:
是单例集合的顶层接口,Collection表示一组对象,这些对象也称为collection的元素
JDK不提供此接口的任何直接实现:它提供更具体的子接口(Set和List)实现。
创建Collection集合对象
多态的方式
具体类的实现类ArrayList()
添加元素
add(E e)
public class CollectionDemo {
public static void main(String[] args) {
//创建Collection集合对象
//JDK7的新特性,看懂就可以
//Collection<String> c = new ArrayList<>();//多态的方式(不建议使用)
Collection<String> c = new ArrayList<String>();//多态的方式
//boolean add(E e) 添加元素
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c);
// 输出了集合中的元素按照指定格式拼接的内容,说明ArrayList重写了toString()方法
}
}
Collection集合的成员方法
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection<String> c = new ArrayList<String>();
1. boolean add(E e):添加元素
System.out.println("add:"+c.add("hello"));
//通过查看ArrayList源码,我们知道了add(E e)方法返回值永远是true
c.add("world");
c.add("java");
2. boolean remove(Object o):从集合中移除元素
//System.out.println("remove:"+c.remove("world"));
//System.out.println("remove:"+c.remove("111")); //失败返回false
3. void clear():清空集合中的元素
//c.clear();
4. boolean contains(Object o):判断集合中是否存在指定的元素
//System.out.println("contains:"+c.contains("world")); //true
//System.out.println("contains:"+c.contains("111")); //false
5. boolean isEmpty():判断集合是否为空
System.out.println("isEmpty:"+c.isEmpty()); //false
6. int size():集合的长度,也就是集合中元素的个数
System.out.println("siez:"+c.size());
//输出集合对象
System.out.println(c);
}
}
Collection集合的遍历
Collection集合的遍历
在Collection中的iterator()方法中,
Iterator<E> iterator():返回的是一个collection的元素上进行迭代的迭代器
通过集合对象调用iterator()方法得到迭代器对象,他是Iterator<E>的实现类对象
Iterator:
E next();:返回迭代的下一个元素
boolean hasNext():如果有元素则可以迭代,返回true
void remove():
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection<String> c = new ArrayList<String>();
//添加元素
c.add("hello");
c.add("world");
c.add("java");
//遍历集合
Iterator<String> it = c.iterator(); //Iterator是接口,所以返回的肯定是实现类的对象
//System.out.println(it.next());
//System.out.println(it.next());
//System.out.println(it.next());
//java.util.NoSuchElementException
//System.out.println(it.next());
//boolean hasNext()
while (it.hasNext()){
//System.out.println(it.next());
String s = it.next();
System.out.println(s);
}
}
}
Collection集合的练习
需求:Collection集合存储自定义对象并遍历
自定义学生类:给出成员变量name和age,遍历集合的时候,在控制台输出对象的成员变量值
package com.scy11;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionDemo {
public static void main(String[] args) {
//创建集合对象
Collection<Student> c = new ArrayList<Student>();
//创建元素对象
Student s1 = new Student("林青霞",20);
Student s2 = new Student("张曼玉",35);
Student s3 = new Student("王祖贤",50);
//把元素添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
//遍历集合
Iterator<Student> it = c.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
List
List:
有序的 collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确
地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
与 set 不同,列表通常允许重复的元素。
List集合特点:
a:有序(存储和取出的元素的顺序一致)
b:存储的元素可以重复
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
Iterator<String> it = list.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
List集合的遍历
List集合的遍历:
a:迭代器;
b:普通for循环遍历
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
for (int i=0;i<list.size();i++){
String s = list.get(i)
System.out.println(s);
}
List集合的练习
需求:List集合存储自定义对象并遍历
提示:自定义一个学生类,给出成员变量name和age,遍历集合的时候,在控制台输出学生对象的
成员变量值(用两种方式遍历)
public class ListDemo {
public static void main(String[] args) {
//创建List集合对象
List<Student> list = new ArrayList<Student>();
//创建元素对象
Student s1 = new Student("林青霞",20);
Student s2 = new Student("张曼玉",10);
Student s3 = new Student("王祖贤",25);
list.add(s1);
list.add(s2);
list.add(s3);
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
System.out.println("-------------------");
for (int i=0;i<list.size();i++){
Student s = list.get(i);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
List迭代器的特有功能
ListIterator:
ListIterator<E> listIterator():返回次列表元素的列表迭代器
public interface ListIterator<E> extends Iterator<E>
特有功能:
E previous(): 返回列表中的前一个元素
boolean hasPrevious():如果以逆向遍历列表,列表迭代器有多个元素,则返回true
注意:ListIterator可以实现逆向遍历,但是要求先正向遍历,才能逆向遍历
由于这个特点所以我们一般不使用列表迭代器ListIterator
*但是ListIterator接口对象有特有的add(E e)方法,可以在迭代器循环集合时,
对集合进行修改
并发修改异常
并发修改异常产生的原因及解决方案
我有一个集合:List<String> list= new ArrayList<String>();
里面有三个list.add("hello");list.add("world");list.add("java");
我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请用代码实现
ConcurrentModificationException:
当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
产生的原因:迭代器依赖于集合存在,在判断成功后,集合中添加了新的元素而迭代器不知道。
其实这个问题说的是:迭代器遍历集合中元素时,不要使用集合对象去修改集合中的元素。
如何解决它?
a:迭代器遍历的时候,我们可以通过迭代器修改集合中的元素
元素添加在,我们刚才迭代的元素后面
b:集合遍历的时候,我可以通过集合对象修改集合中的元素
元素是在最后添加的
public class ListIteratorDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
//Iterator<String> it = list.iterator();
//while (it.hasNext()){
// String s = it.next();
// if (s.equals("world")){
// it.add("javaee"); ConcurrentModificationException
//}
//}
//迭代器遍历的时候,我们可以通过迭代器修改集合中的元素
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()){
String s = lit.next();
if (s.equals("world")){
lit.add("javaee");
}
}
System.out.println(list);
//集合遍历的时候,我可以通过集合对象修改集合中的元素
for (int i=0;i<list.size();i++){
if (list.get(i)=="world"){
list.add("javaee");
}
}
System.out.println(list);
}
}
增强for
增强for:是for循环的一种
格式:
for(元素的数据类型 变量名:数组名或者Collection集合对象名) {
使用变量名即可,这个变量名代表的其实就是数组或者Collection集合的元素
}
好处:简化了数组和Collection集合的遍历
弊端:目标不能为null,需要在遍历前判断list不能与null
package com.scy11;
import java.util.ArrayList;
import java.util.List;
public class ForDemo {
public static void main(String[] args) {
//定义一个int类型的数组
int[] arr = {1,2,3,4,5};
//普通for循环
for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
System.out.println("------------");
//增强for循环
for(int i:arr){
System.out.println(i);
}
System.out.println("------------");
//定义一个Stringx类型的数组
String[] strArray = {"hello","world","java"};
for (String s:strArray){
System.out.println(s);
}
System.out.println("------------");
//创建集合对象
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
//list = null; --->NullPointerException
// if(list !=null);
// for (String s:list){
// System.out.println(s);
// }
//增强for其实就是用来替代迭代器的
for (String s:list){
if (s.equals("world")){
list.add("javaee");
}
}
}
}
增强for练习
List集合存储自定义对象并遍历
提示:自定义一个学生类,给出成员变量name和age。遍历集合时,在控制台输出学生对象的成员
变量的值。
遍历方式:
增强for
package com.scy11;
import java.util.ArrayList;
import java.util.List;
public class ForTest {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",36);
Student s3 = new Student("王祖贤",32);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
for (Student s:studentList){
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
数据结构
数据结构:数据的组织方式,如何存储数据。
栈:先进后出
队列:先进先出
数组:存储同一种数据类型的多个元素的容器。有索引,访问我们获取元素
特点:查询快,增删慢
链表:由一个链子把多个节点连接起来的数据
节点:由数据和地址组成
特点:查询慢,增删快
List集合子类特点及ArrayList集合存储字符串并遍历
List:
ArrayList:底层数据结构时数组,查询快,增删慢
LinkedList:底层数据结构是链表,查询慢,增删快
ArrayList存储字符串并遍历
package com.scy11;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] args) {
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//添加元素
array.add("hello");
array.add("world");
array.add("java");
//迭代器遍历
Iterator<String> it = array.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
System.out.println("------------------");
//普通for
for (int i=0;i<array.size();i++){
String s = array.get(i);
System.out.println(s);
}
System.out.println("------------------");
//增强for
for (String s:array){
System.out.println(s);
}
}
}
ArrayList练习
需求:ArrayList集合的练习存储自定义对象并遍历
提示:自定义学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员
变量的三种方式遍历
package com.scy11;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] args) {
//创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
//添加元素
Student s1 = new Student("林青霞",20);
Student s2 = new Student("张曼玉",30);
Student s3 = new Student("王祖贤",40);
array.add(s1);
array.add(s2);
array.add(s3);
//迭代器遍历
Iterator<Student> it = array.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+s.getAge());
}
System.out.println("------------------");
//普通for
for (int i=0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getName()+s.getAge());
}
System.out.println("------------------");
//增强for
for (Student s:array){
System.out.println(s.getName()+s.getAge());
}
}
}
Set
Set:
一个不包含重复元素的collection
HashSet
它不保证set的迭代顺序;特别是它不保证该顺序是恒久不变的
public class SetDemo {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
//添加元素
set.add("hello");
set.add("world");
set.add("java");
//唯一性
set.add("world");
for (String s:set){
System.out.println(s);
}
}
}
HashSet保证元素唯一性的原理
通过查看add方法的源码,我们添加元素时,进行了判断
流程:首先比较了对象的哈希值是否相同,这个哈希值是根据hashCode()计算出来的
如果哈希值不同,就直接添加到集合中
如果哈希值相同,继续执行equals方法进行比较
返回的是true,说明元素重复,不添加
反之就添加
如果我们使用HashSet集合存储对象,要想保证元素的唯一性,就必须重写hashCode()和
equals()方法。因为String类型重写了这两个所以可以保证元素的唯一性
HashSet练习1
HashSet集合存储自定义对象并遍历
提示:自定义一个学生类,给出成员变量name和age。遍历集合时,在控制台输出学生对象的
成员变量的值。
两种方式遍历
迭代器
增强for
package com.scy12;
public class Student {
private String name;
private int age;
public Student() {
}
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;
}
}
package com.scy12;
import java.util.HashSet;
import java.util.Iterator;
public class HashSetDemo {
public static void main(String[] args) {
//创建集合对象
HashSet<Student> hs = new HashSet<Student>();
//创建元素对象
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",32);
Student s3 = new Student("王祖贤",33);
hs.add(s1);
hs.add(s2);
hs.add(s3);
Iterator<Student> it = hs.iterator();
//迭代器循环
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+s.getAge());
}
//增强for循环
for (Student s :hs){
System.out.println(s.getName()+s.getAge());
}
}
}
HashSet练习2
需求:HashSet集合存储自定义对象并遍历
要求:如果成员变量值相同,就认为是同一个元素。
提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象
成员变量
两种方式遍历
迭代器
增强for
因为我们存储的元素所属的Student类,没有重写hashCode()和equals()方法
package com.scy12;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
package com.scy12;
import java.util.HashSet;
import java.util.Iterator;
public class HashSetDemo {
public static void main(String[] args) {
//创建集合对象
HashSet<Student> hs = new HashSet<Student>();
//创建元素对象
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",32);
Student s3 = new Student("王祖贤",33);
Student s4 = new Student("王祖贤",33);
Student s5 = new Student("张曼玉",32);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
hs.add(s5);
Iterator<Student> it = hs.iterator();
//增强for循环
for (Student s :hs){
System.out.println(s.getName()+s.getAge());
}
}
}
Map
public interface Map<K,V>将键映射到值的对象。一个映射不能包含重复的键;每个键最多
只能映射到一个值。
举例:学生的学号和姓名
001 林青霞
002 张曼玉
003 王祖贤
实现类为HashMap
Map集合的成员方法:
V put(K key, V value) 添加元素
V remove(Object key) 根据键删除键值对元素
void clear() 移除所有键值对元素
boolean containsKey(Object key) 判断集合是否包含指定的键
boolean containsValue(Object value) 判断集合是否包含指定的值
boolean isEmpty() 判断集合是否为空
int size() 返回集合中键值对的对数
Map集合中的实现类的数据结构只针对键有效;
package com.scy12;
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("001","林青霞");
map.put("002","张曼玉");
map.put("003","王祖贤");
System.out.println(map);
}
}
package com.scy12;
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
//1.V put(K key,V value):添加元素
//如果键是第一次存储,就直接存储返回null
System.out.println("put:"+map.put("张无忌","周芷若"));
//如果键不是第一次存储,就用值把以前的值替换,返回以前的值;
System.out.println("put:"+map.put("张无忌","赵敏"));
System.out.println(map);
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
System.out.println(map);
//2. V remove(Object key):根据键删除键值对元素
System.out.println(map.remove("杨过")); //返回小龙女即该键对应的值
System.out.println(map.remove("郭襄")); //没有改键则返回null
//3. void clear():移除所有的键值对元素
//map.clear(); //不建议使用
//4. boolean containsKey(Object key):判断集合是否包含指定的键
System.out.println("containsKey:"+map.containsKey("张无忌"));
//5. boolean containsValue(Object value)
//6. boolean isEmpty():判断集合是否为空
System.out.println("isEmpty:"+map.isEmpty());
//7. int size():返回集合中的键值对对数
System.out.println("size:"+map.size());
}
}
Map集合的获取方法
V get(Object key):根据键获取值
Set<k> keySet():获取所有键的集合
Collenction<V> values():获取所有值的集合
Set<Map.Entry<K,v>> set = map.entrySet(); 返回键值对对象
package com.scy12;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String, String>();
//添加元素
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
map.put("张无忌","赵敏");
// V get(Object key): 我知道键,我想知道键对应的值
System.out.println("get:"+map.get("张无忌"));
// Set<k> keySet(): 我不知道集合里有哪些键
System.out.println("keySet():"+map.keySet());
//Collection<V> values(): 我们不知道集合里有哪些值
Collection<String> values = map.values();
for (String value: values){
System.out.println(value);
}
}
}
Map集合的遍历
思路:
Map看成是一对夫妻对的集合
a:把所有的丈夫给集中起来
b:遍历丈夫的集合,获取到每一个丈夫
c:根据丈夫去找对应的妻子
方式1:根据键找值
-获取所有键的集合
-遍历键的集合,获取到每一个键
-根据键找值
public class MapDemo {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String, String>();
//添加元素
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
map.put("张无忌","赵敏");
//获取所有键的集合
Set<String> set = map.keySet();
for (String key:set){
String value = map.get(key);
System.out.println(key+"---"+value);
}
}
}
方式2:根据键值对,对象找键和值
-获取所有键值对,对象的集合
-遍历键值对,对象的集合,获取到每一个键值对对象
-根据键值对,对象找键和值
package com.scy12;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String, String>();
//添加元素
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
map.put("张无忌","赵敏");
//获取所有键值对,对象的集合
//Set<Map.Entry<K,v>> set = map.entrySet();
Set<Map.Entry<String,String>> set = map.entrySet();
//遍历键值对对象,得到每一个键值对对象
for (Map.Entry<String,String> me:set){
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"---"+value);
}
}
}
HashSet集合练习1
练习1:两种迭代方式遍历
HashMap<String,Student>
键:String 学员
值:Student 学生对象
package com.scy12;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
//创建集合对象
HashMap<String,Student> hm = new HashMap<String, Student>();
// 创建元素对象
Student s1 = new Student("林青霞",20);
Student s2 = new Student("张曼玉",30);
Student s3 = new Student("王祖贤",35);
//添加元素到集合中
hm.put("it001",s1);
hm.put("it002",s2);
hm.put("it003",s3);
//遍历HashSet集合方式1
Set<String> keys = hm.keySet();
for (String key:keys){
Student s = hm.get(key);
System.out.println(key+s.getName()+s.getAge());
}
//遍历HashSet集合方式2
Set<Map.Entry<String,Student>> set = hm.entrySet();
for (Map.Entry<String,Student> me:set){
String s = me.getKey();
Student std = me.getValue();
System.out.println(s+std.getName()+std.getAge());
}
}
}
HashSet集合练习2
练习2:任选一种方式遍历
HashMap<Student,String>
键:Student 学生对象
值:String 学生住址
要求:如果学生对象的成员变量相同,就说明是同一个键
package com.scy12;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
package com.scy12;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
//创建集合对象
HashMap<Student,String> hm = new HashMap<Student, String>();
// 创建元素对象
Student s1 = new Student("林青霞",20);
Student s2 = new Student("张曼玉",30);
Student s3 = new Student("王祖贤",35);
Student s4 = new Student("王祖贤",35);
//添加元素到集合中
hm.put(s1,"南京");
hm.put(s2,"北京");
hm.put(s3,"上海");
hm.put(s4,"香港");
//遍历HashSet集合方式1
Set<Student> keys = hm.keySet();
for (Student key:keys){
String s = hm.get(key);
System.out.println(key.getName()+ key.getAge()+s);
}
}
}
集合的嵌套练习1
需求:ArrayList集合嵌套HashMap集合并遍历。
* 定义一个ArrayList集合,它包含三个元素,每一个元素都是HashMap类型的。
* 每一个HashMap集合的键和值都是String类型的,
* 键:String 丈夫的姓名
* 值:String 妻子的姓名
* 给出如下的字符串数据,请用代码实现需求。
* 第一个HashMap集合的元素:
* 孙策 大乔
* 周瑜 小乔
* 第二个HashMap集合的元素:
* 郭靖 黄蓉
* 杨过 小龙女
* 第三个HashMap集合的元素:
* 令狐冲 任盈盈
* 林平之 岳灵珊
package com.scy12;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class ArrayListIncludeHashMapTest {
public static void main(String[] args) {
ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>();
//创建元素
HashMap<String,String> hm1 = new HashMap<String, String>();
hm1.put("孙策","大乔");
hm1.put("周瑜","小乔");
HashMap<String,String> hm2 = new HashMap<String, String>();
hm2.put("郭靖","黄蓉");
hm2.put("杨过","小龙女");
HashMap<String,String> hm3 = new HashMap<String, String>();
hm3.put("令狐冲","任盈盈");
hm3.put("林平之","岳灵珊");
//添加到集合
array.add(hm1);
array.add(hm2);
array.add(hm3);
//遍历ArrayList集合
for(HashMap<String,String> hm:array){
Set<String> set = hm.keySet();
for (String key:set){
String value = hm.get(key);
System.out.println(key+"---"+value);
}
System.out.println("------------");
}
}
}
集合的嵌套练习2
需求:HashMap集合嵌套ArrayList集合并遍历。
定义一个HashMap集合,它包含三个元素,每一个元素的键是String类型,值是ArrayList类型。
键:String 人物来自哪部电视剧
值:ArrayList 人物的名称
每一个ArrayList集合的数据是String类型的。
给出如下的字符串数据,请用代码实现需求。
第一个ArrayList集合的元素:(三国演义)
诸葛亮
赵云
第二个ArrayList集合的元素:(西游记)
唐僧
孙悟空
第三个ArrayList集合的元素:(水浒传)
武松
鲁智深
package com.scy12;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class HashMapIncludeArrayListTest {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
ArrayList<String> sgyy = new ArrayList<String>();
sgyy.add("诸葛亮");
sgyy.add("赵云");
hm.put("三国演义",sgyy);
ArrayList<String> xyj = new ArrayList<String>();
xyj.add("唐僧");
xyj.add("孙悟空");
hm.put("西游记",xyj);
ArrayList<String> shz = new ArrayList<String>();
shz.add("武松");
shz.add("鲁智深");
hm.put("水浒传",shz);
//遍历HashMap
Set<String> set = hm.keySet();
for (String key:set){
ArrayList<String> array = hm.get(key);
for (String s:array){
System.out.println(s);
}
System.out.println("-----------------------");
}
}
}
拜师教育学员文章:作者:976-沈同学,
转载或复制请以 超链接形式 并注明出处 拜师资源博客。
原文地址:《7.Java中的集合》 发布于2020-03-14
评论 抢沙发