I am trying to find an overview of all methods in the java.util package returning backed Collections (and Maps). The only ones easy to find are the synchronizedXX and immutableXX. But there are others like subMap(). Is there a more comfortable way to find out more about all util methods returning backed collections than to actually read the docs? A visual overview maybe?
the tutorial for wrapped classes (has been proposed twice as an answer) at http://download.oracle.com/javase/tutorial/collections/implementations/wrapper.html is oblivious of the NavigableSet/Map interfaces and therefore does not provide an overview of methods returning backed Collections
I know this doesn't exactly answer your question (and I risk being down-voted), but I will try anyway.
You should try to study the collections API as much as you can, in general it is good advice for any programming language/platform to invest some time, and learn the basics.
When studying Java collections you will also notice some oddities in the design, and will also realize that there are many things that are not provided that you either have to build your own or get them from somewhere else (such as Apache commons).
In any case, using a modern IDE (such as IntelliJ IDEA or Eclipse) will make things a lot easier on you. Both have ways of searching for symbols with a few keystrokes and also let you navigate the collections API (and any source code you throw at them) making it a lot easier to figure out what is available and how you might take advantage of it.
Try this mnemonic to understand some methods from TreeSet and TreeMap.
It's a bit tricky though there's a numeric TreeSet (1 2 3 4 5 6 7 8 9 10) below. So it's easy to remember that headSet() & headMap() methods work with the "Head" of the collection.
Also the mnemonic describes that there are two cases of using headSet with different results:
headSet(element)
headSet(element, inclusive).
The tutorial has a page on wrapper classes.
Related
What was the driving factor or design plan in making the methods of HashTable synchronized?
This link says that HashTable is synchronized because its methods are synchronized. But, I want to know the reason "why" the methods were synchronized?
Was it just to provide some synchronization feature? A developer could explicitly handle a race condition through synchronization techniques. Why provide HashTable with this feature?
Keep in mind: these classes were created "ages" ago - when you check the javadoc for Hashtable, you find it says "since Java 1.0"; whereas HashMap says "1.2"!
Back then, Java was trying to compete with languages like C and C++; by providing unique selling points such as "built-in concurrency".
But people quickly figured that one better synchronizes containers when using them in multi-threaded environments!
So my (more of an opinion-based) answer is: at the time when this class was first designed, people assumed that the requirement "can be used by multiple threads" was more important than "gives optimal performance".
Because Java was "advertised" like: "use it to write multi-threaded write once run everywhere code". That approach fails quickly when the default container classes given to people need additional outside wrapping to actually make them "multi-threaded" ready.
During the years, the people behind Java started to understand that "more granular" solutions are required. Therefore the core collection classes are not synchronized to avoid the corresponding performance hits. Meaning: the default with collections is to go "unprotected"; so you have to put in some thoughts when your requirements is that "multi-threaded" correctness.
Same for "lists" btw: Vector is synchronized; ArrayList is not.
We cannot tell you why. Those who designed Java over two decades ago maybe can. It's not a useful question. Assuming you actually wanted to ask about java.util.Hashtable and not the fictional HashTable type, bear in mind that it's been obsolescent for nineteen years. Nineteen years! Don't use it. It (and Vector) have cruft that the replacement types, both synchronized and unsynchronized, do not carry. Use the modern (as of nineteen years ago) types.
I was frustrated recently in this question where OP wanted to change the format of the output depending on a feature of the number being formatted.
The natural mechanism would be to construct the format dynamically but because PrintStream.format takes a String instead of a CharSequence the construction must end in the construction of a String.
It would have been so much more natural and efficient to build a class that implemented CharSequence that provided the dynamic format on the fly without having to create yet another String.
This seems to be a common theme in the Java libraries where the default seems to be to require a String even though immutability is not a requirement. I am aware that keys in Maps and Sets should generally be immutable for obvious reasons but as far as I can see String is used far too often where a CharSequence would suffice.
There are a few reasons.
In a lot of cases, immutability is a functional requirement. For example, you've identified that a lot of collections / collection types will "break" if an element or key is mutated.
In a lot of cases, immutability is a security requirement. For instance, in an environment where you are running untrusted code in a sandbox, any case where untrusted code could pass a StringBuilder instead of a String to trusted code is a potential security problem1.
In a lot of cases, the reason is backwards compatibility. The CharSequence interface was introduced in Java 1.4. Java APIs that predate Java 1.4 do not use it. Furthermore, changing an preexisting method that uses String to use CharSequence risks binary compatibility issues; i.e. it could prevent old Java code from running on a newer JVM.
In the remainder it could simply be - "too much work, too little time". Making changes to existing standard APIs involves a lot of effort to make sure that the change is going to be acceptable to everyone (e.g. checking for the above), and convincing everyone that it will all be OK. Work has to be prioritized.
So while you find this frustrating, it is unavoidable.
1 - This would leave the Java API designer with an awkward choice. Does he/she write the API to make (expensive) defensive copies whenever it is passed a mutable "string", and possibly change the semantics of the API (from the user's perspective!). Or does he/she label the API as "unsafe for untrusted code" ... and hope that developers notice / understand?
Of course, when you are designing your own APIs for your own reasons, you can make the call that security is not an issue. The Java API designers are not in that position. They need to design APIs that work for everyone. Using String is the simplest / least risky solution.
See http://docs.oracle.com/javase/6/docs/api/java/lang/CharSequence.html
Do you notice the part that explains that it has been around since 1.4? Previously all the API methods used String (which has been around since 1.0)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have had a quick scan of the Guava API and the new collection types it provides(Multimap and BiMap for example appear useful) and I am thinking of including the library in the project(s) I work on.
However, I also have a reticence to include libraries willy-nilly if they are of no great benefit and learning the features wastes valuable time.
Have you included the Guava library in your project and has it proved useful in any unexpected way? Would you always use it in the future? What has been its main benefit/time saver? What are its hidden features?
Seriously, everything in Guava is useful. I've been using it for quite a while, and am still always discovering something new I can do with it that takes less code than doing it by hand.
Some things others have not really mentioned that I love:
Multimaps are just great. Any time you would use something like Map<Foo, Collection<Bar>>, use a multimap instead and save yourself a ton of tedious checking for an existing collection mapped to a key and creating and adding it if it isn't there.
Ordering is great for building Comparators that behave just how you want.
Maps.uniqueIndex and Multimaps.index: these methods take an Iterable and a Function and build an ImmutableMap or ImmutableListMultimap that indexes the values in the Iterable by the result of applying the function to each. So with a function that retrieves the ID of an item, you can index a list of items by their ID in one line.
The functional stuff it provides... filter, transform, etc. Despite the verbosity of using classes for Functions and Predicates, I've found this useful. I give an example of one way to make this read nicely here.
ComparisonChain is a small, easily overlooked class that's useful when you want to write a comparison method that compares multiple values in succession and should return when the first difference is found. It removes all the tedium of that, making it just a few lines of chained method calls.
Objects.equal(Object,Object) - null safe equals.
Objects.hashCode(Object...) - easy way to get a hash code based on multiple fields of your class.
Objects.firstNonNull(Object,Object) - reduces the code for getting a default value if the first value is null, especially if the first value is the result of a method call (you'd have to assign it to a variable before doing this the normal way).
CharMatchers were already mentioned, but they're very powerful.
Throwables lets you do some nice things with throwables, such as Throwables.propagate which rethrows a throwable if it's a RuntimeException or an Error and wraps it in a RuntimeException and throws that otherwise.
I could certainly go on, but I have to get to work. =) Anyway, despite my having listed some things I like here, the fact is that everything in Guava is useful in some situation or another. Much of it is useful very often. As you use it, you'll discover more uses. Not using it will feel a bit like having one hand tied behind your back.
I've been effectively using Guava for a couple of years, inside Google - and it's wonderful.
The parts I'm particularly fond of are:
Charsets.* - so simple, so useful
Collections
IO handling (read a resource completely in a single line, etc)
Splitter/Joiner
Preconditions
I initially used it for collections shorthands. For example, instead of:
Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long, List<String>>>();
you can do this:
Map<String, Map<Long, List<String>>> map = Maps.newHashMap();
It's also easy to populate maps:
ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");
Now, I have discovered some other useful utilities present in Guava. For example, the CharMatcher class allows you to match sequences of characters. You can do:
CharMatcher.inRange('a','z').or(inRange('A','Z'));
or
String phoneNumber = CharMatcher.DIGIT.retainFrom("my phone number is 123456789");
CharMatcher's precomputed() method (source) is a nice "hidden feature" I stumbled upon the other day.
It's really just an optimization, that creates a lookup table (using a bit array), and then simply looks up characters to see if they "match".
It's the kind of hidden optimization you can leverage when you use a library, that you might not have thought of yourself in your own code.
Of course,
if you create a complex CharMatcher, which you plan to use many times, you must remember to call the precomputed() method, like:
CharMatcher complexMatcher = CharMatcher.anyOf("cat")
.or(CharMatcher.DIGIT)
.or(CharMatcher.WHITESPACE)
.precomputed();
Here's a YouTube video from Google (speaker: Kevin Bourrillion, lead engineer for Google's core Java libraries) which shows the beauty of Google Collections. One thing Google did, which I believe is brilliant, is guarantee Immutability in collections.
Google Guava is a utility library, so I doubt that there is a killer class inside it. The whole things about utility is you almost use that in every projects you have. I can't remember any project I've done that doesn't use Java collection. And truth is, the collection utility of Google Guava is wonderful and should be in the Java SDK itself.
I've written three articles about classes on Google Guava:
Using CheckedFuture: http://blog.firdau.si/2010/07/07/guava-using-checkedfuture/
Using ListenableFuture: http://blog.firdau.si/2010/07/05/guava-using-listenablefuture/
ComputingMap on Google Collection (now Guava) http://blog.firdau.si/2009/11/13/computing-map-on-google-collections/
And this is not all, there are many other things you can do with Guava.
Absolutely very super useful. It's almost invariably the first library added to a new project.
We're very fond of Iterators/Iterables and the Function interface.
The Service family of interfaces are great abstractions
We're so committed we've started to use the ImmutableXXX classes our API types to communicate that it can't be changed.
Computing maps (from MapMaker) are wonderful in certain situations.
Overall, the library is very high quality. The API is well thought out, the implementation solid. Highly recommended.
MapMaker now offers bounded LRU caches - that's some substantial machinery hidden behind a tiny API. This has the potential for huge utility, and I'm still all over the code.
Can anyone give me references of a web site containing a summary of the main Java data structures, and their respective complexity in time (for some given operations like add, find, remove), e.g. Hashtables are O(1) for finding, while LinkedLists are O(n). Some details like memory usage would be nice too.
This would be really helpful for thinking in data structures for algorithms.
Is there a reason to think that Java's implementation is different (in terms of complexity) than a generic, language agnostic implementation? In other words, why not just refer to a general reference on the complexity of various data structures:
NIST Dictionary of Algorithms and Data Structures
But, if you insist on Java-specific:
Java standard data structures Big O notation
Java Collections cheatsheet V2 (dead link, but this is the first version of the cheatsheet)
The most comprehensive Java Collections overview is here
http://en.wikiversity.org/wiki/Java_Collections_Overview
I found very useful The Collections Framework page, expecially the Outline of the Collections Framework, where every interface/class is breeefly described. Unfortunately there's no big-O information.
I couldn't see this particular resource mentioned here, i've found it of great use in the past. Know Thy Complexities!
http://bigocheatsheet.com/
Time and space complexities for the main collection classes should correspond to data structures known time complexity. I don't think there's anything Java specific about it, e.g. (as you say) hash lookup should be O(1). You could look here or here.
I don't believe there is any single website outlining this (sounds like a good idea for a project though). I think part of the problem is that an understanding in how each of the algorithms runs is very important. For the most part, it sounds like you understand Big-O, so I would use that as your best guesses. Follow it up with some benchmarking/profiling to see what runs faster/slower.
And, yes, the Java docs should have much of this information in java.util.
Besides the dynamic nature of Python (and the syntax), what are some of the major features of the Python language that Java doesn't have, and vice versa?
List comprehensions. I often find myself filtering/mapping lists, and being able to say [line.replace("spam","eggs") for line in open("somefile.txt") if line.startswith("nee")] is really nice.
Functions are first class objects. They can be passed as parameters to other functions, defined inside other function, and have lexical scope. This makes it really easy to say things like people.sort(key=lambda p: p.age) and thus sort a bunch of people on their age without having to define a custom comparator class or something equally verbose.
Everything is an object. Java has basic types which aren't objects, which is why many classes in the standard library define 9 different versions of functions (for boolean, byte, char, double, float, int, long, Object, short). Array.sort is a good example. Autoboxing helps, although it makes things awkward when something turns out to be null.
Properties. Python lets you create classes with read-only fields, lazily-generated fields, as well as fields which are checked upon assignment to make sure they're never 0 or null or whatever you want to guard against, etc.'
Default and keyword arguments. In Java if you want a constructor that can take up to 5 optional arguments, you must define 6 different versions of that constructor. And there's no way at all to say Student(name="Eli", age=25)
Functions can only return 1 thing. In Python you have tuple assignment, so you can say spam, eggs = nee() but in Java you'd need to either resort to mutable out parameters or have a custom class with 2 fields and then have two additional lines of code to extract those fields.
Built-in syntax for lists and dictionaries.
Operator Overloading.
Generally better designed libraries. For example, to parse an XML document in Java, you say
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("test.xml");
and in Python you say
doc = parse("test.xml")
Anyway, I could go on and on with further examples, but Python is just overall a much more flexible and expressive language. It's also dynamically typed, which I really like, but which comes with some disadvantages.
Java has much better performance than Python and has way better tool support. Sometimes those things matter a lot and Java is the better language than Python for a task; I continue to use Java for some new projects despite liking Python a lot more. But as a language I think Python is superior for most things I find myself needing to accomplish.
I think this pair of articles by Philip J. Eby does a great job discussing the differences between the two languages (mostly about philosophy/mentality rather than specific language features).
Python is Not Java
Java is Not Python, either
One key difference in Python is significant whitespace. This puts a lot of people off - me too for a long time - but once you get going it seems natural and makes much more sense than ;s everywhere.
From a personal perspective, Python has the following benefits over Java:
No Checked Exceptions
Optional Arguments
Much less boilerplate and less verbose generally
Other than those, this page on the Python Wiki is a good place to look with lots of links to interesting articles.
With Jython you can have both. It's only at Python 2.2, but still very useful if you need an embedded interpreter that has access to the Java runtime.
Apart from what Eli Courtwright said:
I find iterators in Python more concise. You can use for i in something, and it works with pretty much everything. Yeah, Java has gotten better since 1.5, but for example you can iterate through a string in python with this same construct.
Introspection: In python you can get at runtime information about an object or a module about its symbols, methods, or even its docstrings. You can also instantiate them dynamically. Java has some of this, but usually in Java it takes half a page of code to get an instance of a class, whereas in Python it is about 3 lines. And as far as I know the docstrings thing is not available in Java