Map的使用
/**
* @Auther:Yuhai
* @Date:2022/3/24-03-24-13:23
* @Description:IntelliJ IDEA
* @version:1.0
*/
public class MapDemo {
// public static void main(String[] args) {
// Map map = new HashMap();
// //添加
// map.put("aaa", 123);
// map.put(45, 123);
// map.put("BB", 123);
// map.put("DD", 123);
//
// System.out.println(map);
// System.out.println("********************");
//
// map.putAll(map);
// // System.out.println(map);
//
// map.remove("BB");
// System.out.println(map);
public static void main(String[] args) {
Map map = new HashMap();
//添加
map.put("aaa", 123);
map.put(45, 9387);
map.put("BB", 13);
map.put("DD", 13948);
//根据指定的key找到指定的value
System.out.println(map .get("BB") );
//根据指定的value找到指定的key
System.out.println(map.containsValue(123));
//判断集合中的中是否存在这个key
boolean isExit = map.containsKey("aaa");
System.out.println(isExit);
//清除集合中的元素
map.clear();
System.out.println(map.size());
}
}
map的集合遍历:
根据指定的方式进行遍历
如: 根据指定的键找到指定的值,
/**
* @Auther:Yuhai
* @Date:2022/3/24-03-24-13:51
* @Description:IntelliJ IDEA
* @version:1.0
*/
public class MapTest {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("www", 27);
map.put("273", 24);
map.put("w23", 23);
map.put("w44", 25);
//遍历集合中的key集
Set set = map.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
//遍历集合中的value集
Collection collection = map.values();
for (Object o : collection) {
System.out.println(o);
}
//遍历集合方式二
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
System.out.println("*****************************");
}
}
TreeMap的简单例子
/**
* @Auther:Yuhai
* @Date:2022/3/19-03-19-16:21
* @Description:IntelliJ IDEA
* @version:1.0
*/
class phone implements Comparable<phone> {
// 实现Comparable接口,重写CompareTo方法
private String Brand;
private int Price;
public phone(String brand,int price){
Brand = brand;
Price = price;
}
public String getBrand() {
return Brand;
}
public void setBrand(String brand) {
Brand = brand;
}
public int getPrice() {
return Price;
}
public void setPrice(int price) {
Price = price;
}
//字符串排序使用comPareTo方法
//重写CompareTo 方法来进行数据排序
@Override
public int compareTo(phone o) {
//根据价格来进行排序的,这个可以试低价向高价,也可以是高价向低价
// return ((phone)o).getPrice()-(this.getPrice());
//return ((phone)o).getPrice()-(this.getPrice());
return (this.getPrice())-((phone)o).getPrice();
}
}
/*
reeMap 键不允许插入null
TreeMap: 键的数据结构是红黑树,可保证键的排序和唯一性
排序分为自然排序和比较器排序
线程是不安全的效率比较高
TreeMap集合排序:
实现Comparable接口,重写CompareTo方法
使用比较器
*/
public class TreeMapDemo {
public static void main(String[] args) {
//创建集合对象
TreeMap<phone, String> map = new TreeMap<>();
//添加元素
map.put(new phone("华为", 6000), "中国");
map.put(new phone("苹果", 12999), "美国");
map.put(new phone("索尼", 4599), "日本");
map.put(new phone("三星", 5466), "韩国");
map.put(new phone("诺基亚",210),"亚马逊");
//遍历集合
Set<phone> phones = map.keySet();
Iterator<phone> iterator = phones.iterator();
while (iterator.hasNext()){
phone next = iterator.next();
System.out.println(next.getBrand()+"==="+next.getPrice()+"==="+map.get(next));
}
}
}
。
下面是几个简单的demo
public class Student {
private String name;
private int age;
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);
}
}
/**
* @Auther:Yuhai
* @Date:2022/3/16-03-16-8:42
* @Description:IntelliJ IDEA
* @version:1.0
*/
/*
问题:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
要求:学生成员变量值相同,我们认为是同一个对象
思路:
1.创建学生类
2.创建HashSet集合对象
3.创建学生对象
4.把学习添加到集合
5.遍历结合(增强for循环)
*/
public class HashSetDemo {
public static void main(String[] args) {
// 创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>();
// 3.创建学生对象
Student s1 = new Student("张三", 21);
Student s2 = new Student("小明", 20);
Student s3 = new Student("小红", 19);
Student s4 = new Student("小红",19);
//把学生添加到集合
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
//对象不重复可以使用的方法重写
//在学生类中重写两个方法
// hashCode()和equals(),自动生成即可
//遍历集合
for (Student s : hs) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
/**
* @Auther:Yuhai
* @Date:2022/3/26-03-26-8:57
* @Description:IntelliJ IDEA
* @version:1.0
*/
public class MapDemo2 {
public static void main(String[] args) {
String str = "aa@sohu.com,bb@163.com,cc@sina.com";
//创建数组,将上面的信息分配到数组中,以便得到每一个email
String strs[] = str.split(",");
// System.out.println(str);
//创建集合对象,存放分割后的email信息
Map Map = new HashMap();
//得到单独的email对应关系,从而使@变为“=”
for (String email: strs){
String temp[] = email.split("@");
Map.put(temp[0],temp[1]);
}
System.out.println(Map);
System.out.println("**************************");
//遍历集合
Set set1 = Map.entrySet();
for (Object entry : set1){
System.out.println(entry);
}
}
}
总结: map集合如果有一个键就可以找到对应的值,相反,如果有一个值那么不可以找到对应的键,这是map集合的唯一性。
在遍历集合时,使用增强For循环无疑是一种简单的遍历方式,但是如果加入其他条件的情况下,增强For循环的能力就会受到限制。如果想要取里面的值,就可以使用。其他情况下尽量使用迭代(iterator)器来遍历集合