Which collection allows duplicates
Like Article. Next How to sort TreeSet in descending order in Java? Recommended Articles. Check whether array has all identical elements using Arrays. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment?
Please use ide. The Big-O notation can also be used to describe other behavior such as memory consumption. Since the collection classes are actually data structures, we usually use the Big-O notation to chose the best implementation to use, based on time, memory and performance. Big-O notation can give a good indication about performance for large amounts of data. The major advantage of an ordered array is that the search times have time complexity of O log n , compared to that of an unordered array, which is O n.
The disadvantage of an ordered array is that the insertion operation has a time complexity of O n , because the elements with higher values must be moved to make room for the new element. Instead, the insertion operation for an unordered array takes constant time of O 1. Enumeration is twice as fast as compared to an Iterator and uses very less memory. However, the Iterator is much safer compared to Enumeration , because other threads are not able to modify the collection object that is currently traversed by the iterator.
Also, Iterators allow the caller to remove elements from the underlying collection, something which is not possible with Enumeration. The HashSet is Implemented using a hash table and thus, its elements are not ordered. The add, remove, and contains methods of a HashSet have constant time complexity O 1.
On the other hand, a TreeSet is implemented using a tree structure. The elements in a TreeSet are sorted, and thus, the add, remove, and contains methods have time complexity of O logn. Serif Sans. White Sepia Night. Twitter Google Facebook Weibo Instapaper. Java Collections What are the basic interfaces of Java Collections Framework? The most basic interfaces that reside in the Java Collections Framework are: Collection , which represents a group of objects known as its elements.
Set , which is a collection that cannot contain duplicate elements. List , which is an ordered collection and can contain duplicate elements. Map , which is an object that maps keys to values and cannot contain duplicate keys. What is an Iterator? What differences exist between Iterator and ListIterator? The differences of these elements are listed below: An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only over List.
The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions. The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc. What is difference between fail-fast and fail-safe? How HashMap works in Java? What is the importance of hashCode and equals methods?
What differences exist between HashMap and Hashtable? A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments. A HashMap provides its set of keys and a Java application can iterate over them.
Stefan Dollase Stefan Dollase 4, 3 3 gold badges 22 22 silver badges 48 48 bronze badges. Brian Brian 21 5 5 bronze badges. Arif Burhan Arif Burhan 4 4 silver badges 12 12 bronze badges. Your case basically needs a HashMap. Just put key as key and value as value in a HashMap.
For ex. As per your requirement : Key1 aaaa -- should be stored Key1 bbbb -- should be stored Key1 aaaa -- should not be stored as it is duplicate. Hence, duplicacy of values is automatically handled by hashmap.
Hence , you can make use of HashMap in your case. In your arrogance you forgot to give a valid reason for the down vote. HashMap doesn't store duplicate values under the same key - you can only have one entry per key; and that entry will be replaced if you add another entry with the same key. If you want to store all entries for a given key, the value would have to be a collection which you add to; which make it a lot more complicated to maintain — simonalexander Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile.
0コメント