Itβs pretty accurate that there is no Java API method for this. However, you can write:
// This assumes your list is sorted according to someValue() // SomeValueType is the type of SomeObject.someValue() public Map<SomeValueType, List<SomeObject>> partition(List<SomeObject> list) { Object currValue = null; HashMap<SomeValueType, LinkedList<SomeObject>> result = new HashMap<SomeValueType, LinkedList<SomeObject>>(); LinkedList<SomeObject> currList = null; for (SomeObject obj : list) { if (!obj.someValue().equals(currValue()) { currValue = obj.someValue(); currList = new LinkedList<SomeObject>(); result.put(currValue, currList); } currList.add(obj); } }
This will return you the HashMap subscriptions, where the key is someValue and the value is the associated list of partitioned files. Note that I have not tested this, so do not just copy the code.
EDIT: did this hashmap return instead of arraylist.
source share