Skip to main content
 首页 » 编程设计

java算法之自定义线性结构-Map

2022年07月18日78xxx_UU
** 
 * @desc: 基于单链表实现Map结构
* 实现Iterable接口,方便遍历Map
* @author: 毛会懂 * @create: 2020-12-30 13:31:00 **/ public class MyMap<K,V> implements Iterable<K>{ private Node head; private Integer count; public MyMap() { this.head = new Node(null,null,null); count = 0; } //插入元素 public void put(K key,V value){ Node newNode = new Node(key,value,null); Node temp = head; while (temp.next != null){ temp = temp.next; if(temp.key.equals(key)){ temp.value = value; return; } } temp.next = newNode; count++; } //获取元素 public V get(K key){ Node temp = head; while (temp.next != null){ temp = temp.next; if(temp.key.equals(key)){ return temp.value; } } return null; } //删除元素 public V del(K key){ if(isEmpty()){ return null; } Node node = head; Node pre = null; while (node.next != null){ //上一个节点 pre = node; //当前节点 node = node.next; if(node.key.equals(key)){ pre.next = node.next; return node.value; } } return null; } public Boolean isEmpty(){ return count == 0; } // public Integer size(){ return count; } @Override public Iterator<K> iterator() { return new MyIterator(); } public class MyIterator implements Iterator<K>{ private Node node; public MyIterator(){ node = head; } @Override public boolean hasNext() { return node.next != null; } @Override public K next() { node = node.next; return node.key; } } private class Node{ private K key; private V value; private Node next; public Node(K key, V value, Node next) { this.key = key; this.value = value; this.next = next; } } }

 //map的key可以是对象,这里实现了Comparable接口,为下一篇有序Map做铺垫。

public class Student implements Comparable<Student>{ 
    private String name; 
    private Integer age; //为了方便,假设age不能为空吧 
 
    public Student(String name, Integer age) { 
        this.name = name; 
        this.age = age; 
    } 
 
    public String getName() { 
        return name; 
    } 
 
    public void setName(String name) { 
        this.name = name; 
    } 
 
    public Integer getAge() { 
        return age; 
    } 
 
    public void setAge(Integer age) { 
        this.age = age; 
    } 

@Override
public boolean equals(Object obj) { if(this == obj){ return true; } if(obj == null){ return false; } if(obj instanceof Student){ Student temp = (Student) obj; //这里就假设age不能为空吧 if(temp.age.equals(this.age)){ return true; } } return false; } @Override public int hashCode() { return age.hashCode(); } @Override public int compareTo(Student o) { return this.getAge() - o.age; } }

测试:

//非有序的map
public static void main2(String[] args) {
MyMap<String,String> map = new MyMap();
map.put("abc","abc");
map.put("bcd","dddd");
System.out.println("取元素:" + map.get("bcd"));
System.out.println("取元素:" + map.get("abcd"));
System.out.println("元素的个数:" + map.size());
System.out.println("元素是否为空:" + map.isEmpty());

System.out.println("--------以下演示key为Student对象的使用----------");
Student[] stus = {
new Student("b", 11),
new Student("a", 10),
new Student("c", 12),
new Student("b", 111),
new Student("a", 111),
new Student("c", 4)};
MyMap<Student,Integer> stuMap = new MyMap<>();
stuMap.put(stus[0],11);
stuMap.put(stus[1],111);
stuMap.put(stus[2],11);
stuMap.put(stus[3],122);
stuMap.put(stus[4],11); //这里根据年龄判断的key是否相同,所以stus[3]和stus[4] key相同
stuMap.put(stus[5],11);
Integer value = stuMap.get(stus[3]);
System.out.println(value);
System.out.println("遍历学生map");
Iterator<Student> iterator = stuMap.iterator();
while (iterator.hasNext()){
Student next = iterator.next();
System.out.println("姓名:" + next.getName() + " 年龄:" + next.getAge() + " 值:" + stuMap.get(next));
}
System.out.println("-----删除元素-----");
Integer del = stuMap.del(stus[5]);
System.out.println("删除的value=" + del);
}

本文参考链接:https://www.cnblogs.com/maohuidong/p/14216183.html