[Python core] – Collections framework in Python for Java learners

Many people are amazed by the fact that Python is way too simple than other languages. If you have already had knowledge of another language, I would be even easier than eating a piece of cake.

In this post, I’ll discuss Collections Framework in Python for those who have already had a basic Java basement.

COLLECTIONS FRAMEWORK IN JAVA

The Collections Framework consist of a “well-designed set of interfaces and classes” for the purpose of working with groups of objects (Zukowski 2011). The diagram below illustrates the framework interface hierarchy.

Figure 1. Java Collections Framework (Lewis 2019)


In the diagram, List and Set are two interfaces that extend Collection class. Many people might think that Map also extends Collection class, but it does not. In fact, Map is just a collection of pairs (Zukowski 2011). The popular feature of Map is to provide values via respective keys, which is far more different from Collection. Map interface has two implementations, namely HashMap – Map implementation with Hash Tables (Lewis 2019) and TreeMap – Map Implementation with SortedMap sub-interface of Map. Thanks to SortedMap sub-interface, TreeMap can give keys in sorted order (figure 2).

Figure 2. TreeMap

Meanwhile, Set is a sub-interface extended from Collection interface. It is a collection that has no duplicates, so adding a duplicate causes no effect on a Set (Trent). Sets have two implementations, namely HashSet and TreeSet. Similar to HashMap, HashSet is based on a hash table and has better performance for search or access (Lewis 2019). TreeSet, on the other hand, is ordered and based on a TreeMap (Lewis 2019).

List is another interface branch of Collection. Lists are ordered, which mean inserted elements are kept in a sequence. Also, duplication is allowed in this type of data structure. Lists can be implemented by either ArrayList class or LinkedList class. While ArrayList is a List implemented with an array and supports fast random element access, LinkedList is a List implemented by a sequence of linked nodes and supports speedy insertion or removal. However, the biggest drawback of ArrayList and LinkedList are slow insertion/removal and slow random element access respectively (Lewis 2019).

A COMPARISON WITH PYTHON

Collections in JavaCollections in Python
Lists• Lists in Python have full manipulations like Java’s (Python 2019) and more. Elements in a Python list are put in a pair of Bracket []. For example:
- list.append(x): to add item
- list.insert(i, x): to insert item x at position i
- list.remove(x): to remove first item in list that has value equals to x.
- list.pop([i]): to remove the item at the given position i in the list. In case no index specified, it will remove and return the last item in the list.
- …
• Unlike Java, Lists in python can contain different data type of object in it. For example, a list in Python can contain Integer, Double, String, or even another List at the same time. For example:
In [1]: list_A = [1, 5.0, "Jayden Tran", [1,2,3]]

In [2]: list_A

Out[2]: [1, 5.0, 'Jayden Tran', [1, 2, 3]]

• Lists in Python also have sorting method.
- list.sort(key=None, reverse=False): the arguments are used to customisation.
• Lists in Python can also be used to implement Stack and Queue
• Especially, Python has List Comprehension which is utmost convenient in creating a new list. For example: Creating a list with only even number from 1 to 10.
In [3]: list_B = [i for i in range(1,11) if i % 2 == 0]

In [4]: list_B
Out[4]: [2, 4, 6, 8, 10]
Sets• Similar to Set in Java, Sets in Python are unordered Collection with no duplicate elements (Python 2019). Elements in a Python set are put in a part of curly brackets {}. Elements also do not need to be in the same data type. For example:
In [19]: set_A = {'Application', 0.5, 8}

In [20]: set_A
Out[20]: {0.5, 8, 'Application'}

• Unlike Java, Sets in Python are not classified as Hash Set and Tree Set.
• Beside the basic methods such as add(), remove(), discard(), Sets in Python also have typical operations for Sets such as intersection(), union()
Sets in Python also have Set Comprehension.
Maps• Maps in Java can be referred as Dictionaries in Python. They can be thought of as a set of key: value pairs. The keys must be unique in one dictionary. For example:
In [21]: dict_A = {'jack':17, 'jane': 20, 'Tom':19}

In [22]: dict_A['kim'] = 50

In [23]: dict_A
Out[23]: {'jack': 17, 'jane': 20, 'Tom': 19, 'kim': 50}

• A pair in dictionary can be deleted by del() method
In [24]: del dict_A['jane']

In [25]: dict_A
Out[25]: {'jack': 17, 'Tom': 19, 'kim': 50}

Dictionaries can be sorted by key or value.

Okay, that’s it. Hope you now have a better understanding of Collections in Python and ready to immerse yourself in it.

Happy coding.

 

Leave a Reply

Your email address will not be published. Required fields are marked *