1、集合框架Map介绍
方法归类
map集合中存放的都是一组组映射关系 key=value
Map集合没有继承Collection接口,其提供的是key到value的映射 如下:
package map.lww.com;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*
* map集合中三个特有的方法
* 1.put
* 当容器中已经有了key,再次存放,会覆盖原有的key所对应的值value
* 调用此方法,可以获取原来key对应的值
*
* 2.entryset
* 3.keyset
* 这两个方法map集合中特有的方法
*
* hasmap是无序的,集合的底层是set集合做的
*
*
* hashmap 数据结构 哈希表
* treemap数据结构 二叉树
* 能够进行自然排序以及比较器排序
*
* map集合不继承collection接口
* 所有不具备迭代器的方法
*
*
*
*
* */
public class MapDemo {
public static void main(String[] args) {
Map map=new HashMap<>();
map.put("zs", 12);
map.put("ww", 22);
map.put("mm", 33);
map.put("hh", 55);
map.put("ll", 44);
map.put("ss", 88);
map.put("ww", 23);
Object old=map.put("ww",23);
// System.out.println(old);
System.out.println(map);
//
// Set<Entry<Object,Object>> entrySet=map.entrySet();
// for(Entry<Object,Object> entry : entrySet) {
// System.out.println("key:"+entry.getKey() +",value:"+entry.getValue());
// }
Set<Object> keySet = map.keySet();
for(Object key:keySet) {
System.out.println("key:"+key + ",value:" +map.get(key));
}
如果存在相同的会被覆盖,取后一个
}
2.map的应用
package map.lww.com;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeMap;
/**
* 案例1
* 将学生作为键,地址作为值进行储存,名字年龄相同被认定为一个人,最后输出
*
* 思路:
* a.封装学生类
* b.判重(hashcode/equals)
* c.打印
*
* 2.按年龄排序
* 3.需求改变,按姓名进行排序
*
* 改变代码是不可取的,需要新增比较器来完成需求
* 实现java.util.comparator
*
* */
public class HashMapDemo {
public static void main(String[] args) {
// HashMap map=new HashMap<>();
TreeMap map=new TreeMap<>(new StuNameComp());
map.put(new Student("zs",25), "bj");
map.put(new Student("ww",44), "sz");
map.put(new Student("mm",25), "gz");
map.put(new Student("hh",55), "cs");
map.put(new Student("ll",66), "hz");
map.put(new Student("zs",25), "zj");
Set keySet=map.keySet();
for (Object key : keySet) {
System.out.println(key);
}
}
}
class StuNameComp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0) {
return o1.getAge() - o2.getAge();
}
return num;
}}
class Student implements Comparable<Student>{
private String name;
private int 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 String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.name.hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
Student stu = (Student) obj;
return this.name.equals(stu.name) && this.age==stu.age;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num = this.age-o.age;
if(num ==0) {
this.name.compareTo(o.name);
}
return num;
}
}
排序如图:(按名字排序)
然后这个是没有排序的
案例2:
package map.lww.com;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeMap;
/**
* 案例1
* 将学生作为键,地址作为值进行储存,名字年龄相同被认定为一个人,最后输出
*
* 思路:
* a.封装学生类
* b.判重(hashcode/equals)
* c.打印
*
* 2.按年龄排序
* 3.需求改变,按姓名进行排序
*
* 改变代码是不可取的,需要新增比较器来完成需求
* 实现java.util.comparator
*
* */
public class HashMapDemo {
public static void main(String[] args) {
// HashMap map=new HashMap<>();
TreeMap map=new TreeMap<>(new StuNameComp());
map.put(new Student("zs",25), "bj");
map.put(new Student("ww",44), "sz");
map.put(new Student("mm",25), "gz");
map.put(new Student("hh",55), "cs");
map.put(new Student("ll",66), "hz");
map.put(new Student("zs",25), "zj");
Set keySet=map.keySet();
for (Object key : keySet) {
System.out.println(key);
}
}
}
class StuNameComp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0) {
return o1.getAge() - o2.getAge();
}
return num;
}}
class Student implements Comparable<Student>{
private String name;
private int 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 String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.name.hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
Student stu = (Student) obj;
return this.name.equals(stu.name) && this.age==stu.age;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num = this.age-o.age;
if(num ==0) {
this.name.compareTo(o.name);
}
return num;
}
}
根据上面要求,名字年龄相同被认定为一个人,然后这个就是覆盖同一个人,最后输出
集合框架根据类(collections,Arrays)
package map.lww.com;
import java.util.Arrays;
/**
* 集合框架根据类(collections,Arrays)
*
* */
public class UtilDemo {
public static void main(String[] args) {
int arr[] = {32,453,545,662};
System.out.println(arr);
//二分搜索法/冒泡排序/选择排序/快速排序/希尔排序
System.out.println(Arrays.toString(arr));
}
}
}