文章目录
集合概述
ArrayList构造方法和添加方法
- 构造集合/追加集合末尾
package heima;
import java.util.ArrayList;
public class P135 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
//输出集合
System.out.println("array:"+array);
}
}
自动在两个元素之间加上逗号空格
- 在指定位置插入元素
//在集合中指定位置插入指定元素
//插入成功
array.add(1,"javase");
//插入失败 IndexOutOfBoundsException
array.add(4,"javase");
插入成功:
插入失败:
ArrayList集合常用方法
- remove 删除指定元素(boolean)
package heima;
import java.util.ArrayList;
public class P136 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
//删除指定元素,类型boolean
System.out.println(array.remove("world"));
//输出
System.out.println("array:"+array);
}
}
- remove删除指定索引处元素(E)
package heima;
import java.util.ArrayList;
public class P136 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
//删除指定索引处元素
System.out.println(array.remove(0));
//输出
System.out.println("array:"+array);
}
}
注意:也存在IndexOutOfBoundsException情况
- get 返回指定索引处的元素(E)
package heima;
import java.util.ArrayList;
public class P136 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.get(0));
//输出
System.out.println("array:"+array);
}
}
- size返回集合中的元素的个数(int)
package heima;
import java.util.ArrayList;
public class P136 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println(array.size());
//输出
System.out.println("array:"+array);
}
}
ArrayList存储字符串并遍历
package heima;
import java.util.ArrayList;
public class P136 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
for(int i = 0;i<array.size();i++){
String s = array.get(i);
System.out.println(s);
}
}
}
- 案例–存储学生对象并遍历
package heima.P137;
import heima.P137.Student;
import java.util.ArrayList;
public class P137 {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
Student s1 = new Student("小林",30);
Student s2 = new Student("小红",33);
Student s3 = new Student("小张",18);
array.add(s1);
array.add(s2);
array.add(s3);
for(int i = 0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
}
集合类体系结构
Collection集合概述和使用
package heima;
import java.util.ArrayList;
import java.util.Collection;
public class P221 {
public static void main(String[] args) {
//创建Collection集合的对象
Collection<String> c = new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
System.out.println(c);
}
}
Collection集合常用方法
新函数功能:clear、contains、isEmpty
- clear
- contains
- isEmpty
Collection集合
- next
package heima;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class P223 {
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();
//返回迭代器中的下一个元素
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
}
}
NoSuchElementException-溢出
- next改进
//用while循环改进判断
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
- 集合的使用步骤
package heima;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class P223 {
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();
//用while循环改进判断
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
集合中迭代器类似于指针。
Collection集合存储学生对象并遍历
package heima.P225;
import heima.P225.Student;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class P225 {
public static void main(String[] args) {
Collection<Student> c = new ArrayList<Student>();
Student s1 = new Student("小林",30);
Student s2 = new Student("小红",37);
Student s3 = new Student("小明",19);
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集合特点
package heima;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class P226 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
System.out.println(list);
Iterator<String> it = list.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
List集合特有方法
List存储学生并遍历
package heima.P228;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class P228 {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student s1 = new Student("小l",30);
Student s2 = new Student("小z",35);
Student s3 = new Student("小w",33);
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循环方式
for(int i = 0;i<list.size();i++){
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
}
Student.java
package heima.P228;
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 heima;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class P229 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
//使用迭代器遍历,查找"world",有则添加"javase"
Iterator<String> it = list.iterator();
while(it.hasNext()){
String s = it.next();
if(s.equals("world")){
list.add("javase");
}
}
// for(int i = 0;i<list.size();i++){
// String s = list.get(i);
// if(s.equals("world")){
// list.add("javase");
// }
// }
System.out.println(list);
}
}
按照正常逻辑写出的代码,却报错
去源码寻找原因:
发现是next方法中的checkFormodification函数不对劲
按照里面的modCount和expectedModCoutntq去寻找错误
发现add函数中导致modCount加一,导致两者不相等
Listlterator
与Iterator迭代器不同之处
package heima;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class P230 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("java");
//获取列表迭代器
ListIterator<String> lit = list.listIterator();
while(lit.hasNext()){
String s = lit.next();
if(s.equals("world")){
lit.add("javase");
}
}
System.out.println(list);
}
}
运行成功:
观察源码:
ListIterator的add方法有不同之处:
使expectedModCount和modCount两者赋值相等
增强for循环
- 演示增强for循环功能
- 证明增强for内部其实是一个iterator迭代器
package heima;
import java.util.ArrayList;
import java.util.List;
public class P231 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
for(int i : arr){
System.out.println(i);
}
System.out.println("-----------------------");
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");
for(String s : list){
System.out.println(s);
}
System.out.println("--------------------------");
//内部原理是一个Iterator迭代器
for (String s:list){
if (s.equals("world")){
list.add("javase");
}
}
}
}
区分三种遍历方式
package heima.common;
import heima.common.Student;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class P {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student s1 = new Student("小l",30);
Student s2 = new Student("小z",35);
Student s3 = new Student("小w",33);
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
for (int i = 0;i< list.size();i++){
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("------------");
//增强for
for (Student s:list){
System.out.println(s.getName()+","+s.getAge());
}
}
}
数据结构与集合类
LinkedList
与ArrayList区别
存储学生对象的三种方式
package heima;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
public class P235 {
public static void main(String[] args) {
//#集合对象
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
System.out.println("增强for: ");
//1.增强for
for(String s:array){
System.out.println(s);
}
System.out.println("----------------------");
//2.普通for
for(int i = 0;i< array.size();i++){
String s = array.get(i);
System.out.println(s);
}
System.out.println("----------------------");
//3.迭代器iterator
Iterator<String> it = array.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
System.out.println("链表对象: ");
//#链表对象
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("hello");
linkedList.add("world");
linkedList.add("java");
//1.增强for
for(String s : linkedList){
System.out.println(s);
}
System.out.println("----------------------");
//2.普通for
for(int i = 0;i< linkedList.size();i++){
String s = linkedList.get(i);
System.out.println(s);
}
System.out.println("----------------------");
//3.迭代器iterator
Iterator<String> it1 = linkedList.iterator();
while (it1.hasNext()){
String s = it1.next();
System.out.println(s);
}
}
}
LinkedList集合特有功能
package heima;
import java.util.HashSet;
import java.util.Set;
public class P238 {
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);
}
}
}
哈希值
hashCode
package heima;
import heima.common.Student;
public class P239 {
public static void main(String[] args) {
Student s1 =new Student("小林",30);
//同一个对象多次调用hashCode方法返回的哈希值是相同的
System.out.println(s1.hashCode());//356573597
System.out.println(s1.hashCode());//356573597
System.out.println("------------------");
//默认情况下,不同对象的哈希值是不同的
Student s2 = new Student("小林",30);
System.out.println(s2.hashCode());//1735600054
System.out.println("------------------");
//通过方法重写,可以实现不同对象的哈希值是相同的
System.out.println("hello".hashCode());//99162322
System.out.println("world".hashCode());//113318802
System.out.println("java".hashCode());//3254818
System.out.println("world".hashCode());//113318802
System.out.println("------------------");
System.out.println("重地".hashCode());//1179395
System.out.println("通话".hashCode());//1179395
}
}
String哈希值相同的原因:内部重写了hashcode方法:
HashSet
package heima;
import java.util.HashSet;
public class P240 {
public static void main(String[] args) {
HashSet<String> hs = new HashSet<String>();
hs.add("hello");
hs.add("world");
hs.add("java");
hs.add("world");
for(String s : hs){
System.out.println(s);
}
}
}
常见数据结构之哈希表
哈希表机构:数组+链表
- HashSet集合保证元素唯一性源码分析
还是不懂去看B课P242
- 存储流程
HashSet集合存储学生对象
package heima;
import heima.common.Student;
import java.util.HashSet;
public class P243 {
public static void main(String[] args) {
HashSet<Student> hs = new HashSet<Student>();
Student s1 = new Student("小a",30);
Student s2 = new Student("小h",3);
Student s3 = new Student("小c",10);
Student s4 = new Student("小h",3);
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
for(Student s:hs){
System.out.println(s.getName()+","+s.getAge());
}
}
}
发现一样输出:
对于s2和s4属于不同对象
- 解决–Student类中重写equals和hasCode方法
别忘了快捷键alt + insert
LinkedHashSet集合
package heima;
import java.util.LinkedHashSet;
public class P244 {
public static void main(String[] args) {
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
linkedHashSet.add("hello");
linkedHashSet.add("world");
linkedHashSet.add("java");
linkedHashSet.add("hello");
for(String s:linkedHashSet){
System.out.println(s);
}
}
}
TreeSet集合概述和特点
排序方式取决于构造方法
package heima;
import java.util.TreeSet;
public class P245 {
public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(10);
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(20);
ts.add(30);
for(Integer i:ts){
System.out.println(i);
}
}
}
Comparable自然排序
- TreeSet自然排序类对象,报错:
package heima;
import heima.common.Student;
import java.util.TreeSet;
public class P246 {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
Student s1 = new Student("a",29);
Student s2 = new Student("c",28);
Student s3 = new Student("e",17);
Student s4 = new Student("b",19);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
for (Student s:ts){
System.out.println(s.getName()+","+s.getAge());
}
}
}
- 解决–需要重写Comparable方法
1.发现输出只有一个对象
原因:return 0
2.改为return 1–按add升序排序
3.return -1–按add降序排序
4.按照年龄从小到大排序
在重写方法内:
//按照年龄从小到大排序
int num = this.age - s.age;
return num;
bug:出现年龄相同的不同对象时,不输出
输出:
5.增加判断
年龄相同时,按姓名字母顺序排序
//按照年龄从小到大排序
int num = this.age - s.age;
//年龄相同时,按姓名字母顺序排序
int num2 = num==0?this.name.compareTo(s.name):num;
return num2;
Comparator比较器排序
只是换了一种形式
package heima;
import heima.common.Student;
import java.util.Comparator;
import java.util.TreeSet;
public class P247 {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
Student s1 = new Student("a",29);
Student s2 = new Student("c",28);
Student s3 = new Student("e",17);
Student s4 = new Student("b",19);
Student s5 = new Student("f",17);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
for (Student s:ts){
System.out.println(s.getName()+","+s.getAge());
}
}
}
案例:成绩排序
package heima.P248;
import java.util.Comparator;
import java.util.TreeSet;
public class P248 {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
int num2 = num == 0?s1.getChinese() - s2.getChinese():num;
int num3 = num2 == 0?s1.getName().compareTo(s2.getName()):num;
return num3;
}
});
Student s1 = new Student("a",129,100);
Student s2 = new Student("c",128,90);
Student s3 = new Student("e",117,120);
Student s4 = new Student("b",119,88);
Student s5 = new Student("f",127,120);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
for(Student s:ts){
System.out.println(s.getName()+","+s.getChinese()+","+s.getMath());
}
}
}
案例–不重复的随机数
package heima;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class P249 {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
//创建随机数对象
Random r = new Random();
//判断集合的长度是不是小于10
while (set.size()<10){
//产生一个随机数,添加到集合
int number = r.nextInt(20) + 1;
set.add(number);
}
for(Integer i:set){
System.out.println(i);
}
}
}
泛型
泛型概述
- 直接创建集合对象
中途加入c.add(100)直到最后才报错
package heima;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class P250 {
public static void main(String[] args) {
Collection c = new ArrayList<>();
c.add("hello");
c.add("world");
c.add("java");
c.add(100);
Iterator it = c.iterator();
while(it.hasNext()){
String s = (String) it.next(); //ClassCastException
System.out.println(s);
}
}
}
- 指定为泛型
会提前在编译时期报错
package heima.P250;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class P250_2 {
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
// c.add(100);
Iterator<String> it = c.iterator();
while(it.hasNext()){
// String s = (String) it.next(); //ClassCastException
String s = it.next();
System.out.println(s);
}
}
}
泛型类
- 列:一个方法只能传入一种类型
- 正常情况:两个对象才能实现两个类型
输出:
- 使用泛型
一个对象可以接受多种类型
common/Generic.java
package heima.common;
public class Generic<T> {
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
P251.java
package heima;
import heima.common.Generic;
import heima.common.Student;
import heima.common.Teacher;
public class P251 {
public static void main(String[] args) {
Student s = new Student();
s.setName("小玲");
System.out.println(s.getName());
Teacher t = new Teacher();
t.setAge(30);
// t.setAge("30");
System.out.println(t.getAge());
System.out.println("-----------");
Generic<String> g1 = new Generic<String>();
g1.setT("小林");
System.out.println(g1.getT());
Generic<Integer> g2 = new Generic<Integer>();
g2.setT(30);
System.out.println(g2.getT());
}
}
泛型方法
- 泛型方法1
Generic.java
package heima.common;
import com.sun.org.apache.xpath.internal.operations.Bool;
public class Generic<T> {
public void show(String s){
System.out.println(s);
}
public void show(Integer i){
System.out.println(i);
}
public void show(Boolean b){
System.out.println(b);
}
}
P252.java
package heima;
import heima.common.Generic;
public class P252 {
public static void main(String[] args) {
Generic g = new Generic();
g.show("小林");
g.show(30);
g.show(true);
}
}
- 方法2
Generic.java
public void show(T t){
System.out.println(t);
}
P252.java
Generic<String> g1 = new Generic<String>();
g1.show("小林");
Generic<Integer> g2 = new Generic<Integer>();
g2.show(30);
Generic<Boolean> g3 = new Generic<Boolean>();
g3.show(true);
- 方法3–泛型方法
Generic.java
public <T> void show(T t){
System.out.println(t);
}
P252.java
Generic g = new Generic();
g.show("小林");
g.show(30);
g.show(true);
g.show(12.34);
泛型接口
- 基本实现
P253.java
package heima.P253;
public class P253
{
public static void main(String[] args) {
Generic<String> g1 = new Genericlmpl<String>();
g1.show("小米");
Generic<Integer> g2 = new Genericlmpl<Integer>();
g2.show(30);
}
}
Generic.java
package heima.P253;
public interface Generic<T> {
void show(T t);
}
Genericlmpl.java
package heima.P253;
public class Genericlmpl<T> implements Generic<T> {
@Override
public void show(T t) {
System.out.println(t);
}
}
类型通配符
- 越过上界
- 越过下界
可变参数
- 过去:传递同一方法不同参数时,很繁琐
package heima;
public class P255 {
public static void main(String[] args) {
System.out.println(sum(10,20));
System.out.println(sum(10,20,30));
System.out.println(sum(10,20,30,40));
}
public static int sum(int a,int b){
return a +b;
}
public static int sum(int a,int b,int c){
return a+b+c;
}
public static int sum(int a,int b,int c,int d){
return a+b+c+d;
}
}
- 现在:使用可变参数
package heima;
public class P255 {
public static void main(String[] args) {
System.out.println(sum(10,20));
System.out.println(sum(10,20,30));
System.out.println(sum(10,20,30,40));
}
public static int sum(int... a){
return 0;
}
}
- 1.可变参数实质是一个数组
应用此方法,实现原先相加逻辑:
public static int sum(int... a){
// System.out.println(a);
// return 0;
int sum = 0;
for(int i:a){
sum+=i;
}
return sum;
}
- 2.可变参数放在最前面时,报错
可变参数的使用
需要jdk9以上
Map集合
Map集合创建
package heima;
import java.util.HashMap;
import java.util.Map;
public class P257 {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//public V put(K key,V value)将指定的值与此映射中的指定键相关联。
map.put("item01","小林");
map.put("item02","小王");
map.put("item03","小张");
map.put("item03","小明"); //new
//输出
System.out.println(map); //{item03=小张, item02=小王, item01=小林}
//{item03=小明, item02=小王, item01=小林}
//使用Hash保证数据唯一性
}
}
Map集合基本方法
package heima;
import java.util.HashMap;
import java.util.Map;
public class P258 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
//public V put(K key,V value)将指定的值与此映射中的指定键相关联。
//V remove(object key);根据键删除对象元素
// System.out.println(map.remove("item01")); //小林
// System.out.println(map.remove("item04")); //null
//public void clear() 从这张地图中删除所有的映射。
// map.clear(); //{}
//public boolean containsKey(Object key)如果此映射包含指定键的映射
System.out.println(map.containsKey("item01")); //true
//其他自学
//输出
System.out.println(map);
}
}
Map集合的获取方法
package heima;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class P259 {
public static void main(String[] args) {
Map<String,String>map = new HashMap<String,String>();
map.put("item01","小林");
map.put("item02","小王");
map.put("item03","小张");
// //public V get(Object key)返回到指定键所映射的值
// System.out.println(map.get("item03")); //小张
// System.out.println(map.get("item07")); //null
//public Set<K> keySet()返回此地图中包含的 键 的Set视图
Set<String> keySet = map.keySet();
for(String key:keySet){
System.out.println(key);
}
//item03
//item02
//item01
//public Collection<V> values()返回此地图中包含的 值 的Collection视图
Collection<String> values = map.values();
for(String value:values){
System.out.println(value);
}
//小张
//小王
//小林
}
}
Map集合遍历方法
- way1
package heima;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class P260 {
public static void main(String[] args) {
//# Map集合遍历方法
//way1
Map<String,String> map = new HashMap<String,String>();
map.put("item01","小林");
map.put("item02","小王");
map.put("item03","小张");
Set<String> keySet = map.keySet();
for(String key:keySet){
String value = map.get(key);
System.out.println(key+","+value);
}
//item03,小张
//item02,小王
//item01,小林
}
}
- way2
package heima;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class P261 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("item01","小林");
map.put("item02","小王");
map.put("item03","小张");
//获取所有键值对象的集合
Set<Map.Entry<String,String>> entrySet = map.entrySet();
for (Map.Entry<String,String>me:entrySet){
String key = me.getKey();
String value = me.getValue();
System.out.println(key+","+value);
}
}
}
案例–HashMap集合存储学生对象并遍历
- 当学生对象作值时:
package heima;
import heima.common.Student;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class P262 {
public static void main(String[] args) {
HashMap<String, Student> hm = new HashMap<String,Student>();
Student s1 = new Student("小林",30);
Student s2 = new Student("小王",37);
Student s3 = new Student("小李",38);
hm.put("item01",s1);
hm.put("item02",s2);
hm.put("item03",s3);
Set<String> keySet = hm.keySet();
for (String key:keySet){
Student value = hm.get(key);
System.out.println(key+","+value.getName()+","+value.getAge());
}
System.out.println("-------------------");
Set<Map.Entry<String,Student>> entrySet = hm.entrySet();
for (Map.Entry<String,Student>me:entrySet){
String key = me.getKey();
Student value = me.getValue();
System.out.println(key+","+value.getName()+","+value.getAge());
}
}
}
- 当学生对象作键
需要重写
package heima;
import heima.common.Student;
import java.util.HashMap;
import java.util.Set;
public class P263 {
public static void main(String[] args) {
HashMap<Student,String> hm = new HashMap<Student,String>();
Student s1 = new Student("小宁",18);
Student s2 = new Student("小厂",45);
Student s3 = new Student("小林",3);
Student s4 = new Student("小林",3);
hm.put(s1,"西安");
hm.put(s2,"北京");
hm.put(s3,"广州");
hm.put(s4,"重庆");
Set<Student> keySet = hm.keySet();
for (Student key:keySet){
String value = hm.get(key);
System.out.println(key.getName()+","+key.getAge()+","+value);
}
}
}
在Student.java中重写equals和hashCode方法
案例:ArrayList集合存储HashMap元素并遍历
没什么难的,都是有规律的
package heima;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class P264 {
public static void main(String[] args) {
//创建ArrayList集合
ArrayList<HashMap<String,String>> array = new ArrayList<HashMap<String,String>>();
//创建HashMap集合,并对元素添加键值
HashMap<String,String> hm1 = new HashMap<String,String>();
hm1.put("孙策","大乔");
hm1.put("周瑜","小乔");
array.add(hm1);
HashMap<String,String> hm2 = new HashMap<String,String>();
hm2.put("小1","小2");
hm2.put("小3","小4");
array.add(hm2);
HashMap<String,String>hm3 = new HashMap<String,String>();
hm3.put("小5","小6");
hm3.put("小7","小8");
array.add(hm3);
//遍历
for (HashMap<String,String> hm :array){
Set<String> keySet = hm.keySet();
for (String key:keySet){
String value = hm.get(key);
System.out.println(key+","+value);
}
}
}
}
案例:HashMap集合存储ArrayList元素并遍历
package heima;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class P265 {
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);
//遍历
Set<String> keySet = hm.keySet();
for (String key:keySet){
System.out.println(key);
ArrayList<String> value = hm.get(key);
for (String s : value){
System.out.println("\t"+s);
}
}
}
}
案例:统计字符串中每个字符出现的次数
package heima;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class P266 {
public static void main(String[] args) {
//录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
//创建
HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
// TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();
//遍历
for (int i = 0;i<line.length();i++){
//得到每一个字符
char key = line.charAt(i);
//拿到每一个字符作为键盘到HashMap集合中去找对应值,看其返回值
Integer value = hm.get(key);
if (value == null){
//说明字符在HashMap集合中不存在,先赋值值为1
hm.put(key,1);
}else{
//说明字符HashMap集合中存在,则继续存储该字符的对应的值
value++;
hm.put(key,value);
}
}
//遍历HahMap集合,得到键和值,按照要求进行拼接
StringBuilder sb = new StringBuilder();
Set<Character> keySet = hm.keySet();
for (Character key:keySet){
Integer value = hm.get(key);
sb.append(key).append("(").append(value).append(")");
}
String result = sb.toString();
//输出
System.out.println(result);
}
}
Collections概述和使用
jdk9以上