在Java哈希表中返回泛型类型

在Java哈希表中返回泛型类型,java,generics,Java,Generics,我们目前正在Java课程中学习哈希表 演讲者为我们提出了一些构建的方法。前两个很好,但我正在努力解决“public E max()”。我读过的大多数东西似乎都表明不能实例化泛型类型,所以我很难理解如何为哈希表编写这个方法 目标当然是返回哈希表中的最大值,如果类型不是泛型,我想我可以这样做,但在本例中是这样的 如果我的代码有点难读,请道歉 import java.lang.reflect.Array; import java.util.*; public class Assignment6_20

我们目前正在Java课程中学习哈希表

演讲者为我们提出了一些构建的方法。前两个很好,但我正在努力解决“public E max()”。我读过的大多数东西似乎都表明不能实例化泛型类型,所以我很难理解如何为哈希表编写这个方法

目标当然是返回哈希表中的最大值,如果类型不是泛型,我想我可以这样做,但在本例中是这样的

如果我的代码有点难读,请道歉

import java.lang.reflect.Array;
import java.util.*;

public class Assignment6_2015 {
public static void main(String[] args){

//=======================================================
// Question 1, test Point class by creating a hashlist of Point instances       

    HashList<Point> h1 = new HashList<Point>(5);
    h1.add(new Point(1,2));
    h1.add(new Point(2,4));
    h1.add(new Point(2,4));
    h1.add(new Point(2,4));
    h1.add(new Point(3,8));
    h1.add(new Point(3,8));
    h1.add(new Point(7,3));
    h1.add(new Point(9,10));
    h1.add(new Point(9,10));
    h1.add(new Point(9,10));
    h1.add(new Point(9,10));
    h1.add(new Point(9,10));
    h1.displayLists();

//=======================================================
// Question 2, testing new methods

    // ----- Frequency Method Test -----
    System.out.println();
    System.out.print("Frequency of Points (9,10): ");
    System.out.println(h1.freq(new Point(9,10)));

    System.out.println();
    System.out.print("Frequency of Points (2,4): ");
    System.out.println(h1.freq(new Point(2,4)));

    System.out.println();
    System.out.print("Frequency of Points (1,2): ");
    System.out.println(h1.freq(new Point(1,2)));
    // ----- End Frequency Method Test -----

    System.out.println();
    System.out.print("Table Size: ");
    System.out.println(h1.tableSize());
    System.out.print("All used?: ");
    System.out.println(h1.allUsed());
    System.out.print("Percentage used?: ");
    System.out.println(h1.percentUsed());
    }
    }

    //=======================================================
    // class Point
    class Point implements Comparable<Point>{
    private int x,y;
    Point(int a, int b){x = a; y = b;}
    public int x(){return x;}
    public int y(){return y;}
    public String toString(){return "("+x+","+y+")";}

    public boolean equals(Object ob){
    Point p = (Point)ob;
    if(x == p.x() && y == p.y()){
        return true;
    }
    else{
        return false;
    }
}

public int compareTo(Point p){
    if(x > p.x() && y > p.y()){
        return 0;
    }
    else{
        return -1;
    }
}

public int hashCode(){
    return x*y*31;
    }
}
   //End class Point
  //=======================================================
  //HashTable class
  class HashList<E extends Comparable<E>>{
  private GLinkedList<E> data[];
  public HashList(int n){
    data = (GLinkedList<E>[])(new GLinkedList[n]);
    for(int j = 0; j < data.length;j++)
        data[j] = new GLinkedList<E>();
}
private int hashC(E x){
    int k = x.hashCode();
    //an alternative is to mask the minus using
    //int k = x.hashCode() & 0x7fffffff;

    int h = Math.abs(k % data.length);
    return(h);
}

public void add(E x){
    int index = hashC(x);
    data[index].add(x);
}

public boolean contains(E x){
    int index = hashC(x);
    return(data[index].contains(x));
}
public void displayLists(){
    for(GLinkedList<E> k : data){
     if(k.length() > 0)
          k.display();
    }
}
public void display(){
    System.out.print("<");
    int ind = 0;
    while(ind < data.length){
        Iterator<E> it = data[ind].iterator();
        while(it.hasNext())
            System.out.print(it.next()+" ");
        ind++;
    }
    System.out.println(">");
}
public int tableSize(){
    return data.length;
}
//===================================================================
//Add new methods for assignment here

public int freq(E x){
    int freq = 0;
    int index = hashC(x);
    for(int j = 0; j < data[index].length();j++){
        if(data[index].contains(x)){
            freq++;
        }
    }
        return freq;
}

public boolean allUsed(){
    int total = data.length;
    int inuse = 0;
    for(int j = 0; j < data.length;j++){
        if(data[j].length() >= 1){
            inuse++;
        }
    }

    if(inuse == total){return true;}
    else{return false;}
}

public E max(){
    int j = 0;
    E y = // ???;
    Iterator<E> it = data[j].iterator();
        while(j<data.length){
            it = data[j].iterator();
            E x = it.next();
            while(it.hasNext()){
                if(data[j].iterator().next().compareTo(x) == 0){
                    x = it.next();
                    y = x;
                }
            }
        j++;
        }
    return y;
}

//int x = (it.next().compareTo(largest));
//if(x == 0){largest = it.next();}




//====================================================================
public double percentUsed(){
    int count = 0;
    for(int j = 0; j < data.length; j++){
        if(data[j].length() > 0)
          count++;
    }
    double p = count *100.0 / data.length;
    return p;
}
public int largestBucket(){
    int max = 0;
    for(int j = 0; j < data.length; j++)
        if(data[j].length() > max) max = data[j].length();
    return max;
}
public int smallestBucket(){
    int min = data[0].length();
    for(int j = 1; j < data.length; j++)
        if(data[j].length() < min) min = data[j].length();
    return min;
}
public int[] listSizes(){
    int n = this.largestBucket();
    int d[] = new int[n+1];
    for(int j = 0; j < d.length; j++) d[j] = 0;
    for(int j = 0; j < data.length; j++){
        int m = data[j].length();
        d[m] = d[m] + 1;
    }
    return d;
}
public int empty(){
    int count = 0;
    for(int j = 0; j < data.length; j++)
        if(data[j].length() == 0) count++;
    return count;
}
public Iterator<E> iterator(){
  ArrayList<E> items = new ArrayList<E>();
  int ind = 0;
  while(ind < data.length){
        Iterator<E> it = data[ind].iterator();
        while(it.hasNext())
            items.add(it.next());
        ind++;
   }
   return items.iterator();
}
}
class GLinkedList<E extends Comparable<E>>{
private Node<E> head = null;//empty list
private int size = 0;
public void add(E x){ //add at head
    Node<E> nw = new Node<E>(x);
    nw.setNext(head);
    head = nw;
    size++;
}

public boolean contains(E x){
    Node<E> k = head;
    boolean found = false;
    while(k != null && !found){
        E kk = k.data();
        if(kk.compareTo(x) == 0) found = true;
        else k = k.next();
    }
    return found;
}

public void remove(E x){
    Node<E> k = head; Node<E> bk = head;
    boolean found = false;
    while(k != null && !found){
        if(k.data().compareTo(x) == 0) found = true;
        else{ bk = k; k = k.next();}
    }
    if(found)
        if(k == head)
            head = k.next();
        else
          bk.setNext(k.next());
}

public int length(){
    return size;
}
public void display(){
    Node<E> k = head;
    System.out.print('[');
    while(k != null){
        if(k.next != null)
           System.out.print(k.data()+", ");
        else
           System.out.print(k.data());
        k = k.next();
    }
    System.out.println(']');
}
public Iterator<E> iterator(){
    return new GIterator<E>(head, size);
}
     private static class GIterator<E extends Comparable<E>> implements Iterator<E>{
    private Node <E> head;
    private int size;
    private int index = 0;
    GIterator(Node<E> h, int s){
        head = h; size = s;
    }
    public boolean hasNext(){
        return index < size;
    }
    public E next(){
        if(index == size) throw new NoSuchElementException();
        E item = head.data();
        head = head.next(); index++;
        return item;
    }
    public void remove(){}
}
}
class Node<E extends Comparable<E>>{
E data;
Node<E> next;
public Node(E  x){
    data = x; next = null;
}
public Node<E> next(){return next;}
public void setNext(Node<E> p){
    next = p;
}
public void set(E x){data = x;}
public E data(){return data;}
}
import java.lang.reflect.Array;
导入java.util.*;
公共课堂作业6_2015{
公共静态void main(字符串[]args){
//=======================================================
//问题1,通过创建点实例的哈希列表来测试点类
HashList h1=新的HashList(5);
h1.增加(新的点(1,2));
h1.增加(新的点(2,4));
h1.增加(新的点(2,4));
h1.增加(新的点(2,4));
h1.增加(新的第(3,8)点);
h1.增加(新的第(3,8)点);
h1.增加(新的第(7,3)点);
h1.增加(新的第(9,10)点);
h1.增加(新的第(9,10)点);
h1.增加(新的第(9,10)点);
h1.增加(新的第(9,10)点);
h1.增加(新的第(9,10)点);
h1.displayLists();
//=======================================================
//问题2,测试新方法
//----频率法试验-----
System.out.println();
系统输出打印(“点数频率(9,10):”;
系统输出打印LN(h1频率(新点(9,10));
System.out.println();
系统输出打印(“点数频率(2,4):”;
系统输出println(h1.freq(新点(2,4));
System.out.println();
系统输出打印(“点数频率(1,2):”;
系统输出println(h1.freq(新点(1,2));
//----端频率法试验-----
System.out.println();
系统输出打印(“表格大小:”);
System.out.println(h1.tableSize());
系统输出打印(“全部使用:”;
System.out.println(h1.allUsed());
系统输出打印(“使用百分比:”;
System.out.println(h1.percentUsed());
}
}
//=======================================================
//类点
类点实现了可比较的{
私有整数x,y;
点(inta,intb){x=a;y=b;}
公共int x(){return x;}
public int y(){return y;}
公共字符串toString(){return“(“+x+”,“+y+”);}
公共布尔等于(对象ob){
p点=(点)ob;
如果(x==p.x()&&y==p.y()){
返回true;
}
否则{
返回false;
}
}
公共国际比较(p点){
如果(x>p.x()&&y>p.y()){
返回0;
}
否则{
返回-1;
}
}
公共int hashCode(){
返回x*y*31;
}
}
//终点
//=======================================================
//哈希表类
类哈希表{
专用GLinkedList数据[];
公共哈希表(int n){
数据=(GLinkedList[])(新GLinkedList[n]);
对于(int j=0;j0)
k、 显示();
}
}
公共空间显示(){
系统输出打印(“”);
}
public int tableSize(){
返回数据长度;
}
//===================================================================
//在此处添加新的赋值方法
公共整数频率(E x){
intfreq=0;
int index=hashC(x);
对于(int j=0;j=1){
inuse++;
}
}
如果(inuse==total){return true;}
else{return false;}
}
公共E max(){
int j=0;
E y=/???;
迭代器it=数据[j]。迭代器();
while(j0)
计数++;
}
双p=计数*100.0/数据长度;
返回p;
}
public int largestBucket(){
int max=0;
对于(int j=0;jmax)max=data[j].length();
返回最大值;
}
公共int smallestBucket(){
int min=数据[0]。长度();
对于(int j=1;jE y=null;
for ( GLinkedList<E> d : data ) {
  final Iterator<E> it = d.iterator();
  while ( it.hasNext() ) {
    final E currentElement = it.next();

    // Insert logic here!

  }
}
declare max as a `Comparable` 
for each linked-list `ll` in `data`
   for each element `c` in `ll`
      if `c.compareTo(max) >= 0` then
         max <- c
      endif
   endfor
endfor
return max
E y = (E)data[0]; // ???;
int j = 1;
if(y==null) return null; //since there is no element in the array