标签:增加 i++ key 代码 ash hash shm 简单 存储
public class Entry { // 键 private Object key; // 值 private Object value; //构造器 public Entry(Object key, Object value) { super(); this.key = key; this.value = value; } public Object getKey() { return key; } public void setKey(Object key) { this.key = key; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } }
public class HashMap { //存储元素数组 private Entry[] entry = null; //记录map个数 private int size; //构造器 public HashMap() { this.entry = new Entry[100]; } //增加新元素 public void put(Object key, Object value) { Entry e = new Entry(key, value); entry[size] = e; size++; } //获取元素 public Object get(Object key) { for (int i = 0; i < size; i++) { Entry e = entry[i]; Object k = e.getKey(); if (key.equals(k)) { return e.getValue(); } } return null; } }
代码测试
public class Demo { /*** * 简单的 put get方法 * @param args */ public static void main(String[] args) { HashMap map=new HashMap(); map.put("刘诗华", "罗兰"); Object s = map.get("刘诗华"); System.out.println(s); } }
标签:增加 i++ key 代码 ash hash shm 简单 存储
原文地址:https://www.cnblogs.com/hua900822/p/9763520.html