Google Guava vs. Apache Commons [closed]

In my opinion the better choice is Guava (formerly known as Google collections): it’s more modern (has generics) it absolutely follows the Collections API requirements it’s actively maintained CacheBuilder and it’s predecessor MapMaker are just plain awesome Apache Commons Collections is a good library as well, but it has long failed to provide a generics-enabled … Read more

Size-limited queue that holds last N elements in Java

Apache commons collections 4 has a CircularFifoQueue<> which is what you are looking for. Quoting the javadoc: CircularFifoQueue is a first-in first-out queue with a fixed size that replaces its oldest element if full. import java.util.Queue; import org.apache.commons.collections4.queue.CircularFifoQueue; Queue<Integer> fifo = new CircularFifoQueue<Integer>(2); fifo.add(1); fifo.add(2); fifo.add(3); System.out.println(fifo); // Observe the result: // [2, 3] If … Read more

How to sort an ArrayList in Java [duplicate]

Use a Comparator like this: List<Fruit> fruits= new ArrayList<Fruit>(); Fruit fruit; for(int i = 0; i < 100; i++) { fruit = new Fruit(); fruit.setname(…); fruits.add(fruit); } // Sorting Collections.sort(fruits, new Comparator<Fruit>() { @Override public int compare(Fruit fruit2, Fruit fruit1) { return fruit1.fruitName.compareTo(fruit2.fruitName); } }); Now your fruits list is sorted based on fruitName.

Convert JSON to Map

I hope you were joking about writing your own parser. 🙂 For such a simple mapping, most tools from http://json.org (section java) would work. For one of them (Jackson https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator), you’d do: Map<String,Object> result = new ObjectMapper().readValue(JSON_SOURCE, HashMap.class); (where JSON_SOURCE is a File, input stream, reader, or json content String)

How to get a reversed list view on a list in Java?

Use the .clone() method on your List. It will return a shallow copy, meaning that it will contain pointers to the same objects, so you won’t have to copy the list. Then just use Collections. Ergo, Collections.reverse(list.clone()); If you are using a List and don’t have access to clone() you can use subList(): List<?> shallowCopy … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)