When should I accept a parameter of Iterable vs. Collection in Java?

Many of the collection types existed before Iterable<T> (which was only introduced in 1.5) – there was little reason to add a constructor to accept Iterable<T> as well as Collection<T> but changing the existing constructor would have been a breaking change. Personally I would use Iterable<T> if that allows you to do everything you want … Read more

Is it faster to add to a collection then sort it, or add to a sorted collection?

TreeSet has a log(n) time complexity guarantuee for add()/remove()/contains() methods. Sorting an ArrayList takes n*log(n) operations, but add()/get() takes only 1 operation. So if you’re mainly retrieving, and don’t sort often, ArrayList is the better choice. If you sort often but dont retrieve that much TreeSet would be a better choice.

Shortcut for adding to List in a HashMap

Since Java 8 you can make use of Map#computeIfAbsent(). Map<String, List<User>> usersByCountry = new HashMap<>(); for (User user : listOfUsers) { usersByCountry.computeIfAbsent(user.getCountry(), k -> new ArrayList<>()).add(user); } Or, make use of Stream API’s Collectors#groupingBy() to go from List to Map directly: Map<String, List<User>> usersByCountry = listOfUsers.stream().collect(Collectors.groupingBy(User::getCountry)); In Java 7 or below, best what you can … Read more

Sort Java Collection

Use a Comparator: List<CustomObject> list = new ArrayList<CustomObject>(); Comparator<CustomObject> comparator = new Comparator<CustomObject>() { @Override public int compare(CustomObject left, CustomObject right) { return left.getId() – right.getId(); // use your logic } }; Collections.sort(list, comparator); // use the comparator as much as u want System.out.println(list); Additionally, if CustomObjectimplements Comparable, then just use Collections.sort(list) With JDK 8 … Read more

maintaining TreeSet sort as object changes value

As others have noted, there is no in-built way. But you can always subclass that TreeSet, with your constructor(s) of choice, and add in the required functionality: public class UpdateableTreeSet<T extends Updateable> extends TreeSet<T> { // definition of updateable interface Updateable{ void update(Object value); } // constructors here … // ‘update’ method; returns false if … Read more

How to move specific item in array list to the first item

What you want is a very expensive operation in an ArrayList. It requires shifting every element between the beginning of the list and the location of C down by one. However, if you really want to do it: int index = url.indexOf(itemToMove); url.remove(index); url.add(0, itemToMove); If this is a frequent operation for you, and random … Read more

How to Serialize a list in java?

All standard implementations of java.util.List already implement java.io.Serializable. So even though java.util.List itself is not a subtype of java.io.Serializable, it should be safe to cast the list to Serializable, as long as you know it’s one of the standard implementations like ArrayList or LinkedList. If you’re not sure, then copy the list first (using something … Read more

Collectors.toMap() keyMapper — more succinct expression?

You can use a lambda: Collectors.toMap(p -> p.getLast(), Function.identity()) or, more concisely, you can use a method reference using ::: Collectors.toMap(Person::getLast, Function.identity()) and instead of Function.identity, you can simply use the equivalent lambda: Collectors.toMap(Person::getLast, p -> p) If you use Netbeans you should get hints whenever an anonymous class can be replaced by a lambda.

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