How to get a list of specific fields values from objects stored in a list?

Fortunately, you can do this using Java 8 – Streams Assume you have an entity named YourEntity public class YourEntity { private String field1; private String field2; public YourEntity(String field1, String field2) { this.field1 = field1; this.field2 = field2; } public void setField1(String field1) { this.field1 = field1; } public void setField2(String field2) { this.field2 … Read more

Is there an elegant way to remove nulls while transforming a Collection using Guava?

There’s already a predicate in Predicates that will help you here — Predicates.notNull() — and you can use Iterables.filter() and the fact that Lists.newArrayList() can take an Iterable to clean this up a little more. Collection<String> resourceIds = Lists.newArrayList( Iterables.filter( Iterables.transform(matchingComputers, yourFunction), Predicates.notNull() ) ); If you don’t actually need a Collection, just an Iterable, … Read more

Why does using Collections.emptySet() with generics work in assignment but not as a method parameter?

The short answer is – that’s a limitation of the type inference in Java’s generic system. It can infer generic types against concrete variables, but not against method parameters. I suspect this is because methods are dispatched dynamically depending on the runtime class of the owning object, so at compile time (when all generic information … Read more

How are Scala collections able to return the correct collection type from a map operation?

Scala collections are clever things… Internals of the collection library is one of the more advanced topics in the land of Scala. It involves higher-kinded types, inference, variance, implicits, and the CanBuildFrom mechanism – all to make it incredibly generic, easy to use, and powerful from a user-facing perspective. Understanding it from the point-of-view of … Read more

Add items to a collection if the collection does NOT already contain it by comparing a property of the items?

You start by finding which elements are not already in the collection: var newItems = DownloadedItems.Where(x => !CurrentCollection.Any(y => x.bar == y.bar)); And then just add them: foreach(var item in newItems) { CurrentCollection.Add(item); } Note that the first operation may have quadratic complexity if the size of DownloadedItems is close to the size of CurrentCollection. … Read more

Java: Hashset Vs TreeSet – when should I use over the other? [duplicate]

HashSet is Implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1). TreeSet is implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity O(log (n)). It offers several … Read more

Scala pattern matching on sequences other than Lists

As of the ides of March 2012, this works in 2.10+: def doMatch(seq: Seq[Int]): Unit = seq match { case last +: Seq() => println(“Final element.”) case head +: tail => println(“Recursing…”); doMatch(tail) } //> doMatch: (seq: Seq[Int])Unit doMatch(List(1, 2)) //> Recursing… //| Final element. More generally, two different head/tail and init/last decomposition objects mirroring … Read more

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