I don't understand how Collections is generally used. The confusion started when I found out what binary search was and I looked up an implementation in java. The first I found was this https://www.javatpoint.com/binary-search-in-java, but I also found this on Geeksforgeeks: https://www.geeksforgeeks.org/collections-binarysearch-java-examples/.
They pretty much have the same output and obviously the second is simpler, but I don't really understand what the point is of the first link. To generalize for all of Collections, are there situations where using Collections is disadvantageous?
I'm sorry my question can't be more specific or if the question doesn't make sense, but I don't understand enough to make it more specific.
java.util.Collections is a library class containing utility methods for dealing with Collection types. That is, it has helpful methods which solve common problems or do useful things, so that you don't have to write your own code to do them.
Your first link shows an implementation of the binary search algorithm from scratch, while your second link shows how to use the utility method Collections.binarySearch, which saves writing your own implementation.
The first link may be useful for educational purposes (since students often have to learn about the binary search algorithm), or it may be useful for people who need to adapt binary search to a different problem. For example, a variation of binary search can be used to find the first occurrence of the target number, or the smallest number greater than or equal to the target, but the Collections.binarySearch method cannot do either of these things, so you could have to write an implementation yourself.
The first link you posted actually explains how a binary search works, giving the theory behind it, and how to implement it on your own. This is good to understand how, and why a binary search works.
However, the Java language has a util library for Collections (Maps, Lists, etc) that have some of these simple methods already implemented. The second link explains how to use that library.
Related
I get, that we read from left to right and
ApacheConfigFileHandler implements ConfigFileHandler
sounds better than
ConfigFileHandlerApache implements ConfigFileHandler
when spoken.
But when I search a class, I usually know the Interface I'm looking for and usually dont know the exact implemenation name.
I know that I'm looking for a ConfigFileHandler, but I might not know that the Implemenation name I'm looking for is 'Apache'.
So in terms of code completion, with the suffix approach, I could search for ConfigFileHandler and it would then suggest me all implementations of a ConfigFileHandler, including the AppacheConfigFileHandler.
The advantage of the suffix approach becomes even more clear, when the inheritance hierarchy is bigger:
Let's say I have
FastApacheConfigFileHandler extends ApacheConfigFileHandler
which would be
ConfigFileHandlerApacheFast extends ConfigFileHandlerApache
with the suffix approach.
I know I'm searching for a ConfigFileHandler so I type ConfigFileHandler, then I see Apache and think "yeah, I remember the class I'm searching for is a AppacheConfigFileHandler", so I continue typing until ConfigFileHandlerApache and see all extensions/ implementations of AppacheConfigFileHandler and finally find my FastApacheConfigFileHandler aka ConfigFileHandlerApacheFast.
Is there anything wrong with the implementation name suffix approach (except it does not sound that nice when spoken)?
If yes, what is wrong is it?
If no, why hasn't this approach become the standard?
edit:
Another Point: It makes more sense to me, that, when I'm reading a class name (from left to right), it becomes more specific the further I read the name and does not start with the most detail.
One example for that: When im reading the first word Fast from FastApacheConfigFileHandler, I have no idea what im dealing with, then I'm reading FastApache, still not much of an idea, then im reading FastApacheConfigFileHandler, than I know exactly what im dealing with. Vs when I read ConfigFileHandler first, I kinda know what im dealing with and get more details, the further I read the classname.
The most important reason I can see, is that it completely breaks with both standard Java naming conventions and English language semantics. In doing so, it violates the principle of least astonishment.
Taking this approach to its natural conclusion, class names become completely intractable.
Applying it to the java.util package:
Should you use a ListArray or a ListLinked?
What would a TokenizerString be?
Is a MapTree a tree or a map? Is a TaskTimer a timer or a task?
Is a LoaderService a service? What about an ErrorConfigurationService?
Etc.
I've looked for half an afternoon for something similar to the MSDN for Java. The closest I've found is: http://docs.oracle.com/javase/7/docs/api/index.html. It is a very helpful place, and I've already referenced it many times.
My main is problem is trying to find things(e.g. - arrayName.length()) I keep trying to find that .length() part in the documentation. I've looked under the Objects class, all of the array classes, attributes(there are so many with the same name I probably skipped over a dozen without realizing it), and a few other random classes. With how I learn it makes it difficult when I can't find a simple method, albeit self explanatory, in the official documentation.
When I was learning C# all I had to do was google "keyboard event C#" and the MSDN gave me: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx, then that combined with the next link down was a stackoverflow post, I had all the information I wanted, including samples, parameters, and examples.
How do I find such a simple answer in the Java documentation? I guess I'm asking how to fish, in terms of the documentation.
p.s. - This was all I could find, and it doesn't answer any of the questions I have(like 'what else can I add to the end of an array or string?').
getLength
public static int getLength(Object array)
throws IllegalArgumentException
Returns the length of the specified array object, as an int.
Parameters:
array - the array
Returns:
the length of the array
Throws:
IllegalArgumentException - if the object argument is not an array
Try to set-up JDK javadoc properly in your IDE, if you are using eclipse you can follow this thread : Eclipse Helios not showing Javadoc
Go through basic java tutorial to be confident with basic Java API and for frameworks and various libraries always download javadoc with jar and search for cookbooks / tutorial e.g.
for Apache Commons IO i found quickly : http://commons.apache.org/proper/commons-io/description.html
In javadocs, you can find what all methods and attributes does the particular class have.
Arrays are special objects in java, they have a simple attribute named length which is final.
There is no "class definition" of an array (you can't find it in any .class file), they're a part of the language itself.
I would suggest you to go through the java langauage tutorials to understand these things.
http://docs.oracle.com/javase/tutorial/java/
If you know the object it should be attached to then it's pretty easy to find.
If you don't, there is an index at the top near the right of all methods (if you don't know what object you want).
The most common way is just to use eclipse and hit ctrl-space whenever you get stuck, that becomes second nature and is by far the most useful solution.
Had you said "arrayName.[ctrl-space]" you would have seen a list with "length" as one of the possibilities. (also if you had said "listName.[ctrl-space]" the list would have contained size() instead of length because lists, annoyingly, don't use the same name as arrays for size, hmph.
I need a library/function/method to perform a Fisher's exact test in Java, and provide the right, left and two-tailed probabilities.
Simple Googling shows a solution within the packages of Tassel, but the method inside simply applies the test steps with no optimization, and therefore it's extremely slow. Moreover, it uses int types everywhere and it's not really efficient for big contingency tables.
If you know any already written solution, help me :-)
See if this helps: http://www.users.zetnet.co.uk/hopwood/tools/StatTests.java
The formula is quite simple. There's a very simple (two-tailed) implementation here: http://javanus.com/blogs/?p=51 (see the comment by Discretoboy for a much cleaner implementation)
You can also take a look at the test implementation in Java Statistical Classes.
I use http://wordhoard.northwestern.edu/userman/javadoc/edu/northwestern/at/utils/math/statistics/FishersExactTest.html
A (very) brief test showed it to be similar in speed to the Java Statistical Classes (jsc) test mentioned above but it had the additional advantage of not giving me an illegal argument exception when my table included zero, which I believe is a legitimate case.
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.
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.