简体   繁体   English

比较java中具有部分公共值的两个数组列表

[英]Compare two arraylists in java which have partial common values

I have one array list having values as:我有一个数组列表,其值为:

IT Infrastructure IT基础设施
Teams团队
Wave Analytics Starter Kit Wave 分析入门套件
Blog Post博客文章
Case Studies实例探究
InfoGraphics信息图表
Grazitti Site Development-Revamp Grazitti 网站开发-改造

and another array list having values as:另一个数组列表的值为:

Wave Analytics Starter Kit56:00:00 Wave Analytics 入门套件56:00:00
IT Infrastructure01:00:00 IT基础设施01:00:00
Teams08:00:00团队08:00:00
Blog Post21:30:00博文21:30:00
Case Studies02:30:00案例研究02:30:00
InfoGraphics02:45:00信息图表02:45:00
Grazitti Site Development-Revamp11:00:00 Grazitti 网站开发-改造11:00:00
Content Marketing03:00:00内容营销03:00:00
Off-Page Content04:30:00页外内容04:30:00
SMM05:00:00 SMM05:00:00
Guest Blog01:30:00来宾博客01:30:00
LiNC 1600:30:00连线 1600:30:00
Alteryx Inspire Conference 201625:30:00 Alteryx 启发大会 201625:30:00
PPC02:00:00 PPC02:00:00
Training13:45:00培训13:45:00

Can I compare them to get the output as:我可以比较它们以获得输出为:

IT Infrastructure01:00:00 IT基础设施01:00:00
Teams08:00:00团队08:00:00
Wave Analytics Starter Kit56:00:00 Wave Analytics 入门套件56:00:00
Blog Post21:30:00博文21:30:00
Case Studies02:30:00案例研究02:30:00
InfoGraphics02:45:00信息图表02:45:00
Grazitti Site Development-Revamp11:00:00 Grazitti 网站开发-改造11:00:00

The following piece of code isn't giving the desired ouput:以下代码没有给出所需的输出:

for(int c =0; c<extractedvalue.size(); c++){
System.out.println(extractedvalue.get(c));
           if (extractedvalue.contains(hoursplusprojects.get(c))){
               System.out.println(hoursplusprojects.get(c));
           }

Here is a solution in O(nm).这是一个 O(nm) 的解决方案。 n and m are the sizes of your arrays. n 和 m 是数组的大小。 The solution works for the case that the two arrays have the structure that you illustrated in your question.该解决方案适用于两个数组具有您在问题中说明的结构的情况。

List<String> l1 = new ArrayList<String>();

//update l1

List<String> l2 = new ArrayList<String>();

//update l2

List<String> results = new ArrayList<String>();

for(String s1 : l1){
    for(String s2 : l2){
        if(s2.contains(s1)){
            results.add(s2);
        }
}

//print the results
for(String r : results){
    System.out.println(r);
}

Please check the following solution.请检查以下解决方案。 If you have any question, please ask.如果您有任何问题,请提问。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test { 

    public static void main(String[] args) {

        List<String> extractedvalue =Arrays.asList
                ("IT Infrastructure",
                "Teams",
                "Wave Analytics Starter Kit",
                "Blog Post",
                "Case Studies",
                "InfoGraphics",
                "Grazitti Site Development-Revamp");


        List<String> hoursplusprojects =Arrays.asList
                ("Wave Analytics Starter Kit56:00:00",
                "IT Infrastructure01:00:00",
                "Teams08:00:00",
                "Blog Post21:30:00",
                "Case Studies02:30:00",
                "InfoGraphics02:45:00",
                "Grazitti Site Development-Revamp11:00:00",
                "Content Marketing03:00:00",
                "Off-Page Content04:30:00",
                "SMM05:00:00",
                "Guest Blog01:30:00",
                "LiNC 1600:30:00",
                "Alteryx Inspire Conference 201625:30:00",
                "PPC02:00:00",
                "Training13:45:00");

        List<String> output=new ArrayList<String>();


        for (String extractedvalueItem : extractedvalue) {
            for(String hoursplusprojectItem : hoursplusprojects){
                if(hoursplusprojectItem.startsWith(extractedvalueItem)){
                    output.add(hoursplusprojectItem);
                }
            }
        }

        System.out.println("EXTRACTEDVALUE:");
        print(extractedvalue);
        System.out.println("HOURSPLUSPROJECTS:");
        print(hoursplusprojects);
        System.out.println("OUTPUT");
        print(output);
    }

    private static void print(List<String> list){
        for (String item : list) {
            System.out.println(item);
        }
        System.out.println();
    }
}

Here's a correct answer with some sample code:这是一些示例代码的正确答案:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;



public class Test {

    public static void main(String ...args){

        List<String> a = new ArrayList<String>();
        a.add("abc");
        a.add("def");
        a.add("ghi");
        a.add("jkl");

        List<String> b = new ArrayList<String>();
        b.add("abc 123");
        b.add("def 456");
        b.add("ghi 789");
        b.add("xyz 111");

        Map<String, String> map = new HashMap<String, String>();
        for (String item: b){
            map.put(strip(item), item);
        }

        Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
        while (iter.hasNext()){

            Entry<String, String> entry = iter.next();

            if (!a.contains(entry.getKey())){
                iter.remove();
            }
        }

        System.out.println(map.values());
    }

    private static String strip(String input){
        return input.split(" ")[0];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM