What is the equivalent of TreeBidiMap in C#? - java

What is equivalent to [TreeBidiMap][1] in c# from Commons Collections in Java?

BidiMap is an interface which defines map which allows mapping from key to value as well as from value to key (thus "bidirectional"). TreeBidiMap is the implementation of this interfaces which uses red-black tree.
None of this classes exist in .NET BCL per se, but I think those can be composed out of simpler primitives. Specifically, C5 Generic Collection Library provides an implementation of a red-black tree, and bidirectional map can be implemented on top of a standard Dictionary<TKey, TValue>.

Related

java: difference between map and hash table

I don't understand this code Map<E, Integer> d = new HashTable<E, Integer>(list.size()); : we create a new object but is it a map or a hashtable? what is the difference between the both of them? I thought that a map is just a way to put 2 element together like a key and its value (for exemple {3; Detroit})
is it a map or a hashtable?
Yes.
The static, compile time type of the reference is Map. As others have already pointed out, it's an interface. You can call all the methods on the Map interface and know that they'll obey the contract and behave as describe.
The dynamic, run time type of the object reference refers to is Hashtable. It implements all the methods in the Map interface in its own way.
The key idea is that the compile time type of a reference is separate from the run time type of the object on the heap that it points to.
Hashtable is a JDK 1.0 class that sticks around for compatibility reasons. It's been retrofitted to implement the Map interface, which was introduced later. You'd be well advised to choose another implementation, such as HashMap, depending on your requirements.
The last part of Hashtable contains the reason why it should not be used:
As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
This means that it is less efficient than HashMap for single-thread models and less efficient than ConcurrentHashMap for multi-threaded models.
Understanding how compile and run time types differ is crucial to understanding how object oriented polymorphism works. This is true for all OO languages: C++, Java, .NET, Python, etc.
Map is an interface. Hashtable is one of the classes that implements the Map interface.
See the Java Doc for the Map interface. Specifically the section that says all known implementing classes.
Any class that implements a Map provides a key->value data-structure. A Map being an interface defines the contract that all implementing classes must adhere to. By itself, a Map cannot be instantiated.
Note that while Hashtable should ideally have been named as HashTable following the java naming conventions, this is is a pre-historic class in Java which exists even before the standard java naming conventions came into existence. Therefore, it is still called Hashtable and not HashTable as wrongly mentioned in your question.
Map is an interface. HashTable is one implementation of that interface. There are several others, such as HashMap, SortedMap, etc. The interface defines the programming API; the implementation defines how that API is implemented. Different implementations may have different runtime performance characteristics.
With regard to interfaces vs. implementations, you may find my answer to Java - HashMap vs Map objects here helpful.

Why java Dictionary interface become obsolete and was replaced by Map? [duplicate]

What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?
In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)
The Dictionary class has been marked as obsolete, so it's best not to use it.
This Stack Overflow post does a good job explaining the key differences:
Java hashmap vs hashtable
Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".
The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.
You are advised to use the HashMap though.
Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.
Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.
There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.

What kind of tree is used in Java's TreeSet and TreeMap?

Are they AVL trees, red-black trees, or something else?
Red-black trees as described in the first line of the javadoc.
Tree Map
Tree Set
From the java.util.TreeMap<K,V> documentation:
A Red-Black tree based NavigableMap implementation.
For questions like these, you should always first consult the documentation. The API shouldn't describe ALL of the inner-workings of a class, but elementary informations such as general data structures and algorithms used are usually documented.
Other Java Collections Framework trivias
These are all little trivias that are also clearly documented:
TreeSet is implemented with a TreeMap
HashSet is implemented with a HashMap
Collections.sort uses modified mergesort
Map<K,V> is not a Collection<?>
ArrayList doesn't specify exact growth policy (unlike, say, Vector)
Related questions
Why does java.util.Arrays.sort(Object[]) use 2 kinds of sorting algorithms?
Why does the Java Collections Framework offer two different ways to sort?
Why doesn't Java Map extends Collection?
The first sentence of the TreeMap Javadoc states:
A Red-Black tree based NavigableMap implementation.
It is a red-black tree in the Oracle desktop Java implementation, but an AVL-tree in Android.
TreeSet is based on TreeMap.
And they uses red-black tree, red-black tree is a kind of AVL.

Java equivalent of C++ std::map?

I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree):
O(log n) performance for insertion/removal/search
Each element is composed of a unique key and a mapped value
Keys follow a strict weak ordering
I'm looking for implementations with open source or design documents; I'll probably end up rolling my own support for primitive keys/values.
This question's style is similar to: Java equivalent of std::deque, whose answer was "ArrayDeque from Primitive Collections for Java".
ConcurrentSkipListMap is a sorted map backed by a skip list (a self-balancing tree-like structure with O(log n) performance). Generally the bounds on CSLM are tighter than TreeMap (which is a self-balancing red-black tree impl) so it will probably perform better, with the side benefit of being thread-safe and concurrent, which TreeMap is not. CSLM was added in JDK 1.6.
Trove has a set of collections for primitive types and some other interesting variants of the common Java collection types.
Other collection libraries of interest include the Google Collection library and Apache Commons Collections.
The closest class to a binary tree in the standard Java libraries is java.util.TreeMap but it doesn't support primitive types, except by boxing (i.e. int is wrapped as an Integer, double as a Double, etc).
java.util.HashMap is likely to give better performance for large maps. Theoretically it is O(1) but its precise performance characteristics depend on the hash code generation algorithm(s) for the key class(es).
According to Introduction to Collections: "Arrays ... are the only collection that supports storing primitive data types."
You can take a look at commons-collections FastTreeMap as well.
I doubt you will find many collections that support primitive types without boxing, so just live with it. And that is not necessarily needed, because of autoboxing.
If you really want to use primitive (after making benchmarks that show insufficient performance!), you can see the source of the FastTreeMap and add methods for handling primitives.

Difference between a HashMap and a dictionary ADT

What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?
In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)
The Dictionary class has been marked as obsolete, so it's best not to use it.
This Stack Overflow post does a good job explaining the key differences:
Java hashmap vs hashtable
Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".
The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.
You are advised to use the HashMap though.
Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.
Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.
There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.

Categories