Both list and hashtable can store data. Why does list and hashtable need to be selected sometimes? What is the difference between them?

Source: Internet
Author: User
(Programming solution) both list and hashtable can store data. Why are the differences between list and hashtable sometimes selected? This is a frequently asked question during interviews. I have been asked this question. I have not considered it before, but I just want to compare it with vector or
Arraylist, used
Hashtable should still use hashmap. I like to use the example test and add a note to it to better understand it. For more details about the relationship between sets, see: A set of mathematical examples (including comparison and examples of vector, arraylist, list, and so on ). ImportJava. util. *;/*** both list and hashtable can store data. Why are there differences between them when selecting list and hashtable? * The following example can answer this question * @ AuthorHttp://blog.csdn.net/fenglibing **/ Public ClassListandhashtable {// privatize the constructor, so you cannot create new objects for other objects. PrivateListandhashtable () {testlist (); testhashtable ();}/*** @ ParamARGs */ Public Static VoidMain (string [] ARGs ){// TodoAuto-generated method stub NewListandhashtable (); // but it can be used normally here .} VoidTestlist () {list = NewArraylist (); list. add ("ok1"); list. add ("ok1"); // test whether repeated Value List is allowed. add (0, "ok2"); list. add (0, "ok2"); // test whether to replace the previous value with list when specifying the insert position. add ("ok3 "); For( IntI = 0; I <list. Size (); I ++) {// The value of list is obtained by subscript, and the object system cannot be retrieved by value. Out. Print ("" + list. get (I);} // The output above is: ok2 ok2 ok1 ok1 ok3, indicating that repeated values are allowed. // The original value will not be replaced even if the subscript is specified, will automatically move back. System. Out. Println (); iterator it = List. iterator (); While(It. hasnext () {system. Out. Print ("" + it. Next ();} // The output above is: ok2 ok2 ok1 ok1 ok3 system. Out. Println (); iterator it1 = List. listiterator (); While(It1.hasnext () {system. Out. Print ("" + it1.next ();} // The output above is: ok2 ok2 ok1 ok1 ok3} VoidTesthashtable () {hashtable table = NewHashtable (); table. Put ("A", "you are very good! "); Table. Put (" B "," you not are very good! "); Table. Put (" C "," You are very bad! "); Table. Put (" A "," you are very good! Last "); system. Out. Print ("" + table. Get ("A"); // The following output is: You are very good! Last indicates that a key can only have one value, and the subsequent value will replace the original value // value method. It can only be set by pressing the key, but cannot be set by subscript. System. Out. Println (); system. Out. Println (table. tostring (); // The output result is: {B = You not are very good !, A = You are very good! Last, c = You are very bad !} // The table key value can be converted into a string to output enumeration E = table. Elements (); // enumeration. values can be listed in sequence. While(E. hasmoreelements () {system. Out. Print (E. nextelement ();} // The output result is: you not are very good! You are very good! Lastyou are very bad! }} ClassTest { PrivateTest () {// because the listandhashtable constructor is private, the listandhashtable L = NewListandhashtable ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.