映射(Map):
Map 是一种键-值对(key-value)集合,Map 集合中的每一个元素都包含一个键对象和一个值对象。其中,键对象不允许重复,而值对象可以重复,并且值对象还可以是 Map 类型的,就像数组中的元素还可以是数组一样。
Map接口:
增删改查
public interface Map<K,V> {
//添加元素
void add(K key,V value);
//删除元素
V remove(K key);
//是否包含某元素
boolean contains(K key);
//通过键获取值
V get(K key);
//改key对应的value值
void set(K key,V value);
//获取Map中元素的个数
int getSize();
//Map是否为空
boolean isEmpty();
}
基于链表实现的LinkedListMap:
public class LinkedListMap<K,V> implements Map<K,V> {
private class Node {
public K key;
public V value;
public Node next;
public Node(K key,V value,Node next) {
this.key = key;
this.value = value;
this.next = next;
}
public Node(K key) {
this.key = key;
this.value = null;
this.next = null;
}
public Node() {
this.key = null;
this.value = null;
this.next = null;
}
@Override
public String toString(){
return key.toString()+":"+value.toString();
}
}
private Node dummyHead;
private int size;
public LinkedListMap(){
dummyHead = new Node();
size = 0;
}
@Override
public int getSize(){
return size;
}
@Override
public boolean isEmpty(){
return size == 0;
}
//返回key所在的Node
public Node getNode(K key){
Node cur = dummyHead.next;
while (cur != null){
if (cur.key.equals(key)){
return cur;
}
cur = cur.next;
}
return null;
}
@Override
public boolean contains(K key){
return getNode(key) != null;
}
@Override
public V get(K key){
Node node = getNode(key);
return node == null ? null : node.value;
}
@Override
public void add(K key,V value){
Node node = getNode(key);
if (node == null){
dummyHead.next = new Node(key,value,dummyHead.next);
size ++;
}else {
//映射中key是唯一的
node.value = value;
}
}
@Override
public void set(K key,V value){
Node node = getNode(key);
if (node == null) {
throw new IllegalArgumentException(key + "does't exist!");
}
node.value = value;
}
@Override
public V remove(K key){
Node prev = dummyHead;
while (prev.next != null){
if (prev.next.key.equals(key)){
break;
}
prev = prev.next;
}
if (prev.next != null){
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
return delNode.value;
}
return null;
}
}
基于二分搜索树实现的BSTMap:
public class BSTMap<K extends Comparable<K>,V> implements Map<K,V>{
private class Node{
public K key;
public V value;
public Node left,right;
public Node(K key,V value){
this.key = key;
this.value = value;
left = null;
right = null;
}
}
public Node root;
public int size;
public BSTMap(){
root = null;
size = 0;
}
@Override
public int getSize(){
return size;
}
@Override
public boolean isEmpty(){
return size == 0;
}
@Override
public void add(K key,V value){
}
//向以node为根的二分搜索树中插入元素(key,value),递归算法
//返回插入新节点后二分搜索树的根
private Node add(Node node,K key,V value){
if(node == null){
size ++;
return new Node(key, value);
}
if (key.compareTo(node.key) < 0){
node.left = add(node.left,key, value);
}else if(key.compareTo(node.key) > 0){ //插入重复元素说明什么也不做
node.right = add(node.right,key, value);
}else {
node.value = value;
}
return node;
}
//返回以node为根节点的二分搜索树中,key所在的节点
private Node getNode(Node node,K key){
if (node == null){
return null;
}
if (key.compareTo(node.key) == 0){
return node;
}else if (key.compareTo(node.key) < 0){
return getNode(node.left,key);
}else { //key.compareTo(node.key) > 0
return getNode(node.right,key);
}
}
@Override
public boolean contains(K key){
return getNode(root,key) != null;
}
@Override
public V get(K key){
Node node = getNode(root,key);
return node == null ? null : node.value;
}
@Override
public void set(K key, V value) {
Node node = getNode(root,key);
if (node == null){
throw new IllegalArgumentException(key + "dose't exist!");
}
node.value = value;
}
//返回以node为根的二分搜索树的最小值所在的节点,递归算法
private Node minimum(Node node){
if (node.left == null){
return node;
}
return minimum(node.left);
}
//删除以node为根的二分搜索树的最小节点
//返回删除节点后新的二分搜索树的根
private Node removeMin(Node node){
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size --;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
@Override
public V remove(K key){
Node node = getNode(root,key);
if (node != null){
root = remove(root,key);
return node.value;
}
return null;
}
//删除掉以node为根的二分搜索树中值为e节点,递归算法
//返回删除节点后新的二分搜索树的根
private Node remove(Node node,K key){
if (node == null){
return null;
}
if (key.compareTo(node.key) < 0){
node.left = remove(node.left,key);
return node;
}else if (key.compareTo(node.key) > 0){
node.right = remove(node.right,key);
return node;
}
else { //key == node.key
//待删除节点左子树为空的情况
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size --;
return rightNode;
}
//待删除节点右子树为空的情况
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size --;
return leftNode;
}
//待删除节点均不为空的情况
//找到比待删除节点大的最小节点,即待删除节点右子树的最小节点(后继节点)。
//或找到比待删除节点小的最大节点,即待删除节点左子树的最大节点(前驱节点)
//用这个节点顶替待删除节点的位置
Node successor = minimum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = null;
node.right = null;
return successor;
}
}
}
一般来说,BSTMap对数据操作时其性能是远高于LinkedListMap的,因为BSTMap增删改查的方法时间复杂度是O(logn)的,而LinkedListMap是O(n)的。