views:

103

answers:

2

The Java Collections Framework is like the C++ Standard Template Library: "a unified architecture for representing and manipulating collections (objects that group multiple elements into a single unit)."

http://java.sun.com/docs/books/tutorial/collections/intro/index.html

+11  A: 

As it turns out, the equivalent to the Java Collections Framework in Python is... Python. All of the core collections featured in the Java Collections Framework are already present in core Python.

Give it a try! Sequences provide lists, queues, stacks, etc. Dictionaries are your hash-tables and maps. Sets are present, etc.

One might consider Python a "higher" language than Java, because it natively provides all of these higher order abstract data types intrinsically. (It also supports Object Oriented, procedural, and functional programming methodologies.)

Travis Bradshaw
+1 Nice answer. Link to sequence types for reference: http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange
Seth
Good answer. But if Python were the _equivalent_ of the Collections Framework, its functionality would be equal to the JCF, and I'm sure it goes beyond it. ;)
jellybean
I concede, you're right. ;) Python is a superset of the collections framework, not an equivalent set.
Travis Bradshaw
+6  A: 

Other than the built-ins you might what to check out collections.

>>> import collections
>>> dir(collections)
['Callable', 'Container', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'Sequence', 'Set', 'Sized', 'ValuesView', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_abcoll', '_iskeyword', '_itemgetter', '_sys', 'defaultdict', 'deque', 'namedtuple']
>>>
monkut
Excellent addition! It's worth mentioning that the datatypes in collections are often "higher level" than the builtins, or sometimes duplicate functionality from the builtin types but with different performance characteristics. (So, for instance, while a `deque` and a `list` (builtin) are largely compatible, for some usage patterns they perform much differently.)
Travis Bradshaw
However, as of 2.6, the built-ins derive from the collections.ABC, so you can depend on a `list` being a proper subclass of `collections.Iterable`.
S.Lott
I wanted a unified arch with guarantees that algorithms can work on various collections implementations, as long as the implementations were of the same "type". The collections module seems to provide that.
saidimu
As S.Lott pointed out, since Python 2.6 the builtin types are every bit a part of the unified architecture that the `collections` module provides. Don't limit yourself to members of the `collections` module only because of a conceptual bias coming from Java.
Travis Bradshaw
Additionally, it's important to note that unlike Java, the Pythonic methodology isn't "Look Before You Leap". You don't necessarily want to be checking types before performing algorithmic actions. Instead, you perform those actions and let the *duck typing* features of the language determine if the actions are successful or not. Also known as "It's Easier To Ask For Forgiveness Than Permission".
Travis Bradshaw