结合Java语言与数据结构学科内容,利用Java语言实现数据结构中的顺序表(ArrayList)与散列表(Hashmap),这两种数据结构能够对数据进行存取、删减等操作。
程序功能要求:
1.实现的两种数据结构应该能够对数据进行存取、删除操作;
2.程序应当包含去重功能,当有重复的数据添加时应当覆盖原有的数据;
3.除基本存取功能外,还应当能够进行迭代遍历;
4.程序能够进行查找操作,传入一个已有的数据参数能够返回对应的位置(仅ArrayList需要);
都自定义实现了,望采纳!
散列表
package algorithm;
import java.util.Arrays;
/**
* @author jiangnanmax
* @email jiangnanmax@gmail.com
* @description HashMap
* @date 2021/5/14
**/
public class HashMap<K, V> {
/**
* 默认的初始化大小,这里设置为16
*/
private static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
/**
* 默认的扩容因子,这里设置为0.75
*/
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private Entry<K, V>[] table;
final float loadFactor;
private int size = 0;
private int use = 0;
/**
* 默认初始化方法,使用默认的CAPACITY和扩容因子
*/
public HashMap() {
this(DEFAULT_INITIAL_CAPACITY);
}
/**
* 指定CAPACITY的初始化方法,使用默认的扩容因子
*
* @param initCapacity
*/
public HashMap(int initCapacity) {
this(initCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* 指定CAPACITY和扩容因子的初始化方法
*
* @param initCapacity
* @param loadFactor
*/
public HashMap(int initCapacity, float loadFactor) {
this.table = new Entry[initCapacity];
this.loadFactor = loadFactor;
}
/**
* 添加键值对,已存在的键会被新值覆盖
*
* @param key
* @param value
*/
public void put(K key, V value) {
int index = hash(key);
Entry<K, V> e = table[index];
if (e == null) {
table[index] = new Entry<>(key, value, null);
size++;
use++;
if (use >= table.length * loadFactor) {
resize();
}
} else {
for (; e != null; e = e.next) {
K k = e.key;
if (k.equals(key)) {
e.value = value;
return ;
}
}
Entry<K, V> tmp = table[index];
Entry<K, V> newEntry = new Entry<K, V>(key, value, tmp);
table[index] = newEntry;
size++;
}
}
/**
* 获取键相应的值
*
* @param key
* @return
*/
public V get(K key) {
int index = hash(key);
Entry<K, V> e = table[index];
for (; e != null; e = e.next) {
K k = e.key;
if (k.equals(key)) {
return e.value;
}
}
return null;
}
/**
* 删除某一个键值对
*
* @param key
*/
public void remove(K key) {
int index = hash(key);
Entry<K, V> e = table[index];
Entry<K, V> pre = null;
for (; e != null; pre = e, e = e.next) {
K k = e.key;
if (k.equals(key)) {
if (pre == null) {
table[index] = e.next;
} else {
pre.next = e.next;
}
size--;
return ;
}
}
}
/**
* 存在性判断
*
* @param key
* @return
*/
public boolean contains(K key) {
int index = hash(key);
Entry<K, V> e = table[index];
for (; e != null; e = e.next) {
if (e.key.equals(key)) {
return true;
}
}
return false;
}
/**
* 清空
*/
public void clear() {
Arrays.fill(table, null);
size = 0;
use = 0;
}
/**
* 获取当前容器中的元素个数
*
* @return
*/
public int size() {
return size;
}
/**
* 判空
*
* @return
*/
public boolean isEmpty() {
return size == 0;
}
/**
* 计算键的对应slot
*
* @param key
* @return
*/
private int hash(K key) {
int hashCode = Math.abs(key.hashCode());
hashCode %= table.length;
return hashCode;
}
/**
* 扩容
*/
private void resize() {
int newLength = table.length << 1;
Entry<K, V>[] oldTable = table;
table = new Entry[newLength];
use = 0;
for (int i = 0; i < oldTable.length; i++) {
Entry<K, V> e = oldTable[i];
for (; e != null; e = e.next) {
int index = hash(e.key);
if (table[index] == null) {
table[index] = new Entry<>(e.key, e.value, null);
use++;
} else {
Entry<K, V> newEntry = new Entry<>(e.key, e.value, table[index]);
table[index] = newEntry;
}
}
}
}
static class Entry<K, V> {
K key;
V value;
Entry<K, V> next;
Entry(K key, V value, Entry<K, V> next) {
this.key = key;
this.value = value;
this.next = next;
}
}
}
用法
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>(2);
map.put("name", "jiangnanmax");
map.put("age", "**");
map.put("blog", "https://jiangnanmax.blog.csdn.net/");
map.put("ecnu", "cs");
map.put("ecnu", "dase");
System.out.println(map.get("name"));
System.out.println(map.get("ecnu"));
System.out.println(map.size());
System.out.println(map.contains("age"));
map.remove("age");
System.out.println(map.contains("age"));
}
/**
输出:
jiangnanmax
dase
4
true
false
*/
做题不易, 如果有帮助辛苦采纳, 谢谢
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
// 结合Java语言与数据结构学科内容,利用Java语言实现数据结构中的顺序表(ArrayList)与散列表(Hashmap),这两种数据结构能够对数据进行存取、删减等操作。
// 程序功能要求:
// 1.实现的两种数据结构应该能够对数据进行存取、删除操作;
// 2.程序应当包含去重功能,当有重复的数据添加时应当覆盖原有的数据;
// 3.除基本存取功能外,还应当能够进行迭代遍历;
// 4.程序能够进行查找操作,传入一个已有的数据参数能够返回对应的位置(仅ArrayList需要);
ArrayList<String> list = new ArrayList<>();
//ArrayList存储数据
list.add("佟湘玉");
list.add("莫小贝");
list.add("白展堂");
list.add("李大嘴");
//AraryList删除数据
list.remove("白展堂");
//ArrayList迭代遍历
for (String s : list) {
System.out.println("遍历list:"+s);
}
//ArrayList传入一个已有的数据参数能够返回对应的位置(仅ArrayList需要)
int index = findIndex(list, "莫小贝");
System.out.println("在list中找莫小贝的索引"+index);
//===================以下是HashMap=========================
HashMap<String,Integer> map =new HashMap<>();
//HashMap存储数据
map.put("郭芙蓉",20);
map.put("吕秀才",22);
map.put("小六",19);
map.put("老邢",39);
//HashMap删除数据
map.remove("老邢");
//map在添加重复key时候, 新加的数据会覆盖原来数据 , 相当于修改value了
map.put("小六",30);
//map迭代遍历
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
System.out.println("遍历map,key:"+entry.getKey()+",value:"+entry.getValue());
}
}
public static int findIndex(ArrayList<String> list,String s){
//编写一个方法, 如果在list中找到s, 返回s的索引. 没找到 返回-1
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(s)){
return i;
}
}
return -1;
}
}
顺序表 哈希表
点过去 看源码
arraylist 是顺序的,放进去什么顺序取出还是什么顺序,sort集合,是无序的,内容不可重复的
知道底层原理就行了,何必花时间去做一些无用功呢,我们要站在巨人的肩膀上去做更有意义的事情,不要去重复造轮子,没啥意义的