我有一个数组列表,其值为:
IT基础设施
团队
Wave Analytics入门套件
博客文章
案例研究 信息图表
Grazitti网站开发 - 改造
和另一个数组列表,其值为:
Wave Analytics Starter Kit56:00:00
IT Infrastructure01:00:00
Teams08:00:00
博客文章21:30:00
案例研究02:30:00
InfoGraphics02:45:00
Grazitti Site Development-Revamp11:00:00
Content Marketing03:00:00
Off-Page Content04:30:00
SMM05:00:00
嘉宾Blog01:30:00
LiNC 1600:30:00
Alteryx Inspire Conference 201625:30:00
PPC02:00:00
Training13:45:00
我可以比较它们以获得输出:
IT Infrastructure01:00:00
Teams08:00:00
Wave Analytics Starter Kit56:00:00
博客文章21:30:00
案例研究02:30:00
InfoGraphics02:45:00
Grazitti Site Development-Revamp11:00:00
以下代码未提供所需的输出:
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));
}
答案 0 :(得分:1)
这是O(nm)的解决方案。 n和m是数组的大小。该解决方案适用于两个阵列具有您在问题中说明的结构的情况。
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);
}
答案 1 :(得分:0)
以下是一些示例代码的正确答案:
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];
}
}
答案 2 :(得分:0)
请检查以下解决方案。如果您有任何疑问,请询问。
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();
}
}