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?

4

4 回答 4

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, ...)

于 2013-02-28T11:07:03.607 回答
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

于 2013-02-28T11:09:18.470 回答
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.

于 2013-02-28T11:11:01.473 回答
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).

于 2013-02-28T11:11:16.030 回答