Is there a list type in java that stores objects in ascending order and not adds if this object is previously added. I know java maps can do that but I wonder if there is a list type that does what I want. Otherwise I have to override contains, equalsTo and add methods,right? Answer So you need a list containing
Tag: collections
How to convert comma-separated String to List?
Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that? String commaSeparated =…
How to iterate a HashMap using the natural entrySet() order?
My Map contains keys sorted in alphabetical order. When I display it, I’m using entrySet().iterator(), but my results are not in the alphabetical order. How can I get my results in order?
How can I initialize an ArrayList with all zeroes in Java?
It looks like arraylist is not doing its job for presizing: // presizing ArrayList
Print array without brackets and commas
I’m porting a Hangman game to Android and have met a few problems. The original Java program used the console, so now I have to somehow beautify the output so that it fits my Android layout. How do I …
Bounded, auto-discarding, non-blocking, concurrent collection
I’m looking for a collection that: is a Deque/List – i.e. supports inserting elements at “the top” (newest items go to the top) – deque.addFirst(..) / list.add(0, ..). It could be a Queue, but the iteration order should be reverse – i.e. the most recently added items should come first. is bounded – i.e. has a limit of 20 items
What type of data structure should I use to hold table rows?
I’m new to Java and just getting into querying databases. So far I have my results in a ResultSetMetaData. I’m think that for each row in the dataset I should add it to some form of collection? Can anyone tell me the best practice for this? Thanks, Jonesy Answer Usually we have a class with fields that correspond to a
Shortcut for adding to List in a HashMap
I often have a need to take a list of objects and group them into a Map based on a value contained in the object. Eg. take a list of Users and group by Country. My code for this usually looks like: However I can’t help thinking that this is awkward and some guru has a better approach. The closest
Java – Collections.sort() performance
I’m using Collections.sort() to sort a LinkedList whose elements implements Comparable interface, so they are sorted in a natural order. In the javadoc documentation its said this method uses …
How to cast a list of inheriting objects to a collection of objects in Java?
I’ve a collection type: And I’ve a list in my object: Where B is extending A But I can’t do the following: I can’t understand why since Collection is implemented by List. Answer Let’s assume for a moment you could do what you describe: The method call collecA.add(new A()) appears okay since collecA is a collection that holds As. However,