Java Collection-Python Data Structure comparison, java-python
Comparison between Java list and Python list
Java List: ordered, repeatable. (Order refers to the order in which objects in the set are added in the same order)
Python list is ordered and variable.
Java List category:
--- ArrayList: array is used at the underlying layer. The thread is not secure, the query speed is fast, and the addition and deletion speed is slow.
An exception occurs when adding or deleting a set object during iteration.
--- Slow list: the linked list is used at the underlying layer. The thread is not secure, the query speed is slow, and the addition and deletion speed is fast.
Later-in-first-out, similar to stack
--- Vector: array is used at the underlying layer, thread security, fast search speed, slow addition and deletion speed, and replaced by ArrayList
The Python list is unclassified, and the list is the basic data structure of Python.
| Method |
Java List |
Python list: [1, '2', 3] |
Python tuple :( 1, 2, 3) |
| Add to end |
Boolean add (E e ); |
List. append ('adhim ') |
Immutable |
| Add to specified location |
Void add (int index, E element ); |
List. insert (2, 'taobao ') |
Immutable |
| Length |
Int size (); |
Len (list) |
Same as list |
| Update |
E set (int index, E element ); |
List [I] = 'ahim' |
Immutable |
| Delete |
E remove (int index ); |
List. pop (I) |
Immutable |
| Delete all |
Void clear (); |
List. clear () |
Unchangeable. You can use del to delete tuples. |
| Search |
E get (int index ); |
List [I] |
Same as list |
Comparison between Java Map and Python dict
Java Map is a Collection, but not part of the Collection system. It is unordered and cannot be repeated. It exists as a key-value pair.
The full name of Python dict is dictionary, which is also called map in other languages. It is stored with key-value (key-value) and has extremely fast search speed. The key of dict must beImmutable object, because dict uses the hash algorithm to calculate the storage location of value based on the key.
Java Map classification:
--- HashMap: the underlying data structure is a hash table.
The uniqueness of the key is the same as that of the HashSet.
--- TreeMap: the underlying data structure is a binary tree.
The uniqueness of the key is the same as that of the TreeSet.
The Python dictionary is unclassified, and dict is the basic data structure of Python.
| Method |
Java Map |
Python dict: {'1': '1', '2': '2 '} |
| Add |
V put (K key, V value ); |
Dict ['1'] ='' |
| Delete |
V remove (Object key ); |
Del dict ['1'] |
| Change |
Same as adding. If the key is the same, the original value will be overwritten. |
Dict ['1'] = 'King' |
| Search |
V get (Object key ); |
Dict ['1'] |
Comparison between Java Set and Python set
Java Set uses the Java Map key at the underlying layer and the value is Set to null. Therefore, the principle of uniqueness of Set and Map is the same.
Similar to dict, Python set is also a set of keys, but does not store values. Because keys cannot be repeated, there are no duplicate keys in the set.
Java Set: unordered and repeatable
--- HashSet: The underlying hash table is used, and the thread is not secure.
The only way to ensure the Object is to rewrite hashcode () and equals (Object obj). The hash algorithm is used to cause disorder.
--- TreeSet: Binary Tree is used at the underlying layer, and the thread is not secure.
When you add an object using the add method, the object added to the set isSort
The only way to ensure the object is: 1. Implement the Comparable <E> interface. If the return value of the compareTo () method is 0, it cannot be added. 2. Create a class, implement Comparator <T>, and implement the compare () method.
The Python set has no classification. set is the basic data structure of Python.
| Method |
Java Set |
Python set |
| Create |
|
Input a list: set ([1, 2, 3]) or directly s = {1, 2, 3} |
| Add |
Boolean add (E e ); |
S. add (4) |
| Delete |
Boolean remove (Object o ); |
S. remove (4)/s. pop () |
| Update |
Not updated |
Not updated |