Java - Choosing the right collection [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Rule of thumb for choosing an implementation of a Java Collection?
I am looking for a 'Summary' list of all the Java collections detailing the pros and cons of each. I am particularly interested in things like
Which provide faster iteration
Which provide faster search
Which provide slower iteration
Which provide faster insertion or removal
I have seen some sites by searching on Google but i am looking for just a summary preferable in table format.
Thanks in advance.

Take a look at the Collections tutorial, particularly the section on implementations, which includes (in subsections) a discussion of performance characteristics for the various predefined classes. The collections framework is so large, I think, that it would be difficult to summarize everything in a single table.

Related

Overlap in C++ and Java data structures: stack "top" vs "peek" [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I often use the Stack data structure in both Java and C++. This is a standard data structure, very common in implementing many algorithms.
My question is (and the thing that drives me crazy) why does C++ use "top" as a function-name that returns the top-most element value without removing it, and Java uses "peek" as it's method name?
I know there is no standard for data structures, but hasn't computer science come far enough along that there should be a standard? Or am I just too much of a novice to know about a standard...
Do those of you that are professional programmers write your own data-structure libraries that adhere to a common interface across languages? That seems like the best thing to do, in my mind. I write code in C++, Java, Python, C, Perl, and PHP. I just don't see any other way but to write a custom interface for all of these languages. I like "peek", but is there any standard I should be aiming for?
Writing a custom interface just to make method names the same would be a colossal waste of time. What exactly would be the point? You wouldn't be able to easily copy-and-paste most code between the languages you've mentioned even with such a feature.
Personally, I don't like the name of the STL vector method push_back(). I would prefer if it were just called add(), for one thing it'd be less typing. It never occurred to me that I might change it, however. Doing so would just make my code less portable and less readable for others. Now, I suppose this could be done fairly easily with a pre-processor macro, but even that would be a waste of time in my mind.
No there can't be, won't be, and never will be a standard. Anyway, both names are valid, and if you ask me, top makes more sense. Also, as #mimicocotopus says, it's not like having the same method names would let you copy paste code from one language to another. Also, languages like C++ and Java are very distinct, and support different features. If a standard had to use the lowest common denominator, it couldn't take advantage of all of the features of the language it was implemented in.
Anyway, remember what happened last time we standardized something? Cross browser compatibility and porting C code. It gives me shudders just to think of it.

Java and .NET: Why different sorting algorithms are used by default? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Just wondering why Java and .NET Framework uses different sorting algorithm by default.
In Java Array.Sort() uses Merge Sort algorithm by default and as Wikipedia.com says:
In Java, the Arrays.sort() methods use merge sort or a tuned
quicksort depending on the datatypes and for implementation efficiency
switch to insertion sort when fewer than seven array elements are
being sorted
In .NET Framework Array.Sort/List.Sort() uses Quick Sort as default sorting algorithm (MSDN):
List.Sort() uses Array.Sort, which uses the QuickSort algorithm. This
implementation performs an unstable sort; that is, if two elements are
equal, their order might not be preserved. In contrast, a stable sort
preserves the order of elements that are equal.
By looking at the great "Comparison of algorithms" table we can see that both algorithms has pretty different behaviour from Worst Case and Memory Usage perspectives:
Both Java and .NET are great Frameworks for Enterprise Solutions development, both has platforms for embedded development. So why they are using different sorting algorithm by default, any thoughts?
EDIT:
I see that two persons already voted to close this quesion as not constructive. I believe Java and .NET are most popular development Frameworks so it would be really interesting to find any non trivial and interesting thoughts, perhaps facts!, regarding such decision.
Different development teams in two different companies came to different conclusions regarding the usual use case for their frameworks and components and have decided to implement accordingly.

Persistent data structures in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I need a persistent storage in Java for certain (possibly large) data structures, such as:
dense and sparse matrices of integers, doubles, booleans
directed graphs with labeled nodes and edges
binary trees
maps: string->string, string->integer (with fast retrieval of the keys with largest values)
sets of integers or strings
I don't mind if there is a separate storage for each data structure, as long as all the storages have similar, consistent interfaces.
I need to be able to efficiently modify the data structures "remotely", that is, without fetching the entire structure to RAM, modifying it, and storing back. Example operations: put a key-value in a map, remove a node from a tree, modify a node's label in a graph, add a value to a set.
It would be very nice if these storages could be also easily accessed from other programming languages, most notably Python. I'm thinking of a RESTful service in the backend and client APIs in Java and Python.
Motivation: I need to process a large collection of documents and perform various analyses on them. I want to explore various approaches and create fast prototypes (for that reason I need simple, easy-to-use APIs).
I guess I'm not the first person needing such a functionality and I would hate to reinvent the wheel here. Which brings me to the question: which open-source solutions allow easy-to-use persistence of data structures in Java?
Thanks in advance!
Have you thought about using something like MongoDB? It seem perfect for what your looking for and Its picking up a lot of steam. It's a high-performance, schema-free document-oriented database and I love the fact it's based on json! Check it out!
Here is nice Java tutorial.
How about ObjectOutputStream and ObjectInputStream?
It's very hard to wire all that but you can use JPA with bi-directional mappings and lazy-loading.
It would really depend on you usage patterens, i.e., how many reads, how many writes, how often, etc, but I would suggest you just use sql until you prove that it will not work.

Java Data structure [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java tree data-structure?
Is there any Java data structure implementation similar to tree and graph?
Not in the java.util Collections API.
You can use the DefaultTreeModel from Swing for trees.
Jung is a graph framework.
As I answered for a similar question, the Java API contains no general API for trees/graphs, since there is no unique set of features needed in every usecase. There are quite some tree/graph-like APIs for special cases, though.
And in principle it is easy to make your own graph - one could even say that every object is in fact a node in a graph, with the values of its reference type fields being the (outgoing) neighbors.

java: libraries for immutable functional-style data structures [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
This is very similar to another question (Functional Data Structures in Java) but the answers there are not particularly useful.
I need to use immutable versions of the standard Java collections (e.g. HashMap / TreeMap / ArrayList / LinkedList / HashSet / TreeSet). By "immutable" I mean immutable in the functional sense (e.g. purely functional data structures), where updating operations on the data structure do not change the original data, but instead return a new instance of the same kind of data structure. Also typically new and old instances of the data structure will share immutable data to be efficient in time and space.
From what I can tell my options include:
Functional Java
Scala
Clojure
but I'm not sure whether any of these are particularly appealing to me. I have a few requirements/desirements:
the collections in question should be usable directly in Java (with the appropriate libraries in the classpath). FJ would work for me; I'm not sure if I can use Scala's or Clojure's data structures in Java w/o having to use the compilers/interpreters from those languages and w/o having to write Scala or Clojure code.
Core operations on lists/maps/sets should be possible w/o having to create function objects with confusing syntaxes (FJ looks slightly iffy)
They should be efficient in time and space. I'm looking for a library which ideally has done some performance testing. FJ's TreeMap is based on a red-black tree, not sure how that rates.
Documentation / tutorials should be good enough so someone can get started quickly using the data structures. FJ fails on that front.
Any suggestions?
It seems to me you already know what your options are, you just aren't happy with any of them. Here is my take on the three choices you've provided:
Functional Java - This one seems like the best fit for you. It fits all of your requirements except that you don't like the documentation. From my perspective the documentation looks basic, but serviceable. Their code snippets should get you up and running quickly. The learning curve seems almost non-existent which should help mitigate the lack of documentation. FYI, core Java's TreeMap is based on a Red-Black tree as well.
Scala - This is the choice I would make if I was in your shoes. You seem to not want to learn a new language, but Scala is a very easy transition from Java. You can write very java-like code at first, and slowly adopt more functional idioms. The Java <-> Scala interop is excellent in both directions as well.
Clojure - As much as I love Clojure, its tough to recommend in this particular instance due to the radically different syntax and steep learning curve for a java developer.
Perhaps Google's guava-libraries may be of some use:
https://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained
Scala
You can call the methods of either language from methods in the other one
http://www.scala-lang.org/faq/4
I've spent some time making the Clojure persistent/immutable data-structures work in Java, with sensible constructors and generics as part of Pure4J.
This also includes #ImmutableValue class for ensuring that not only are the collections immutable, but the values you put in them are immutable too.
Hope this helps.

Categories

Resources