本文主要是介绍容器第六课,自定义Map的功能,底层源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.pkushutong.Collection;/*** 自定义实现Map的功能* Map:存放键值对,根据键对象找对应的值对象,键不能重复*/
public class Test05 {SxtEntry[] arr = new SxtEntry[100];int size;public void put(Object Key, Object value){SxtEntry e = new SxtEntry(Key, value);for(int i=0; i<size; i++){if(arr[i].Key.equals(Key)){arr[i].value = value;return ;}}arr[size++] = e;}public Object get(Object Key){for (int i = 0; i < size; i++) {if(arr[i].Key.equals(Key)){return arr[i].value;}}return null;}public boolean containsKey(Object Key){for (int i = 0; i < size; i++) {if(arr[i].Key.equals(Key)){return true;}}return false;}public boolean containsValue(Object value){for (int i = 0; i < size; i++) {if(arr[i].value.equals(value)){return true;}}return false;}public static void main(String[] args) {Test05 map = new Test05();map.put("李四", new wife("花花"));map.put("李四", new wife("蕾蕾"));wife w = (wife) map.get("李四");System.out.println(w.name);}}class SxtEntry{Object Key;Object value;public SxtEntry(Object key, Object value) {super();Key = key;this.value = value;}
}
这篇关于容器第六课,自定义Map的功能,底层源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!