飞道的博客

java day028 Map与匿名内部类

441人阅读  评论(0)

java day028 Map与匿名类

1.Map

1.1 Map概述

Map是一个双边队列

数据存储形式为键(Key)值(Value)对形式

Map双边队列对于数据存储类型有限制,存储数据类型在创建Map双边队列时进行约束,保证类型一致化;

但是也没有约束,Map可以满足任意类型的存储

Map使用了俩个泛型

Map<K, V>

1.2 Map整体结构与常用API

interface Map<K, V>
--| class HashMap<K, V>
	底层存储数据结构为哈希表。存储数据时先存储Key,然后通过Key计算出Value的存储地址
--| class TreeMap<K, V>
	底层存储数据结构为二叉树结构,要求存储的键值对Key必须有对应的排序方式,若没有则需要Comparable<T>接口与Comparator<T>接口	
	

常用API
增
	put(K key, V value);
		添加符合Map要求的键值对存入双边队列中
	putAll(Map<? extends K, ? extends V> map);
		添加子集,要求K必须是当前Map本身对应的K或者其子类,V必须是当前Map本身对应的V或者其子类

删
	remove(Object Key);
	删除对应的键值对
改
	put(K key, V value);
	使用value修改已经存在的Key对应的值
查
	int size();
		Map双边队列个数
	boolean isEmpty();
		判断是否为空
	boolean containsKey(Object key);
		判断指定的Key是否存在
	boolean containsValue(Object value);
		判断指定的value是否存在
	Set<K> keySet();
		返回Map所有Key对应的Set集合
	Collection<V> values();
		返回Map中所有value对应的Collection集合

1.3 HashMap方法演示

package com.qfedu.study.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * HashMap示例
 * @author Arkay
 *
 */
public class TestHashMap {
	public static void main(String[] args) {
		
		/**
		 * Map<K, V> 前者是键key,后者是值value,key必须是唯一的
		 */
		Map<Integer, String> m1 = new HashMap<Integer, String>();
		Map<Integer, String> m2 = new HashMap<Integer, String>();
		
		//添加元素
		m1.put(1, "one");
		m1.put(2, "two");
		m1.put(3, "three");
		m1.put(4, "four");
		m1.put(5, "five");
		
		System.out.println(m1);
		
		//添加子集合
		m2.put(6, "six");
		m1.putAll(m2);
		System.out.println(m1);
		
		//若key相同,value不同继续添加则会修改value的值
		m1.put(1, "1");
		System.out.println(m1);
		
		//删除key以及key映射的值
		m1.remove(1);
		System.out.println(m1);
		
		//查询方法
		System.out.println(m1.size());
		System.out.println(m1.isEmpty());
		System.out.println(m1.containsKey(3));
		System.out.println(m1.containsValue("six"));
		
		//得到key对应的值
		System.out.println(m1.get(2));
		
		//返回m1集合中key对应的Set集合
		Set<Integer> key = m1.keySet();
		System.out.println(key);
		
		//返回m1集合中value对应的Collection集合
		Collection<String> c = m1.values();
		System.out.println(c);
	}
}
/*
输出结果:
{1=one, 2=two, 3=three, 4=four, 5=five}
{1=one, 2=two, 3=three, 4=four, 5=five, 6=six}
{1=1, 2=two, 3=three, 4=four, 5=five, 6=six}
{2=two, 3=three, 4=four, 5=five, 6=six}
5
false
true
true
two
[2, 3, 4, 5, 6]
[two, three, four, five, six]
*/

1.4 TreeMap方法演示

package com.qfedu.study.map;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

class Person implements Comparable<Person> {
	
	int age;
	String name;
	
	public Person() {}
	
	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + "]";
	}
	
	/**
	 * 自定义比较方式
	 */
	@Override
	public int compareTo(Person o) {
		return o.age - this.age;
	}
	
}

/**
 * Person类的比较器
 * @author Arkay
 *
 */
class PersonCompare implements Comparator<Person> {

	@Override
	public int compare(Person o1, Person o2) {
		return o1.age - o2.age;
	}
	
}

class Test {}

public class TestTreeMap {
	public static void main(String[] args) {
		
		//采用Person类内比较方式
		Map<Person, Integer> m1 = new TreeMap<Person, Integer>();
		
		m1.put(new Person(14,"张三"), 1);
		m1.put(new Person(18,"李四"), 2);
		m1.put(new Person(20,"王五"), 3);
		System.out.println(m1);
		
		//采用自定义比较器
		Map<Person, Integer> m2 = new TreeMap<Person, Integer>(new PersonCompare());
		
		m2.put(new Person(14,"张三"), 1);
		m2.put(new Person(18,"李四"), 2);
		m2.put(new Person(20,"王五"), 3);
		System.out.println(m2);
		
		//注意:在TreeMap中当key有自己的比较方式时可以不用自定义比较
		TreeMap<Integer, Test> m3 = new TreeMap<Integer, Test>();
		m3.put(1, new Test());
		m3.put(2, new Test());
		m3.put(3, new Test());
		System.out.println(m3);
		
	}
}
/*
输出结果:
{Person [age=20, name=王五]=3, Person [age=18, name=李四]=2, Person [age=14, name=张三]=1}
{Person [age=14, name=张三]=1, Person [age=18, name=李四]=2, Person [age=20, name=王五]=3}
{1=com.qfedu.study.map.Test@15db9742, 2=com.qfedu.study.map.Test@6d06d69c, 3=com.qfedu.study.map.Test@7852e922}
*/

1.5 Map中的entrySet方法

目的:把Key与Value进行封装,完全按照一个数据类型来处理

Set<Map.Entry<K, V>> entrySet();
	返回值类型是Entry键值对形式数据的Set集合
	Set<Map.Entry<K, V>>
		Map.Entry<K, V> Map接口的内部接口Entry,使用的泛型K, V 对应Map创建过程中被约束的K和V
		因为返回值是Set集合,集合带有泛型 Set<Map接口的内部接口Entry>
	
	Entry对应的API
		K getKey();
		V getValue();
		V setValue(V value);
	
package com.qfedu.study.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TestEntry {
	public static void main(String[] args) {
		Map<Integer, String> m1 = new HashMap<Integer, String>();
		
		m1.put(1, "one");
		m1.put(2, "two");
		m1.put(3, "three");
		m1.put(4, "four");
		m1.put(5, "five");
		
		Set<Entry<Integer, String>> set = m1.entrySet();
		for (Entry<Integer, String> entry : set) {
			System.out.println(entry);
		}
	}
}
/*
输出结果:
1=one
2=two
3=three
4=four
5=five
*/

2.匿名内部类

格式:

最原始格式:
接口名 接口引用数据类型变量 = new 接口名() {
	//当前接口要求实现的方法
}
接口引用数据类型变量调用方法使用,或者作为方法的参数传入

匿名内部类的匿名对象直接作为方法参数
Method(new 接口名{
	//当前接口要求实现的方法
});

简化代码冗余度,提高效率,增强功能

interface A {
    public void test();
}

main() {
    
    //第一种格式
    A a = new A{
        @Override
        public void test() {
            System.out.println("匿名内部类的对象赋值给接口引用数据类型变量");
        }
    };
    a.test();
    
    //第二种格式:匿名内部类的匿名对象直接调用方法
    new A() {
        @Oveeride
        public void test() {
            System.out.println("匿名内部类的匿名对象直接调用方法");
        }
    }.test();
    
    //第三种格式:匿名内部类的匿名对象,直接作为方法的参数
    testInterfce(new A() {
        @Override
        public void test() {
             System.out.println("匿名内部类的匿名对象,直接作为方法的参数");
        }
    });
    
}

public static void testIntetface(A a) {
    a.test();
}

转载:https://blog.csdn.net/weixin_43684474/article/details/106201438
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场