make some changes to the ExplicitOrdering
class of guava
.
List<String> orderings= Stream.of("", "").collect(Collectors.toList());
List<String> target= Stream.of( "ss","","3","1", "").collect(Collectors.toList());
class ExplicitOrdering<T> implements Comparator<T> {
private Map<T,Integer> indexMap = new HashMap();
public ExplicitOrdering(List<T> explicit) {
for (int i = 0; i < explicit.size(); iPP) {
indexMap.put(explicit.get(i),i);
}
}
@Override
public int compare(T o1, T o2) {
return rank(o1) - rank(o2);
}
private int rank(T value) {
Integer rank = indexMap.get(value);
if (rank == null) {
return Integer.MIN_VALUE;
}
return rank;
}
}
List<String> strings = target.stream().sorted(new ExplicitOrdering<>(orderings).thenComparing(Comparator.naturalOrder())).collect(Collectors.toList());
System.out.println(strings);
< H1 > the mountain is not turning and the water is turning. < / H1 >
since the standard sort given is not enough, merge it with the comparison of the data to be sorted, and prepare enough, then the sorting method that ultimately conforms to my arrangement. Just add one more code
this comparison of strings may not make much sense. But it simplifies demand. In fact, target is an object, so it is necessary for Map to do so
List<String> orderings= Stream.of("", "").collect(Collectors.toList());
List<String> target= Stream.of("1", "ss","", "").collect(Collectors.toList());
//
List<String> newOrdering = Stream.concat(orderings.stream(), target.stream().filter(item -> !orderings.contains(item))).collect(Collectors.toList());
Ordering ordering = Ordering.explicit(newOrdering);
List<String> strings = target.stream().sorted(new Comparator<String>() {
@Override
public int compare (String o1, String o2) {
if (newOrdering.contains(o1) && newOrdering.contains(o2)) {
return ordering.compare(o1, o2);
} else {
return 0;
}
}
}).collect(Collectors.toList());
System.out.println(strings);
final result:
[temperature, running time, temperature 1, running time ss]
ha, I'm sorry to have asked the subject for so long. Based on the description of the requirements I got, I finally felt that referencing new programming elements to make the business a little easier, I sorted out that the requirements should be:
first of all, given a sort of specified keywords, give a list of strings, sort the list. If the list of strings contains keywords, then this kind of string has a high priority and ranks first, and it is also sorted according to the order of keywords contained in the string. Other strings that do not contain keywords are lower, and the order does not care about
.
so I quote the new programming element, because the orderings
given is in order, so only one string cannot reflect this order. I added a new element OrderParam
.
@Getter
@Builder
public class OrderParam{
private String param;
//
private int order;
}
so the previous
List<String> orderings= Stream.of("", "").collect(Collectors.toList());
becomes
List<OrderParam> orderings= OrderParams.createOrderings("", "");
A utility class OrderParams
is written here.
public class OrderParams{
/**
* OrderParam
* @param params
* @return
*/
public static List<OrderParam> createOrderings(String ... params){
return IntStream.range(0, params.length)
.mapToObj(i -> OrderParam.builder().param(params[i]).order(i).build())
.collect(Collectors.toList());
}
}
then the final test class is written like this
public class Test {
/**
* orderingstargetorderings
* @param args
*/
public static void main(String[] args) {
List<OrderParam> orderings= OrderParams.createOrderings("", "");
List<String> target= Stream.of("sa", "1", "ttttt", "", "", "ss")
.collect(Collectors.toList());
List<String> result = target.stream()
.map(t -> toOrderParam(orderings, t))
// OrderParamOrderParamorder
.sorted(Comparator.comparing(OrderParam::getOrder))
.map(OrderParam::getParam)
.collect(Collectors.toList());
System.out.println(result);
}
private static OrderParam toOrderParam(List<OrderParam> orderings, String t) {
return orderings.stream()
.filter(orderParam -> t.contains(orderParam.getParam()))
.findFirst()
.map(orderParam -> OrderParam.builder().param(t).order(orderParam.getOrder()).build())
// orderings
.orElse(OrderParam.builder().param(t).order(Integer.MAX_VALUE).build());
}
}
final execution result:
that's what I think. I'm asking a lot of questions. no, no, no. Excuse me. Ha