0

In Python, you can get pretty far if you know about the standard 'list', 'tuple', 'set' and 'dictionary'. These are the basic data structures any decent Python programmer should know about.

What are the Java equivalents to these data structures, and are there other data structures worth noting?

phaz
  • 872
  • 9
  • 23
  • In Java, there is `Set`, `List` and `Map` interfaces (you have to choose which implementation you want to go with). No equivalence of `tuple`, unfortunately. – nhahtdh Feb 28 '13 at 11:06
  • 1
    check the java collections framework http://docs.oracle.com/javase/6/docs/technotes/guides/collections/index.html – A4L Feb 28 '13 at 11:07
  • @nhahtdh How would you expect the equivalent of `tuple` to work, other than an unmodifiable `List`? – arne.b Feb 28 '13 at 11:09
  • 1
    This is not a serious question. Don't be lazy, this is googlable in 5 seconds. – Andrey Feb 28 '13 at 11:11
  • @arne.b: You can "simulate" Tuple by writing a generic `Pair` and reuse it for higher dimension tuple. It is very ugly, though. I'd go with class if the tuple is going to reappear many times. – nhahtdh Feb 28 '13 at 11:21

4 Answers4

1

Sadly there is no native Tuple in Java, most of the time you go with:

  • List: ArrayList (is a List and a List is a Collection)
  • Set: HashSet (is a Set and a Set is a Collection)
  • Dictionary: HashMap (is a Map, but a Map is not a Collection)

Have a look at Java Collections in general: http://docs.oracle.com/javase/tutorial/collections/index.html For concurrency: http://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html

There are a lot of external libraries (Guava, Apache, ...)

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
0

You can do a lot with LinkedList, HashMap, HashSet, and we have a dictionary http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Dictionary.html too.

The best way to learn any new language is to read code. GitHub hosts a ton of open source Java applications. You can check on the latest here: https://github.com/languages/Java

Gardner Bickford
  • 1,934
  • 1
  • 17
  • 25
  • 1
    That Dictionary class is obsolete – Christophe Roussy Feb 28 '13 at 11:11
  • 1
    Thanks Christophe. It looks like it was dropped as of 1.6. The latest search results point to [Hashtable](http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html) in its place. As others have mentioned, learning about [generics](http://docs.oracle.com/javase/tutorial/java/generics/) is a great idea. – Gardner Bickford Feb 28 '13 at 11:16
0

Does this help?

Java has all its data structures, including set, list, HashMap, defined in the Java Collections Framework. A HashMap is essentially equivalent to a dictionary.

As for a tuple equivalent, look into unmodifiableList.

Community
  • 1
  • 1
Barney
  • 2,355
  • 3
  • 22
  • 37
0

In Java he have a one-size-fits-all tool: a class. For most problems you solve in Python with a tuple or a dictionary, in Java you write a custom class, with instance variables, constructors, getters, and setters. So when in Java, be prepared to write some boilerplate.

As far as data structures, the default ones are ArrayList, HashSet, and HashMap. There are sorted versions: TreeSet and TreeMap—when you need them, they are very useful.

When you need close-to-the-metal performance, you'll resort to arrays.

In Java you also enjoy the built-in concurrency, and then it gets complicated: there are performant options like ConcurrentHashSet/Map and also synchronized wrappers around plain collections: Collections/synchronizedSet/Map/List (these are methods that return a wrapping object).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436