Skip to content
Advertisement

Tag: collections

How can I initialize an ArrayList with all zeroes in Java?

It looks like arraylist is not doing its job for presizing: Afterwards when I try to access it: Instead of returning 0 it throws IndexOutOfBoundsException: Index 5 out of bounds for length 0. Is there a way to initialize all elements to 0 of an exact size like what C++ does? Answer The integer passed to the constructor represents its

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 print an array without the brackets and commas? The array contains slashes and gets replaced one-by-one when the correct letter is

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

Java : convert List of Bytes to array of bytes

Trying to solve what should be a simple problem. Got a list of Bytes, want to convert it at the end of a function to an array of bytes. compiler doesn’t like syntax on my toArray. How to fix this? Answer The compiler doesn’t like it, because byte[] isn’t Byte[]. What you can do is use commons-lang’s ArrayUtils.toPrimitive(wrapperCollection): If you

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 mergesort algorithm which has n*log(n) performance. My question is if there is a more efficient algorithm to sort my LinkedList? The size of that list could be very high and sort

Map equality using Hamcrest

I’d like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values. My current best guess is: which gives: The method assertThat(T, Matcher<T>) in the type Assert is not applicable for the arguments (Set<Map.Entry<Householdtypes,Double>>, Matcher<Iterable<Set<Map.Entry<Householdtypes,Double>>>>) I’ve also looked into variations of containsAll, and some others provided by

Advertisement