Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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_Comparator - Fatal编程技术网

Java 使用比较器,我如何确定对象之间的匹配质量,或者有更好的方法吗

Java 使用比较器,我如何确定对象之间的匹配质量,或者有更好的方法吗,java,comparator,Java,Comparator,我有一个代码库,试图确定提供的服务之间的相似性。这些服务有不同的类别。但是,我想通过比较类别集、服务时间和服务成本来确定相似性。我已经编写了代码,并且它按照预期工作(我认为),但是我感觉到我遗漏了一些东西,所以我想把它放在这里以获得一些反馈,并获得一些理解 测试 @Test public void testSimilarProjectComparatorSorting() { List<ProjectService> projectServices = createProje

我有一个代码库,试图确定提供的服务之间的相似性。这些服务有不同的类别。但是,我想通过比较类别集、服务时间和服务成本来确定相似性。我已经编写了代码,并且它按照预期工作(我认为),但是我感觉到我遗漏了一些东西,所以我想把它放在这里以获得一些反馈,并获得一些理解

测试

@Test
public void testSimilarProjectComparatorSorting() {
    List<ProjectService> projectServices = createProjectServices();

    System.out.println(projectServices);

    projectServices.sort(new SimilarProjectComparator());

    System.out.println(projectServices);

    // Assertions
}

public static List<ProjectService> createProjectServices() {
    List<ProjectService> projectServices = new ArrayList<>();

    ProjectService projectService = new ProjectService();
    projectService.setAmount(new BigDecimal(1800));
    projectService.setDifficultyScale(Estimation.FIVE);

    Category genericCat = new Category();
    genericCat.setName("Generic");
    genericCat.setSlug("generic");

    Category listingCat = new Category();
    listingCat.setName("Listing");
    listingCat.setSlug("listing");

    Category webCat = new Category();
    webCat.setName("Web");
    webCat.setSlug("web");

    projectService.setCategories(new HashSet<>(Arrays.asList(genericCat, listingCat, webCat)));

    projectServices.add(projectService);

    projectService = new ProjectService();
    projectService.setAmount(new BigDecimal(800));
    projectService.setDifficultyScale(Estimation.THREE);

    Category outliningCat = new Category();
    outliningCat.setName("Outlining");
    outliningCat.setSlug("outlining");

    Category bullHeadedCat = new Category();
    bullHeadedCat.setName("Bull-Headed");
    bullHeadedCat.setSlug("bull-headed");

    projectService.setCategories(
            new HashSet<>(
                    Arrays.asList(
                            outliningCat,
                            bullHeadedCat,
                            webCat
                    )
            )
    );

    projectServices.add(projectService);

    projectService = new ProjectService();
    projectService.setAmount(new BigDecimal(1500));
    projectService.setDifficultyScale(Estimation.FIVE);

    Category writingCat = new Category();
    writingCat.setName("Writing");
    writingCat.setSlug("writing");

    projectService.setCategories(
            new HashSet<>(
                    Arrays.asList(
                            writingCat,
                            genericCat,
                            listingCat
                    )
            )
    );

    projectServices.add(projectService);

    projectService = new ProjectService();
    projectService.setAmount(new BigDecimal(1400));
    projectService.setDifficultyScale(Estimation.TWO);

    projectService.setCategories(
            new HashSet<>(
                    Arrays.asList(
                            writingCat,
                            genericCat,
                            listingCat,
                            webCat
                    )
            )
    );

    projectServices.add(projectService);

    return projectServices;
}
@测试
public void testSimilarProjectComparatorSorting()测试{
List projectServices=createProjectServices();
系统输出打印LN(项目服务);
排序(新的SimilarProjectComparator());
系统输出打印LN(项目服务);
//断言
}
公共静态列表createProjectServices(){
List projectServices=new ArrayList();
ProjectService ProjectService=新的ProjectService();
projectService.setAmount(新的BigDecimal(1800));
项目服务.设置难度量表(估计5);
类别genericCat=新类别();
genericCat.setName(“Generic”);
通用类别固定凸耳(“通用”);
类别列表CAT=新类别();
listingCat.setName(“列表”);
listingCat.setSlug(“上市”);
类别网络广播=新类别();
webCat.setName(“网络”);
webCat.setSlug(“web”);
setCategories(新的HashSet(Arrays.asList(genericCat、listingCat、webCat));
projectServices.add(projectService);
projectService=新的projectService();
projectService.setAmount(新的BigDecimal(800));
项目服务。设置难度量表(估计3);
类别概述CAT=新类别();
大纲显示cat.setName(“大纲显示”);
概述类别固定凸耳(“概述”);
类别bullHeadedCat=新类别();
bullHeadedCat.setName(“bullheaded”);
牛头猫固定凸耳(“牛头”);
projectService.setCategories(
新哈希集(
Arrays.asList(
概述猫,
牛头猫,
网络广播
)
)
);
projectServices.add(projectService);
projectService=新的projectService();
projectService.setAmount(新的BigDecimal(1500));
项目服务.设置难度量表(估计5);
Category writingCat=新类别();
编写cat.setName(“编写”);
写入CAT.setSlug(“写入”);
projectService.setCategories(
新哈希集(
Arrays.asList(
写猫,
通用猫,
列表猫
)
)
);
projectServices.add(projectService);
projectService=新的projectService();
projectService.setAmount(新的BigDecimal(1400));
项目服务.设置难度量表(估计2);
projectService.setCategories(
新哈希集(
Arrays.asList(
写猫,
通用猫,
listingCat,
网络广播
)
)
);
projectServices.add(projectService);
返回项目服务;
}
代码

public class SimilarProjectComparator implements Comparator<ProjectService> {
    @Override
    public int compare(ProjectService o1, ProjectService o2) {
        Set<Category> o1CategorySet = o1.getCategories();
        Set<Category> o2CategorySet = o2.getCategories();
        Integer categoryMatch = 0;
        Double matchQuality = 0.0;

        if (o1CategorySet != null && o2CategorySet != null) {
            for (Category o1Category : o1CategorySet) {
                for (Category o2Category : o2CategorySet) {
                    Integer match = o1Category.getName().compareTo(o2Category.getName());
                    if (match > 0) {
                        categoryMatch++;
                    } else {
                        categoryMatch--;
                    }
                }
            }
        }

        if (categoryMatch > 0) {
            matchQuality++;
        } else {
            matchQuality--;
        }

        Integer scaleMatch = o1.getDifficultyScale().getEstimation().compareTo(
                o2.getDifficultyScale().getEstimation()
        );

        if (scaleMatch > 0) {
            matchQuality++;
        } else {
            matchQuality--;
        }

        Integer amountMatch = o1.getAmount().compareTo(o2.getAmount());

        if (amountMatch > 0) {
            matchQuality++;
        } else {
            matchQuality--;
        }

        return matchQuality.intValue();
    }
}
公共类SimilarProjectComparator实现Comparator{
@凌驾
公共整数比较(项目服务o1、项目服务o2){
设置o1CategorySet=o1.getCategories();
设置o2CategorySet=o2.getCategories();
整数类别匹配=0;
双匹配质量=0.0;
如果(o1CategorySet!=null&&o2CategorySet!=null){
适用于(O1类:O1类集){
适用于(O2类:O2类集){
整数匹配=o1Category.getName().compareTo(o2Category.getName());
如果(匹配>0){
categoryMatch++;
}否则{
类别匹配--;
}
}
}
}
如果(类别匹配>0){
matchQuality++;
}否则{
匹配质量--;
}
整数比例匹配=o1.getDifficultyScale().getEstimation().compareTo(
o2.GetHardicultyScale().getEstimation())
);
如果(比例匹配>0){
matchQuality++;
}否则{
匹配质量--;
}
整数amountMatch=o1.getAmount().compareTo(o2.getAmount());
如果(数量匹配>0){
matchQuality++;
}否则{
匹配质量--;
}
返回matchQuality.intValue();
}
}

我假设顺序是(根据类别的相似程度判断):1400、1500、800,然后是1800。然而,实际订单是:80014015001800。我是比较新手,不确定我是否做对了我是否遗漏了一些东西,或者这是否正确,而我的假设是不正确的?我知道我正在尝试使用3种不同的道具来确定对象匹配的质量,因此类别的相似程度不能保证我上面提到的顺序,除非它具有更大的权重。从业务逻辑的角度来看,我认为这是不必要的。我只是一个新的比较,并试图了解这一点。提前感谢您的帮助

你在寻找相似之处。如果比较两个元素的结果为0,则这些元素相同。否则,它们就不是。我把你的日程安排改为:

public int compare(ProjectService o1, ProjectService o2) {
    Set<Category> o1CategorySet = o1.getCategories();
    Set<Category> o2CategorySet = o2.getCategories();
    int categoryMatch = 0;
    double matchQuality = 0.0;

    if ((o1CategorySet != null) && (o2CategorySet != null)) {
        for (Category o1Category : o1CategorySet) {
            for (Category o2Category : o2CategorySet) {
                int match = o1Category.getName().compareTo(o2Category.getName());
                if (match == 0) {
                    categoryMatch++;
                } else {
                    categoryMatch--;
                }
            }
        }
    }

    if (categoryMatch == 0) {
        matchQuality++;
    } else {
        matchQuality--;
    }

    int scaleMatch = o1.getDifficultyScale().getEstimation().compareTo(o2.getDifficultyScale().getEstimation());

    if (scaleMatch == 0) {
        matchQuality++;
    } else {
        matchQuality--;
    }

    int amountMatch = o1.getAmount().compareTo(o2.getAmount());

    if (amountMatch == 0) {
        matchQuality++;
    } else {
        matchQuality--;
    }

    return (int) matchQuality;
}
public int比较(项目服务o1、项目服务o2){
设置o1CategorySet=o1.getCategories();
设置o2CategorySet=o2.getCategories();
int categoryMatch=0;
双匹配质量=0.0;
if((o1CategorySet!=null)&&(o2CategorySet!=null)){
适用于(O1类:O1类集){