简体   繁体   中英

printing values in hashtable Java

My code stores values in a hashtable and I was wondering how can I print the data from the hashtable? I'm not quite sure how I can iterate through the hashtable to print out it's value. I'm quite new to Java so I don't really know which built in functions I can use.

My code:

public static void deleteDups(LinkedListNode n) {
    Hashtable table = new Hashtable();
    LinkedListNode previous = null;
    while (n != null) {
        if (table.containsKey(n.data)) {
            previous.next = n.next;
        }
        else {
            table.put(n.data, true);
            previous = n;
        }
        n = n.next;
    }

}
for(Object o : table.keySet()) {
    LinkedListNode lln = (LinkedListNode)o;
    System.out.println(lln.data);
}

Also note that you'd be better declaring your table as a Hashtable<LinkedListNode, Boolean> , which would allow you to iterate on the keys as

for(LinkedListNode lln : table.keySet()) {
    System.out.println(lln.data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM