Java:从Hashtable获取Object值<integer,hashtable <string,object>&gt; </integer,hashtable <string,object>

时间:2013-09-14 10:54:10

标签: java hashtable

我在Hashtable<Integer,Hashtable<String,Object>>中编写了一些对象属性,其中:

  • Integer是主要Hashtable(代表对象编号)
  • 的关键字
  • 每个Hashtable<String,Object>分别代表属性name (String)和属性value(Object)

我想将所有属性值放入包含属性值的ArrayList(或数组...)中,然后想要访问每个Object。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

如果您只想要一个列表用于此目的,Maxim的解决方案非常好,或者您可以创建一个实现集合和内部管理哈希表的自定义类。对象。如果您需要在程序的许多方面使用它,我喜欢第二种方式。

例如,您可以修改此类并将Element T添加为具有链接到字符串键的所有proprerties和hastable的自定义类,并在此类中添加用于搜索关键名称的custo mmethod(英语的借口):

  public class NList<T> implements Iterable<T> //, List<T>
  {
     private boolean synchron;
     public List<T>  list;

     public NList(boolean synchron)
     {
        this(15, synchron);
     }

     public NList(int initialCapacity, boolean synchron)
     {
        this.synchron = synchron;
        this.list = synchron ? new Vector<T>(initialCapacity) : new ArrayList<T>(initialCapacity);
     }

     public NList(Collection<T> c, boolean synchron)
     {
        this.synchron = synchron;
        this.list = synchron ? new Vector<T>(c) : new ArrayList<T>(c);
     }

     public final boolean isSynchronized()
     {
        return synchron;
     }

     //@Override
     public final boolean add(T element)
     {
        return list.add(element);
     }

     //@Override
     public final void add(int index, T element)
     {
        list.add(index, element);
     }

     //@Override
     public final T remove(int index)
     {
        return list.remove(index);
     }

     //@Override
     public final List<T> subList(int fromIndex, int toIndex)
     {
        return list.subList(fromIndex, toIndex);
     }

     //@Override
     @SuppressWarnings("unchecked")
     public final T[] toArray()
     {
        return (T[])list.toArray();
     }

     //@Override
     public final T get(int index)
     {
        return list.get(index);
     }

     //@Override
     public final int size()
     {
        return list.size();
     }

     //@Override
     public final boolean isEmpty()
     {
        return list.isEmpty();
     }

     //@Override
     public final void clear()
     {
        list.clear();
     }

     @Override
     public final Iterator<T> iterator()
     {
        return list.iterator();
     }

     //@Override
     public final boolean contains(Object element)
     {
        return list.contains(element);
     }

     //@Override
     @SuppressWarnings("hiding")
     public final <T> T[] toArray(T[] a)
     {
        return list.toArray(a);
     }

     //@Override
     public final boolean remove(Object element)
     {
        return list.remove(element);
     }

     //@Override
     public final boolean containsAll(Collection<?> c)
     {
        return list.containsAll(c);
     }

     //@Override
     public final boolean addAll(Collection<? extends T> c)
     {
        return list.addAll(c);
     }

     //@Override
     public final boolean addAll(int index, Collection<? extends T> c)
     {
        return list.addAll(index, c);
     }

     //@Override
     public final boolean removeAll(Collection<?> c)
     {
        return list.removeAll(c);
     }

     //@Override
     public final boolean retainAll(Collection<?> c)
     {
        return list.retainAll(c);
     }

     //@Override
     public final T set(int index, T element)
     {
        return list.set(index, element);
     }

     //@Override
     public final int indexOf(Object o)
     {
        return list.indexOf(o);
     }

     //@Override
     public final int lastIndexOf(Object o)
     {
        return list.lastIndexOf(o);
     }

     //@Override
     public final ListIterator<T> listIterator()
     {
        return list.listIterator();
     }

     //@Override
     public final ListIterator<T> listIterator(int index)
     {
        return list.listIterator(index);
     }
  }

答案 1 :(得分:0)

这样的事情:

List<HashTable<Integer,Object>> list = new ArrayList<HashTable<Integer,Object>>();

订单使用LinkedHashTable

这就是你如何访问HashTable

Hashtable<Integer, Hashtable<String, Object>> properties = new Hashtable<Integer, Hashtable<String, Object>>(); 

    Enumeration<Integer> nmExt;
    Enumeration<String> nmInt;

    Hashtable<String, Object> innerHash;
    int externalKey;
    String interanlKey;
    Object obj;

    nmExt = properties.keys();

       while (nmExt.hasMoreElements()){
           externalKey = nmExt.nextElement();

           innerHash =  properties.get(externalKey);

           nmInt = innerHash.keys();

           while (nmExt.hasMoreElements()){
               interanlKey = nmInt.nextElement();

               obj = innerHash.get(interanlKey);

               // .....
           }

        }