Java Stream: divide into two lists by boolean predicate

Collectors.partitioningBy: Map<Boolean, List<Employee>> partitioned = listOfEmployees.stream().collect( Collectors.partitioningBy(Employee::isActive)); The resulting map contains two lists, corresponding to whether or not the predicate was matched: List<Employee> activeEmployees = partitioned.get(true); List<Employee> formerEmployees = partitioned.get(false); There are a couple of reasons to use partitioningBy over groupingBy (as suggested by Juan Carlos Mendoza): Firstly, the parameter of groupingBy is a Function<Employee, … Read more

Is there a java hash structure with keys only and no values?

You need Java’s HashSet (Java 8). The description from the official documentation is: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This … Read more

When is the unmodifiablemap (really) necessary?

Collections.unmodifiableMap guarantees that the map will not be modified. It’s mostly useful if you want to return a read-only view of an internal map from a method call, e.g: class A { private Map importantData; public Map getImportantData() { return Collections.unmodifiableMap(importantData); } } This gives you a fast method that does not risk the client … Read more

How to sort an arraylist of objects by a property?

You can use Collections.sort with a custom Comparator<HockeyPlayer>. class HockeyPlayer { public final int goalsScored; // … }; List<HockeyPlayer> players = // … Collections.sort(players, new Comparator<HockeyPlayer>() { @Override public int compare(HockeyPlayer p1, HockeyPlayer p2) { return p1.goalsScored – p2.goalsScored; // Ascending } }); The comparision part can also be written this way : players.sort(Comparator.comparingInt(HockeyPLayer::goalsScored)); Alternatively, … Read more

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