Chapter 18: Java Collections

Example 18.1, page no. 348

In [9]:
"""
Note: there is no such class in Python. We will perform normal operations on array
"""

arrayList = []
print "Initial size of arrayList: ", len(arrayList)
arrayList.append("A")
arrayList.append("B")
arrayList.append("C")
arrayList.append("D")
arrayList.insert(2, "E")
print "Changed contents of arraylist by adding element at the given index: ", arrayList
arrayList.pop(3)
arrayList.remove("A")
print "Changed contents of arraylist by removing element from the list: ", arrayList
Initial size of arrayList:  0
Changed contents of arraylist by adding element at the given index:  ['A', 'B', 'E', 'C', 'D']
Changed contents of arraylist by removing element from the list:  ['B', 'E', 'D']

Example 18.2, page no. 349

In [15]:
"""
Note: There is no such collection in Python. We will dqueue instead, which is very much similar
"""

from collections import deque

d = deque()

class MyStack():
    def push1(self, o):
        d.appendleft(o)
    def push2(self, o):
        d.append(o)
    def bottom(self):
        return d.pop()
    def pop(self):
        return d.popleft()

class Car():
    car1 = "Benz"
    car2 = "Toyoto"
    car3 = "Qualis"
    car4 = "Santro"

class Bird():
    bird1 = "parrot"
    bird2 = "duck"
    bird3 = "raven"

myCar = Car()
myBird = Bird()
s = MyStack()
s.push1(myCar.car1)
s.push2(myBird.bird3)
myCar = s.pop()
print "The first element in the list: ", myCar
print "The last element in the list: ", myBird.bird3
The first element in the list:  Benz
The last element in the list:  raven

Example 18.3, page no. 351

In [23]:
"""
Note: there is no collection hash set in Python. We will solve it in different manner
"""

hs = set()

hs.add("D")
hs.add("A")
hs.add("C")
hs.add("B")
hs.add("E")

#answer will differ due to difference in built-in techniques
print "The elements available in the hash set are: ", hs
The elements available in the hash set are:  set(['A', 'C', 'B', 'E', 'D'])
In [25]:
#There is no TreeSet or similar collection in Python (can be done using normal list like 18.1) so skipping example 18.4

Example 18.5, page no. 353

In [26]:
"""
Note: there is no vector in Python. Doing using normal lists.
"""

fruits = []

fruits.append("Apple")
fruits.append("Orange")
fruits.append("Grapes")
fruits.append("Pine")

for ele in fruits:
    print ele
Apple
Orange
Grapes
Pine

Example 18.6, page no. 354

In [29]:
"""
In python we can use list itself as stack
"""

st = []

st.append("Java")
st.append("latest")
st.append("Edition")
st.append("-fifth")

print "The elements in the Stack: ", st
print "The element at the top: ", st[-1:]
print "The element poped out of the stack: ", st.pop()
print "The element in a stack after pop out element: ", st
try:
    "The result of searching: ", st.index("r u")
except ValueError:
    print "The result of searching: ", -1
The elements in the Stack:  ['Java', 'latest', 'Edition', '-fifth']
The element at the top:  ['-fifth']
The element poped out of the stack:  -fifth
The element in a stack after pop out element:  ['Java', 'latest', 'Edition']
The result of searching:  -1

Example 18.7, page no. 355

In [38]:
"""
Note: we will use dictionaries
"""

ht ={}

ht['item1'] = "Apple"
ht['item2'] = "Orange"
ht['item3'] = "Grapes"
ht['item4'] = "Pine"

for key in ht.iterkeys():
    print ht[key]
    
Orange
Grapes
Apple
Pine

Example 18.8, page no. 357

In [2]:
#Answers will differ due to difference in built-in techniques of the classes

from random import shuffle

l = []

l.append("java")
l.append("is")
l.append("platform")
l.append("independent")

l_r = list(reversed(l))
l_r = sorted(l_r)

print "List sorted in reverse order is: "
for ele in l_r:
    print ele,

print "\nList shuffled: "
shuffle(l_r)
for ele in l_r:
    print ele,

print "\nMinimum: ", min(l_r)
print "Maximum: ", max(l_r)
List sorted in reverse order is: 
independent is java platform 
List shuffled: 
java independent is platform 
Minimum:  independent
Maximum:  platform