Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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,我试图通过定义一些前缀,根据自定义排序顺序对字符串列表进行排序。结果应该根据前缀的顺序排序,不匹配的条目应该在列表的末尾。当多个字符串以同一前缀开头时,或者其他字符串中有多个条目时,应按字母顺序排序 已经尝试了基于另一个线程的解决方案的自定义比较器。但是,我现在被困在我的要求的最后一部分,需要将列表末尾没有前缀的其余条目带到列表中 final Comparator Comparator=new PrefixComparator(Arrays.asList(“java.”、“javax.”、“or

我试图通过定义一些前缀,根据自定义排序顺序对字符串列表进行排序。结果应该根据前缀的顺序排序,不匹配的条目应该在列表的末尾。当多个字符串以同一前缀开头时,或者其他字符串中有多个条目时,应按字母顺序排序

已经尝试了基于另一个线程的解决方案的自定义比较器。但是,我现在被困在我的要求的最后一部分,需要将列表末尾没有前缀的其余条目带到列表中

final Comparator Comparator=new PrefixComparator(Arrays.asList(“java.”、“javax.”、“org”);
最终ArrayList导入=Lists.newArrayList(“java.util.Arrays”、“javax.annotation.Generated”、“org.testng.annotations.Test”、“org.testng.annotations.Optional”、“de.bigmichi1.Test.ClassB”);
收藏。洗牌(进口);
导入。排序(比较);
Assertions.assertThat(imports).containsExactly(“java.util.array”、“javax.annotation.Generated”、“org.testng.annotations.Optional”、“org.testng.annotations.Test”、“de.bigmichi1.Test.ClassB”);
公共类PrefixComparator实现可序列化的比较器{
私有静态最终长serialVersionUID=-5748758614646881440L;
私人最终清单订单;
公共PrefixComparator(@Nonnull final List customOrder){
this.customOrder=customOrder;
}
@凌驾
公共整数比较(最终字符串o1,
最终字符串(o2){
整数顺序1=-1;
整数顺序2=-1;
字符串余数1=“”;
字符串余数2=“”;
for(最终字符串前缀:customOrder){
if(o1.带(前缀)的开始){
order1=customOrder.indexOf(前缀);
remainder1=o1.substring(prefix.length());
}
if(o2.带(前缀)的启动){
order2=customOrder.indexOf(前缀);
remainder2=o2.substring(前缀.length());
}
}
if(order1==order2){
返回remainder1.compareTo(remainder2);
}否则{
退货订单1-订单2;
}
}
}

以下是我的做法:

import java.util.*;

public class Test
{
    public static void main(String[] args) {
        List<String> prefix = Arrays.asList("java.", "javax.", "org.");
        List<String> imports = Arrays.asList("org.testng.annotations.Test", "java.util.Arrays", "javax.annotation.Generated", "org.testng.annotations.Optional", "de.bigmichi1.test.ClassB");
        Collections.sort(imports, new Comparator<String>(){
            int prefix1Index = Integer.MAX_VALUE, prefix2Index = Integer.MAX_VALUE;
            @Override
            public int compare(String o1, String o2) {
                prefix1Index = Integer.MAX_VALUE;
                prefix2Index = Integer.MAX_VALUE;
                prefix.forEach((pre) -> {
                    if(o1.startsWith(pre)){
                        prefix1Index = prefix.indexOf(pre);
                    }
                    if(o2.startsWith(pre)){
                        prefix2Index = prefix.indexOf(pre);
                    }
                });

                if(prefix1Index == prefix2Index) return o1.compareTo(o2);
                else return prefix1Index - prefix2Index;
            }
        });

        imports.forEach((imported) -> {
            System.out.println(imported);
        });

    }
}

如果字符串不以前缀开头,则剩余部分不应为空字符串。它应该是字符串本身。如果字符串不以前缀开头,因为您希望它位于排序列表的末尾,那么它的顺序不应该是-1(比其他顺序低),而是,
customOrder.size()
,比其他顺序大。
java.util.Arrays
javax.annotation.Generated
org.testng.annotations.Optional
org.testng.annotations.Test
de.bigmichi1.test.ClassB