Java 链表数组的哈希表
我正在尝试制作一个通用对象的链接列表。我已经制作了链表,现在我必须加载一个电影文件(在电影类中,我有流派、分级、标题)。首先我需要加载一个电影的CVS文件,即创建哈希表对象,该对象包含一个链表对象数组,该数组反过来存储电影。例如,在电影课上,我会有流派,流派可以很多。我想得到流派的散列代码,然后将其存储在数组链表的散列表中。这就是我使用LoadingMovie类的目的 我不知道我在做什么,因为这是我第一次使用哈希表和链接列表等。这是我到目前为止所做的:Java 链表数组的哈希表,java,generics,linked-list,hashmap,hashtable,Java,Generics,Linked List,Hashmap,Hashtable,我正在尝试制作一个通用对象的链接列表。我已经制作了链表,现在我必须加载一个电影文件(在电影类中,我有流派、分级、标题)。首先我需要加载一个电影的CVS文件,即创建哈希表对象,该对象包含一个链表对象数组,该数组反过来存储电影。例如,在电影课上,我会有流派,流派可以很多。我想得到流派的散列代码,然后将其存储在数组链表的散列表中。这就是我使用LoadingMovie类的目的 我不知道我在做什么,因为这是我第一次使用哈希表和链接列表等。这是我到目前为止所做的: public class List
public class List<T> {
private Node<T> head;
private Node<T> tail;
private int count;
public void append(T d) {
if (head == null) {
head = tail = new Node<T>(d);
} else {
tail.insertAfter(d);
tail = tail.getNext();
count++;
}
}
public void prepend(T d) {
if (head == null) {
head = tail = new Node<T>(d);
} else {
head.insertBefore(d);
head = head.getPrevious();
count++;
}
}
public void removeHead() {
if (head == null) {
return;
} else if (head == tail) {
head = tail = null;
count--;
return;
}
head = head.getNext();
count--;
}
public ListIterator<T> getIterator() {
return new ListIterator<T>(this, head);
}
public void add(ListIterator<T> iter, T data) {
if (iter.getList() != this) {
return;
}
if (!iter.isValid()) {
append(data);
} else {
iter.getCurrentNode().insertAfter(data);
count++;
if (iter.getCurrentNode() == tail) {
tail = iter.getCurrentNode().getNext();
}
}
}
public void remove(ListIterator<T> iter) {
if (iter.getList() != this) {
return;
}
Node<T> node = iter.getCurrentNode();
if (node == null) {
return;
} else if (node == head) {
removeHead();
} else if (node == tail) {
removeTail();
} else {
Node<T> ptn = node.getPrevious();
ptn.setNext(node.getNext());
node.getNext().setPrevious(ptn);
iter.advance();
count--;
}
}
public void removeTail() {
if (head == null) {
} else if (head == tail) {
head = tail = null;
count--;
} else {
Node<T> node = head;
while (node.getNext() != tail) {
node = node.getNext();
}
tail = node;
tail.setNext(null);
count--;
}
}
public void display() {
ListIterator<T> iter = getIterator();
do {
System.out.println(iter.item()+ " , ");
iter.advance();
} while (iter.isValid());
}
public void displayReverse() {
ListIterator<T> iter = getIterator();
iter.end();
do {
System.out.print(iter.item() + " , ");
iter.previous();
} while (iter.isValid());
}
public Node<T> getHead() {
return head;
}
public Node<T> getTail() {
return tail;
}
public int getCount() {
return count;
}
public void setHead(Node<T> head) {
this.head = head;
}
public void setTail(Node<T> tail) {
this.tail = tail;
}
public void setCount(int count) {
this.count = count;
}
@Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + Objects.hashCode(this.head);
hash = 89 * hash + Objects.hashCode(this.tail);
hash = 89 * hash + this.count;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final List<?> other = (List<?>) obj;
if (!Objects.equals(this.head, other.head)) {
return false;
}
if (!Objects.equals(this.tail, other.tail)) {
return false;
}
if (this.count != other.count) {
return false;
}
return true;
}
this is the node class :
public class Node<T> {
T anElement;
Node<T> next;
Node<T> previous;
public Node() {
anElement = null;
next = null;
}
public Node(T elem) {
anElement = elem;
next = null;
}
public T getAnElement() {
return anElement;
}
public void setAnElement(T anElement) {
this.anElement = anElement;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
public Node<T> getPrevious() {
return previous;
}
public void setPrevious(Node<T> previous) {
this.previous = previous;
}
@Override
public String toString() {
return "MyNode{" + "anElement=" + anElement + ", next=" + next + '}';
}
public void insertAfter(T nextData) {
if (nextData == null) {
return;
}
Node s = new Node(nextData);
s.setNext(next);
s.setPrevious(this);
if (next != null) {
next.setPrevious(s);
}
next = s;
}
public void insertBefore(T data) {
if (data == null) {
return;
}
Node s = new Node(data);
s.setNext(this);
s.setPrevious(previous);
if (previous != null) {
previous.setNext(s);
}
previous = s;
}
}
this is the load file class :
public class LoadingMovies {
private static final int size = 127;
private static HashMap<String, Movies> hash = new HashMap(size);
public static void loadMovies(String filename) {
String split = ","; //split with comma
try {
Scanner in = new Scanner(new File(filename));
String wordIn;
//List<Movies> linked = new List<>();
while (in.hasNextLine()) {
wordIn = in.nextLine();
String splitter[] = wordIn.split(split);
String movieTitle = splitter[0];
String movieGenre = splitter[1];
String ageRating = splitter[2];
double scoreRating = Double.parseDouble(splitter[3]);
Movies movie = new Movies();
movie.setTitle(movieTitle);
movie.setGenre(movieGenre);
movie.setAgeRating(ageRating);
movie.setScoreRating(scoreRating);
hash.find(movie.getGenre());
hash.insert(movie.getGenre(), movie);
hash.display();
}
} catch (FileNotFoundException e) {
System.out.println("Exception occured in the loadMovies() method in the Loadingovies class");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String filename = input.next();
loadMovies(filename);
}
this is the hash map method:
public class HashMap<KeyType,DataType>
{
private int count;
private int size;
private List<HashEntry<KeyType,DataType>> [] table;
public HashMap() {
}
public HashMap(int num)
{
size = num;
table = new List[num];
for(int i = 0; i < num; i++){
table[i] = new List<HashEntry<KeyType,DataType>>();
}
}
public void insert(KeyType key, DataType data){
if(key != null && data != null){
int hash = key.hashCode() % size;
HashEntry<KeyType, DataType> obj = new HashEntry(key, data);
table[hash].append(obj);
count++;
}
}
public void display(){
for(int i = 0 ; i < size; i++){
System.out.println("tables" + i + " ");
table[i].display();
}
}
public DataType find(KeyType key){
int hash = key.hashCode() % size;
List<HashEntry<KeyType,DataType>> list = table[hash];
ListIterator <HashEntry<KeyType, DataType>> iter = list.getIterator();
while(iter.isValid()){
if(iter.item().getKey().equals(key)){
return iter.item().getData();
}
iter.advance();
}
return null;
}
public void remove(KeyType key){
int hash = key.hashCode() % size;
List<HashEntry<KeyType,DataType>> list = table[hash];
ListIterator <HashEntry<KeyType, DataType>> iter = list.getIterator();
while(iter.isValid()){
if(iter.item().getKey().equals(key)){
list.remove(iter);
}
iter.advance();
}
}
}
and this is what i have for movie class:
public class Movies {
private String title;
private String genre;
private String ageRating;
private double scoreRating;
public Movies() {
title = "";
genre = "";
ageRating = "";
scoreRating = 0;
}
public Movies(String title, String genre, String ageRating, double scoreRating) {
this.title = title;
this.genre = genre;
this.ageRating = ageRating;
this.scoreRating = scoreRating;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getAgeRating() {
return ageRating;
}
public double getScoreRating() {
return scoreRating;
}
public void setTitle(String title) {
this.title = title;
}
public void setGenre(String genre) {
this.genre = genre;
}
public void setAgeRating(String ageRating) {
this.ageRating = ageRating;
}
public void setScoreRating(double scoreRating) {
this.scoreRating = scoreRating;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Movies other = (Movies) obj;
if (!Objects.equals(this.title, other.title)) {
return false;
}
if (!Objects.equals(this.genre, other.genre)) {
return false;
}
if (!Objects.equals(this.ageRating, other.ageRating)) {
return false;
}
if (Double.doubleToLongBits(this.scoreRating) != Double.doubleToLongBits(other.scoreRating)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 11;
hash = (int) ((hash * 10) + scoreRating);
if (this.title != null) {
hash = (hash * 10) + title.hashCode();
}
if (this.genre != null) {
hash = (hash * 10) + genre.hashCode();
}
if (this.ageRating != null) {
hash = (hash * 10) + ageRating.hashCode();
}
return hash;
}
@Override
public String toString() {
String statement = "Movie Title:" + title + "\n" + "Movie Genre:" + genre + "\n" + "Age Rating: " + ageRating + "\n" + "User Score: " + scoreRating + "\n";
return statement;
}
公共类列表{
专用节点头;
私有节点尾部;
私人整数计数;
公共无效附加(TD){
if(head==null){
头部=尾部=新节点(d);
}否则{
尾部插入器(d);
tail=tail.getNext();
计数++;
}
}
公共无效预结束(TD){
if(head==null){
头部=尾部=新节点(d);
}否则{
头。插入前(d);
head=head.getPrevious();
计数++;
}
}
公共无效删除头(){
if(head==null){
返回;
}else if(head==tail){
头=尾=空;
计数--;
返回;
}
head=head.getNext();
计数--;
}
公共ListIterator getIterator(){
返回新的ListIterator(这个,head);
}
公共无效添加(ListIterator iter,T数据){
if(iter.getList()!=此){
返回;
}
如果(!iter.isValid()){
附加(数据);
}否则{
iter.getCurrentNode().insertAfter(数据);
计数++;
if(iter.getCurrentNode()==tail){
tail=iter.getCurrentNode().getNext();
}
}
}
公共无效删除(ListIterator iter){
if(iter.getList()!=此){
返回;
}
Node=iter.getCurrentNode();
if(node==null){
返回;
}else if(节点==头部){
移除头部();
}else if(节点==尾部){
移除尾();
}否则{
Node ptn=Node.getPrevious();
ptn.setNext(node.getNext());
node.getNext().setPrevious(ptn);
iter.advance();
计数--;
}
}
公共无效删除邮件(){
if(head==null){
}else if(head==tail){
头=尾=空;
计数--;
}否则{
节点=头部;
while(node.getNext()!=tail){
node=node.getNext();
}
尾=节点;
tail.setNext(null);
计数--;
}
}
公共空间显示(){
ListIterator iter=getIterator();
做{
System.out.println(iter.item()+“,”);
iter.advance();
}while(iter.isValid());
}
公共无效显示反向(){
ListIterator iter=getIterator();
iter.end();
做{
System.out.print(iter.item()+“,”);
iter.previous();
}while(iter.isValid());
}
公共节点getHead(){
回流头;
}
公共节点getTail(){
返回尾;
}
public int getCount(){
返回计数;
}
公共无效设置头(节点头){
这个头=头;
}
公共void setTail(节点尾部){
this.tail=tail;
}
公共无效集合计数(整数计数){
this.count=计数;
}
@凌驾
公共int hashCode(){
int hash=5;
hash=89*hash+Objects.hashCode(this.head);
hash=89*hash+Objects.hashCode(this.tail);
hash=89*hash+this.count;
返回散列;
}
@凌驾
公共布尔等于(对象obj){
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最终列表其他=(列表)obj;
如果(!Objects.equals(this.head,other.head)){
返回false;
}
如果(!Objects.equals(this.tail,other.tail)){
返回false;
}
if(this.count!=其他.count){
返回false;
}
返回true;
}
这是节点类:
公共类节点{
T元素;
节点下一步;
节点前向;
公共节点(){
anElement=null;
next=null;
}
公共节点(T元素){
元素=元素;
next=null;
}
公共T getAnElement(){
返回元素;
}
公共无效setAnElement(T anElement){
this.anElement=anElement;
}
公共节点getNext(){
下一步返回;
}
公共void setNext(节点next){
this.next=next;
}
公共节点getPrevious(){
返回上一个;
}
公共void setPrevious(节点previous){
this.previous=先前;
}
@凌驾
公共字符串toString(){
返回“MyNode{“+”anElement=“+anElement+”,next=“+next+'}”;
}
公共无效插入符(T nextData){
如果(nextData==null){
返回;
}
节点s=新节点(nextData);
s、 setNext(下一个);
s、 (本);
如果(下一步!=null){
下一个。上一个(s);
}
next=s;
}
公共void insertBefore(T数据){
如果(数据==null){
返回;
}
节点s=新节点(数据);
s、 setNext(本);
s、 setPrevious(先前);
如果(上一个!=null){
上一个。下一个(s);
}
先前=s;
}
}
这是加载文件类:
公共类加载电影{
私有静态最终整数大小=127;
私有静态HashMap hash=新HashMap
int hash = key.hashCode() % size;
int hash = Math.abs(key.hashCode()) % size;