Java: Detect duplicates in ArrayList?

Simplest: dump the whole collection into a Set (using the Set(Collection) constructor or Set.addAll), then see if the Set has the same size as the ArrayList. List<Integer> list = …; Set<Integer> set = new HashSet<Integer>(list); if(set.size() < list.size()){ /* There are duplicates */ } Update: If I’m understanding your question correctly, you have a 2d … Read more

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

Common elements in two lists

Use Collection#retainAll(). listA.retainAll(listB); // listA now contains only the elements which are also contained in listB. If you want to avoid that changes are being affected in listA, then you need to create a new one. List<Integer> common = new ArrayList<Integer>(listA); common.retainAll(listB); // common now contains only the elements which are contained in listA and … Read more

How can I create an array in Kotlin like in Java by just providing a size?

According to the reference, arrays are created in the following way: For Java’s primitive types there are distinct types IntArray, DoubleArray etc. which store unboxed values. They are created with the corresponding constructors and factory functions: val arrayOfZeros = IntArray(size) //equivalent in Java: new int[size] val numbersFromOne = IntArray(size) { it + 1 } val … 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

How to use ArrayAdapter

Implement custom adapter for your class: public class MyClassAdapter extends ArrayAdapter<MyClass> { private static class ViewHolder { private TextView itemView; } public MyClassAdapter(Context context, int textViewResourceId, ArrayList<MyClass> items) { super(context, textViewResourceId, items); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(this.getContext()) .inflate(R.layout.listview_association, parent, false); viewHolder = … Read more

convert ArrayList to JSONArray

If I read the JSONArray constructors correctly, you can build them from any Collection (arrayList is a subclass of Collection) like so: ArrayList<String> list = new ArrayList<String>(); list.add(“foo”); list.add(“baar”); JSONArray jsArray = new JSONArray(list); References: jsonarray constructor: http://developer.android.com/reference/org/json/JSONArray.html#JSONArray%28java.util.Collection%29 collection: http://developer.android.com/reference/java/util/Collection.html