Java实现T first()方法到类Hashset <t>

时间:2015-12-03 11:11:31

标签: java data-structures

我在进行以下练习时遇到困难。我不知道采用哪种方式。我知道我应该使用某种迭代,但我不确定。我已经能够使用二叉搜索树实现T first()方法,但不能使用HashSet。

Add the following method to class HashSet<T> and write a suitable test program. T first() // least value in the set (if the set is empty // throws NoSuchElementException)

    import java.util.*;
    import java.lang.Iterable;

 class HashSet<T extends Comparable<T>> implements Iterable<T> { 

  private LinkedSet<T>[] hashTable; // hash table


HashSet() { // create the empty set
    hashTable = (LinkedSet<T>[])(new LinkedSet[1000]); 
                                              // note coding trick!     
    for (int i=0; i<hashTable.length; i++)
        hashTable[i] = new LinkedSet<T>();  

    //Exercise 1
    int numItems = 0;
    for (LinkedSet<T> miniSet: hashTable)
        numItems = numItems+miniSet.size();


}




private int hash(T t) { // hash t into hashTable index
    return Math.abs(t.hashCode()%hashTable.length);
}

int size() {    
    int numItems = 0;
    for (LinkedSet<T> miniSet: hashTable)
        numItems = numItems+miniSet.size();
    return numItems;
}

boolean contains(T t) { 
    return hashTable[hash(t)].contains(t);
}

boolean add(T t) { 
    return hashTable[hash(t)].add(t);
} 

boolean remove(T t) {
    return hashTable[hash(t)].remove(t);
}

//Exercise 3

  public Iterator<T> iterator() { 
    ArrayList<T> items = new ArrayList<T>();
    for (LinkedSet<T> ls: hashTable)
        for (T t: ls) items.add(t);
    return items.iterator();
}   





  boolean addAll(HashSet<T> ts){
    boolean changed = false;
    for(T i : ts)
        if(add(i))
            changed =true;
    return true;
    // add all elements of ts to set; ts is unchanged.

    }





}

import java.util.Iterator;




 public class Prog {

    public static <T extends Comparable<T>> T first(HashSet<T> hs)
    // least value in the set (if the set is empty
   // throws NoSuchElementException
     {
      T least = null;

      for(T i : hs){
      if (i.compareTo(least)<0){
        i = least;
      }

     }
      return least;
    }

import java.util.List;


  public class main1 {

   public static void main(String[] args) {
    // TODO Auto-generated method stub
    HashSet<String> test1 = new HashSet<String>();
    test1.add("sean");
    test1.add("adam");
    test1.add("ava");

    HashSet<Integer> test2 = new HashSet<Integer>();
    test2.add(2);
    test2.add(10);
    test2.add(5);

    System.out.println(test1.size());

    System.out.println(Prog.first(test2));


   }

 }

2 个答案:

答案 0 :(得分:0)

你可以:

  • 遍历值:尝试(T x:the_set)...

  • 取最小值:取第一个,一个迭代,如果新的更少,那就拿它吧

  • 返回该值(如果没有值则返回异常)

尝试完成此

public static <T> T first(HashSet<T> _ht)
{
// if _ht empty throws an exception
// TODO

// Take the first (or any element)
// TODO
T least; 

for (T one_element: _ht)
  {
    // compare one_element and least
    // TODO 
    // and keep the least !
  }

return least;
}

答案 1 :(得分:0)

我同意Peter的评论,HashSet没有“第一”或“最后”的概念,因为它没有被命令。但是,有一个TreeSet类,它实现了SortedSet

元素类型必须定义自然顺序(例如intComparable<T>),否则您必须为树集的构造函数提供Comparator<T>

示例:

Integer[] numbers = { 5, 9, 1, 11 };
TreeSet<Integer> set = new TreeSet<>(Arrays.asList(numbers));
Integer least = set.first();  // 1

first()方法的一种可能实现(假设二叉树):

public T first() {
    Node<T> p = root;
    if (p != null) {
        while (p.left != null) {  // not "while (p != null)"
            p = p.left;
        }
    }
    return p == null ? null : p.item;
}