In Java how do you sort one list based on another?
Using Java 8: Collections.sort(listToSort, Comparator.comparing(item -> listWithOrder.indexOf(item))); or better: listToSort.sort(Comparator.comparingInt(listWithOrder::indexOf));
Using Java 8: Collections.sort(listToSort, Comparator.comparing(item -> listWithOrder.indexOf(item))); or better: listToSort.sort(Comparator.comparingInt(listWithOrder::indexOf));
To insert value into ArrayList at particular index, use: public void add(int index, E element) This method will shift the subsequent elements of the list. but you can not guarantee the List will remain sorted as the new Object you insert may sit on the wrong position according to the sorting order. To replace the … Read more
will this change reflect in the ArrayList? Yes, since you added a reference to the object in the list. The reference you added will still point to the same object, (which you modified). or when I add the object to the ArrayList, Java creates a copy and add it to the ArrayList? No, it won’t … Read more
If you have another list that contains all the items you would like to add you can do arList.addAll(otherList). Alternatively, if you will always add the same elements to the list you could create a new list that is initialized to contain all your values and use the addAll() method, with something like Integer[] otherList … Read more
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
In C#:Fixed with recommendation from Mike ArrayList list = …; // List<object> list = …; foreach (object o in list) { if (o is int) { HandleInt((int)o); } else if (o is string) { HandleString((string)o); } … } In Java: ArrayList<Object> list = …; for (Object o : list) { if (o instanceof Integer)) { … Read more
TL;DR List<V> al = new ArrayList<V>(hashMapVar.values()); Explanation Because HashMap#values() returns a java.util.Collection<V> and you can’t cast a Collection into an ArrayList, thus you get ClassCastException. I’d suggest using ArrayList(Collection<? extends V>) constructor. This constructor accepts an object which implements Collection<? extends V> as an argument. You won’t get ClassCastException when you pass the result of … Read more
You can use Collections.swap(List<?> list, int i, int j);
If you’re using Java 8 List<String> list = new ArrayList<>(); boolean containsSearchStr = list.stream().anyMatch(“search_value”::equalsIgnoreCase);
Arrays.asList returns a List implementation, but it’s not a java.util.ArrayList. It happens to have a classname of ArrayList, but that’s a nested class within Arrays – a completely different type from java.util.ArrayList. If you need a java.util.ArrayList, you can just create a copy: ArrayList<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue()); If you don’t need an ArrayList just … Read more