Java Collection-python data structure comparison

Source: Internet
Author: User

The Java list is more than the Python list

Java List: Ordered, repeatable. (ordinal refers to the order of the objects in the collection in the same order as they were added)

The Python list (list) is ordered and mutable.

Java List Categories:

---ArrayList: low-level use of arrays, thread insecure, Find Fast, and delete slow

During the iteration, the additions and deletions to the collection object will appear abnormally

---linkedlist: low-level use of linked lists, thread insecure, slow to find, and delete faster

LIFO, similar to stack

---Vector: low-level use of arrays, thread-safe, Find Fast, and delete slow, replaced by ArrayList

The Python list is not categorized, and list is the basic data structure of Python.

method Java List Python List:[1, ' 2 ', 3]    Python tuple: (+/-)
Add to End boolean add (E E ); list.append (' great ') Immutable
added to the specified position void Add (int index, E element); List.insert (2, ' The Great ') Immutable
length int size (); len (list) Same as List
update e set (int index, e element); list[i]= ' great ' Immutable
Remove E Remove (int index); List.pop (i) Immutable
Delete all void Clear (); list.clear () immutable, you can use Del to delete tuples
Find E get (int index); List[i] Same as List

Java map is more dict than Python

Java map belongs to the collection, but not part of the collection system, unordered, non-repeatable, in the form of key-value pairs.

Python dict Full Name dictionary, also known as map in other languages, uses key-value (Key-value) storage, has extremely fast lookup speed, and dict key must be an immutable object. Because Dict uses a hashing algorithm based on key to calculate the storage location of value .

Java Map Classification:

---HASHMAP: The data structure used at the bottom is a hash table

the uniqueness of the hold key is the same as HashSet the same.

---TREEMAP: The data structure used at the bottom is a two-fork tree

the uniqueness of the hold key is the same as TreeSet the same.

The Python dictionary is not categorized, and Dict is the basic data structure of Python.

Method Java Map Python dict:{' 1 ': ' 1 ', ' 2 ': ' 2 '}
Increase V Put (K key, v value); dict[' 1 ']= ' the Great '
Delete V Remove (Object key); Del dict[' 1 ']
Change Same as Add, overwrite the original value if the key is the same dict[' 1 ']= ' king '
Find V get (Object key); dict[' 1 ']

Java set is more than Python set

Java set is used at the bottom of the Java map key, the value is set to NULL, so the set and map remain unique principle is the same.

Python set is similar to Dict and is a collection of key sets, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.

Java Set: Unordered, non-repeatable

---HashSet: the underlying uses a hash table, thread insecure

the only way to guarantee the object : Override Hashcode (), Equals (object obj). Using hashing algorithms to cause unordered

---TreeSet: The bottom uses a two-fork tree, thread insecure

When you add an object by using the Add method, the objects that are joined to the collection are sorted

The only way to guarantee the object : 1 Implement the Comparable<e> interface, the implementation of the CompareTo () method return value is 0, you cannot join. 2 Create a class to implement COMPARATOR<T>, implement the Compare () method.

Python collections are not categorized, and set is the basic data structure of Python.

Method Java Set Python Set
Create Pass in a list:set ([+]) or direct s={1,2,3}
Increase Boolean Add (e e); S.add (4)
Delete Boolean remove (Object O); S.remove (4)/s.pop ()
Update No updates No updates

Java Collection-python data structure comparison

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.