常用方法总结
package com.java.Collectiom;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/*Mop和Collection之间没有继承关系
* Map集合的常用方法
Map也支持泛型 并且Map集合也有一些公用的方法
添加对象 put(key, element);
通过key获取value get(key)
获取键值数目 size()
*/
public class Maptest01 {
public static void main(String [] agrs) {
//创建Map集合
Map<Integer ,String > map= new HashMap<>();
//添加对象
map.put(1, "zhangsan");
map.put(2, "zhang");
map.put(3, "san");
map.put(4, "jack");
//通过key获取value
String s= map.get(1);
System.out.println(s);
//获取键值数目
System.out.println("键值对数目"+ map.size());//4
//通过key栓除value
map.remove(2);
System.out.println("键值对数目"+ map.size());//3
//判断包含某个对象
System.out.println(map.containsValue("zhangsan"));//ture
//获取所有的value
Collection <String> values= map.values();
for(String o:values)
System.out.println(o);
//清空集合
map.clear();
System.out.println("键值对数目"+ map.size());//0
//判断是否是空
System.out.println(map.isEmpty());//true
}
}
遍历方式
package com.java.Collectiom;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*Map集合的遍历方式
1 获取所有的Key,遍历Key来遍历集合
keySet()
2.把Map集合转化为Set集合(大数据类)
entrySet()
*/
public class Maptest02 {
public static void main(String [] agrs) {
//创建map集合(这里使用一下泛型)
Map<Integer ,String > mm= new HashMap<>();
//添加一些元素
mm.put(1, "张三");
mm.put(2, "李四");
mm.put(3, "王伟");
mm.put(4, "小王");
//加强foreach循环取出Values值
Collection ss=mm.values();
for(Object nn:ss) {
System.out.println(nn);
}
//1 获取所有的Key,遍历Key来遍历集合
Set<Integer>keys=mm.keySet();
/*
* for(Integer key:keys) {
System.out.println(mm.get(key));
}
*/
Iterator<Integer> it =keys.iterator();
while(it.hasNext()) {
Integer key= it.next();
String value=mm.get(key);
System.out.println(key+"="+value);
}
//2.把Map集合转化为Set集合
/*
* for(Map.Entry<Integer, String> key:keys) {
System.out.println(getKey()+"="+getValue());
}
*/
Set<Map.Entry<Integer, String>> set=mm.entrySet();
Iterator<Map.Entry<Integer, String>> its=set.iterator();
while(its.hasNext()) {
Map.Entry<Integer, String> node=its.next();
Integer key=node.getKey();
String values =node.getValue();
System.out.println(key+"="+values);
}
}
}
练习遍历
package com.java.Collectiom;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//map集合遍历
public class Maotest03 {
public static void main(String [] agrs) {
Map<Integer, String > map= new HashMap<>();
map.put(1, "Adad");
map.put(2, "Add") ;
map.put(3, "Ad");
//遍历集合
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
Integer key = it.next();
String value = map.get(key);
System.out.println(key + "=" + value);
}
for (Integer key : set) {
System.out.println(map.get(key));
}
System.out.println("============================================");
Set<Map.Entry<Integer, String>> ss = map.entrySet();
Iterator<Map.Entry<Integer, String>> its = ss.iterator();
while (its.hasNext()) {
Map.Entry<Integer, String> key = its.next();
System.out.println(key.getKey() + "=" + key.getValue());
}
System.out.println("============================================");
for (Map.Entry<Integer, String> key : ss) {
System.out.println(key.getKey() + "=" + key.getValue());
}
}
}
练习2(map)集合存放学生信息
package com.java.everdattest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*利用集合存储学生对象信息
创建学生类 包括学生的姓名 学号 提供构造方法
将学生对象存放在集合中
判断集合中是否有某个人
判断集合的大小
输出学生信息
使用泛型机制
使用加强for循环
*/
public class Todaystest05 {
public static void main(String [] agrs) {
// 创建对象
Student a = new Student("张三", 12551552);
Student b = new Student("小明", 1254556);
Student c = new Student("jack", 1236556);
Student d = new Student("bob", 166556);
Student e = new Student("tom", 1525556);
// 创建集合
Map<Integer, Student> map = new HashMap<>();
// 添加元素
map.put(1, a);
map.put(2, b);
map.put(3, c);
map.put(4, d);
map.put(5, e);
//通过key获取value
Student Obj= map.get(1);
System.out.println(Obj);//掉toString()方法
// 获取key值
System.out.println(map.size());
// 删除某个对象(bob)
map.remove(4);//注意和集合不一样这里是一一对应的
// 获取所有的values
Collection<Student> mm = map.values();
for (Student o : mm) {
System.out.println(o);
//输出对象 张三 小明 jack tom
}
//判断是否包含bob
boolean falg =map.containsValue("bab");
System.out.println(falg);
//遍历集合
//将key转化成Set集合
Set<Integer> keys=map.keySet();
//获取迭代器
Iterator<Integer> it =keys.iterator();
//遍历 1
while(it.hasNext()) {
Integer key=it.next();
Student Values=map.get(key);
System.out.println(Values);
}
//遍历2
Set<Map.Entry<Integer, Student>> set=map.entrySet();
Iterator <Map.Entry<Integer, Student>> its=set.iterator();
while(its.hasNext())
{
Map.Entry<Integer, Student>node =its.next();
Integer key=node.getKey();
Student values =node.getValue();
System.out.println(key+"="+values);
}
}
}
//定义学生类
class Student{
String name;//姓名
int no;//编号
//构造方法
public Student() {
}
public Student(String name ,int no) {
this.name =name;
this.no=no;
}
public String toString() {
return " 姓名 "+name
+" 学号 "+no;
}
public boolean equals(Object obj) {
if(this==obj)return true ;
if(null==obj||!(obj instanceof Student)) return false;
Student c =(Student) obj;
if(this.name.equals(c.name)&&this.no==c.no) return false;
return false;
}
}