Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据另一个列表字符串对列表字符串进行排序_Java - Fatal编程技术网

Java 根据另一个列表字符串对列表字符串进行排序

Java 根据另一个列表字符串对列表字符串进行排序,java,Java,我有一个列表,其值如下: static String input = "pro or 7"; List<String> arrayOfString = Arrays.asList(input.toLowerCase().split("\\s+")); 静态字符串输入=“pro或7”; List arrayOfString=Arrays.asList(input.toLowerCase().split(\\s+)); 我有另一个字符串列表: static List<Strin

我有一个
列表
,其值如下:

static String input = "pro or 7";
List<String> arrayOfString = Arrays.asList(input.toLowerCase().split("\\s+"));
静态字符串输入=“pro或7”;
List arrayOfString=Arrays.asList(input.toLowerCase().split(\\s+));
我有另一个字符串列表:

static List<String> organizations = Arrays.asList(
        "Service Provider organisation 3 01254 25742", // count=3
        "Service Provider test 2 132455 132455",       // count=1
        "Service Provider organisation 2 78900 6521",  // count=3
        "VOYAGES ANSELMINO 30876168300025 382510022",  // count=1
        "Service Provider test 1 32722 21211",         // count=2
        "SP recherche autocomplete 7897788 7897788")   // count=1
        .stream().map(String::toLowerCase)
        .collect(Collectors.toList());
static List organizations=Arrays.asList(
“服务提供商组织3 01254 25742”,//计数=3
“服务提供商测试2 132455 132455”,//计数=1
“服务提供商组织2 78900 6521”,//计数=3
“航行安塞尔米诺30876168300025 382510022”//count=1
“服务提供商测试1 32722 21211”,//计数=2
“SP recherche autocomplete 7897788 7897788”)//计数=1
.stream().map(字符串::toLowerCase)
.collect(Collectors.toList());
我想根据list arrayOfString的元素对列表组织进行排序

例如:

  • str=服务提供商测试1 32722 2111
count
将等于2,因为
str
包含“pro”和“7”

  • str=SP recherche自动完成7897788 7897788
count
将等于1,因为
str
包含“7”(消除重复字符)


然后根据
count

的结果对列表组织进行排序,作为练习,我根据提示{@Lokesh give}实现它。 你可以参考一下

List<String> organizations = Arrays.asList(
    "Service Provider organisation 3 01254 25742", // count=3
    "Service Provider test 2 132455 132455",       // count=1
    "Service Provider organisation 2 78900 6521",  // count=3
    "VOYAGES ANSELMINO 30876168300025 382510022",  // count=1
    "Service Provider test 1 32722 21211",         // count=2
    "SP recherche autocomplete 7897788 7897788")   // count=1
    .stream().map(String::toLowerCase).sorted(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return getCountMeetCondition(o2) - getCountMeetCondition(o1);
        }
}).collect(Collectors.toList());

private static int getCountMeetCondition(String s) {
    int count = 0;
    for (int i = 0; i < arrayOfString.size(); i++) {
        if (s.contains(arrayOfString.get(i))) {
            count++;
        }
   }
   return count;
}
List organizations=Arrays.asList(
“服务提供商组织3 01254 25742”,//计数=3
“服务提供商测试2 132455 132455”,//计数=1
“服务提供商组织2 78900 6521”,//计数=3
“航行安塞尔米诺30876168300025 382510022”//count=1
“服务提供商测试1 32722 21211”,//计数=2
“SP recherche autocomplete 7897788 7897788”)//计数=1
.stream().map(字符串::toLowerCase).sorted(新比较器(){
@凌驾
公共整数比较(字符串o1、字符串o2){
返回getCountMeetCondition(o2)-getCountMeetCondition(o1);
}
}).collect(Collectors.toList());
私有静态整型getCountMeetCondition(字符串s){
整数计数=0;
对于(int i=0;i
final Function split=str->new HashSet(Arrays.asList(str.toLowerCase().split(\\s+));
最终设置键=拆分。应用(“pro或7”);
最终函数getCount=str->{
整数计数=0;
str=str.toLowerCase();
用于(字符串键:键)
如果(str.contains(key))
计数++;
返回计数;
};
最终比较器Comparator=Comparator.comparingInt(getCount::apply);

您可以使用此
比较器对收藏进行排序。

您是否尝试过,但失败了?请展示您尝试过的内容。这似乎是一个作业问题,所以我不会给出完整的解决方案。只是一个提示:将stream.sorted方法与自定义比较器一起使用
.stream().sorted(new Comparator(){…})
“For Mother”不包含“pro”,也不包含“7”,但包含“or”。那么,
count
是零还是1?@KevinAnderson count是:1
final Function<String, Set<String>> split = str -> new HashSet<>(Arrays.asList(str.toLowerCase().split("\\s+")));
final Set<String> keys = split.apply("pro or 7");
final Function<String, Integer> getCount = str -> {
    int count = 0;
    str = str.toLowerCase();

    for (String key : keys)
        if (str.contains(key))
            count++;

    return count;
};

final Comparator<String> comparator = Comparator.comparingInt(getCount::apply);