How to find an object in an ArrayList by property

In Java8 you can use streams: public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsIn) { return listCarnet.stream().filter(carnet -> codeIsIn.equals(carnet.getCodeIsin())).findFirst().orElse(null); } Additionally, in case you have many different objects (not only Carnet) or you want to find it by different properties (not only by cideIsin), you could build an utility class, to ecapsulate this logic in it: … Read more

builder for HashMap

There is no such thing for HashMaps, but you can create an ImmutableMap with a builder: final Map<String, Integer> m = ImmutableMap.<String, Integer>builder(). put(“a”, 1). put(“b”, 2). build(); And if you need a mutable map, you can just feed that to the HashMap constructor. final Map<String, Integer> m = Maps.newHashMap( ImmutableMap.<String, Integer>builder(). put(“a”, 1). put(“b”, … Read more

Java Immutable Collections

Unmodifiable collections are usually read-only views (wrappers) of other collections. You can’t add, remove or clear them, but the underlying collection can change. Immutable collections can’t be changed at all – they don’t wrap another collection – they have their own elements. Here’s a quote from guava’s ImmutableList Unlike Collections.unmodifiableList(java.util.List<? extends T>), which is a … Read more

Sorting an ArrayList of objects using a custom sorting order

Here’s a tutorial about ordering objects: The Java Tutorials – Collections – Object Ordering Although I will give some examples, I would recommend to read it anyway. There are various way to sort an ArrayList. If you want to define a natural (default) ordering, then you need to let Contact implement Comparable. Assuming that you … Read more

Create a List of primitive int?

In Java the type of any variable is either a primitive type or a reference type. Generic type arguments must be reference types. Since primitives do not extend Object they cannot be used as generic type arguments for a parametrized type. Instead use the Integer class which is a wrapper for int: List<Integer> list = … Read more

Android-java- How to sort a list of objects by a certain value within the object

Follow this code to sort any ArrayList Collections.sort(myList, new Comparator<EmployeeClass>(){ public int compare(EmployeeClass obj1, EmployeeClass obj2) { // ## Ascending order return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values // return Integer.valueOf(obj1.empId).compareTo(Integer.valueOf(obj2.empId)); // To compare integer values // ## Descending order // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values // return Integer.valueOf(obj2.empId).compareTo(Integer.valueOf(obj1.empId)); // To compare integer … Read more

Any implementation of Ordered Set in Java?

Take a look at LinkedHashSet class From Java doc: Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements … Read more

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