java - How can I sort on certain properties of an object in a pre-sorted TreeSet? -
first of all, reading!
i have created custom class calllog
logid
, logname
, logcompany
, ... these calllogs stored within treeset , sorted logpriority
, logdatetime
default. need print rapports sorted different values. i've created abstract class rapport
methods printbyname()
sort treeset other values well.
i'm not supposed alter compareto()
method of calllog know how sort treeset using other properties of calllog
.
you can't change sorting of existing treeset
, can copy values [temporary] collection sorted differently using custom comparator
. in fact, don't have create new collection, sort streamed values print them: e.g.:
public class report { private set<calllog> calls = // initialized somehow... public void printbyname() { calls.stream() .sorted(comparator.comparing(calllog::logname)) .foreach(system.out::println); }
Comments
Post a Comment