Map<k,v>
1、双列集合
2、一个元素包含两个值(k,v)
3、一个键对应一个值,键是唯一的。
4、key和value一一对应,可以通过key找到value,也就是键值对。
package MapExercise;
import java.util.HashMap;
import java.util.Map;
public class MapTest1 {
public static void main(String[] args) {
// method1();
// method2();
// method3();
method4();
}
private static void method4() {
// containsKey(object key) 判断集合中是否包含指定的键
Map<String,String> map = new HashMap<>();
map.put("name","亚索");
map.put("price","79");
boolean t = map.containsKey("name");
boolean f = map.containsKey("age");
System.out.println(t);
System.out.println(f);
}
private static void method3() {
// get(object key)
// key 存在返回对应的value值
// key不存在,返回null
Map<String,String> map = new HashMap<>();
map.put("name","亚索");
map.put("price","79");
System.out.println(map);
String name = map.get("name");
System.out.println(name);
String age = map.get("age");
System.out.println(age);
}
private static void method2() {
// remove(object key) 返回V,key存在,返回被删除的值,
// key不存在,返回null
Map<String,String> map = new HashMap<>();
map.put("name","亚索");
map.put("price","79");
System.out.println(map);
System.out.println("----------");
String price = map.remove("price");
System.out.println("price: " + price);
System.out.println(map);
System.out.println("age: " + map.remove("age"));
}
private static void method1() {
//put(K key,V value) 返回V,
// 存储键值对时,key不重复,返回值为null,
// key重复时,返回的是被替换的value的值
Map<String,String> map = new HashMap<>();
String s1 = map.put("name","亚索");
System.out.println(s1);
String s2 = map.put("name","剑圣");
System.out.println(s2);
map.put("price","79");
System.out.println(map);
}
}
package MapExercise;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest2 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("name","亚索");
map.put("price","79");
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
String key = iterator.next();
String value = map.get(key);
System.out.println(key + ":" +value);
}
System.out.println("-------------");
for (String key:map.keySet()) {
String value = map.get(key);
System.out.println(key + ":" +value);
}
}
}
在Map接口中有一个内部接口Entry,记录键值对
Map集合里面有一个entrySet方法,把Map集合内部的多个Entry对象取出来存到Set集合当中。
Set<Map.Entry<K,V>>
遍历Set集合得到每一个Entry对象
Entry接口中有两个方法,getkey(),getvalue()
package MapExercise;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapTest3 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("name","亚索");
map.put("price","99");
Set<Map.Entry<String,String>> set = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry<String, String> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+": "+value);
}
System.out.println("-----------");
for (Map.Entry<String, String> entry :set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+": "+value);
}
}
}
Map集合保证key是唯一的,作为key的元素,必须重写hashCode方法和equals方法,以保证key唯一。
package MapExercise;
import java.util.Objects;
public class Hero {
private String name;
private int price;
public Hero() {
}
public Hero(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Hero hero = (Hero) o;
return price == hero.price &&
Objects.equals(name, hero.name);
}
@Override
public int hashCode() {
return Objects.hash(name, price);
}
@Override
public String toString() {
return "Hero{" +
"name=‘" + name + ‘\‘‘ +
", price=" + price +
‘}‘;
}
public void setPrice(int price) {
this.price = price;
}
}
package MapExercise;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapTest4 {
public static void main(String[] args) {
// method1();
method2();
}
private static void method2() {
// key:Hero value:String
HashMap<Hero,String> map = new HashMap<>();
map.put(new Hero("亚索",99),"真实伤害");
map.put(new Hero("剑圣",79),"御剑传说");
map.put(new Hero("盖伦",99),"神王");
map.put(new Hero("盖伦",99),"神王2");
Set<Map.Entry<Hero,String>> set = map.entrySet();
for (Map.Entry<Hero,String> entry : set){
Hero key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}
}
private static void method1() {
// key:String value:Hero
HashMap<String,Hero> map = new HashMap<>();
map.put("hero1",new Hero("亚索",99));
map.put("hero2",new Hero("剑圣",79));
map.put("hero3",new Hero("锐雯",199));
map.put("hero4",new Hero("盖伦",99));
Set<String> strings = map.keySet();
for (String key:strings) {
Hero price = map.get(key);
System.out.println(price);
}
}
}
package MapExercise;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class LinkedHashMapExercise {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
map.put("name1","亚索");
map.put("name2","剑圣");
map.put("name3","盖伦");
System.out.println(map);
LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("name1","亚索");
linkedHashMap.put("name2","剑圣");
linkedHashMap.put("name3","盖伦");
System.out.println(linkedHashMap);
}
}
/*
计算一个字符串中每个字符出现的次数
*/
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("获取字符串中每个字符以及对于的个数");
System.out.println("请输入一个字符串:");
String string = input.next();
Map<Character,Integer> map = new HashMap<>();
for (char c:string.toCharArray()) {
if(map.containsKey(c)){
Integer count = map.get(c);
count++;
map.put(c,count);
}
else {
map.put(c,1);
}
}
for (Map.Entry<Character,Integer> entry:map.entrySet()) {
Character character = entry.getKey();
Integer integer = entry.getValue();
System.out.println(character+": "+integer);
}
}
}
List、Set、Map接口里面添加了一个静态的方法of,可以一次性添加多个元素
1、当前集合存储的元素的个数已经确定了,不能改变。
2、of方法只适应List、Set、Map接口,不适用于接口的实现类
3、of方法的返回值是一个不能改变的集合,集合不能再使用add和put方法添加元素
4、Set接口和Map接口使用of方法的时候,不能有重复的元素
import java.util.List;
import java.util.Map;
import java.util.Set;
public class OfExercise {
public static void main(String[] args) {
List<String> list = List.of("亚索","剑圣","锐雯","盖伦");
System.out.println(list);
Set<String> set = Set.of("亚索","剑圣","锐雯","盖伦");
System.out.println(set);
Map<String, String> hero = Map.of("亚索", "99", "盖伦", "99");
System.out.println(hero);
}
}
原文:https://www.cnblogs.com/saonian450/p/12549567.html