不匹配Java中的数组列表中的产品名称

时间:2015-03-03 07:40:37

标签: java arraylist

我有两个数组列表。

capturedForeginProduct :外国产品名称

capturedLocalProducts :本地产品名称

capturedForeginProduct 数组列表包含以下外国产品名称。

  

华盛顿苹果饮料

     

Token Apple Drink

     

Skinner Apple Drink

capturedLocalProducts 数组列表包含以下本地产品名称。

  

SUNFRESH Apple Drink RTS 1L

     

APPY FIZZ闪亮苹果饮料

     

APPY Apple Drink 250ml宠物瓶

我使用以下代码段将 capturedForeginProduct 数组列表中的产品名称与 capturedLocalProducts 数组列表进行匹配。

if(capturedForeginProduct.get(i).equals(capturedLocalProducts.get(j)))  {

但它不匹配任何产品。基本上我的最终结果应该如下。

  

华盛顿苹果饮料

     

Token Apple Drink

     

Skinner Apple Drink

应该匹配,

  

SUNFRESH Apple Drink RTS 1L

     

APPY FIZZ闪亮苹果饮料

     

APPY Apple Drink 250ml宠物瓶

因为每个产品都包含“ Apple ”字样。我不介意它是否包含大写字母但是如果这个单词可用则它应该匹配。

这是我的代码,以执行此特定任务。

for (int i = 0; i < capturedForeginProduct.size(); i++) {

    for (int j = 0; j < capturedLocalProducts.size(); j++) {
        //   if(capturedForeginProduct.get(i).contains("Garlic")) {
        //  if (Pattern.compile(Pattern.quote(capturedLocalProducts.get(j)), Pattern.CASE_INSENSITIVE).matcher(capturedForeginProduct.get(i)).find() || capturedLocalProducts.get(j).toLowerCase().contains(capturedForeginProduct.get(i).toLowerCase())) {

        if (capturedForeginProduct.get(i).equals(capturedLocalProducts.get(j))) {
            //  if(capturedLocalProducts.get(i).equals("SUNFRESH Mango Drink RTS 1L"))  {

            log.debug("Matching Second Chance .. : " + "\t" + capturedForeginProduct.get(i) + "\t" + capturedLocalProducts.get(j));
            firstForeignProducts.add(capturedForeginProduct.get(i));
            firstLocalProducts.add(capturedLocalProducts.get(j));
        } else {
            log.debug("Un Matching Second Chance .. : " + "\t" + capturedForeginProduct.get(i) + "\t" + capturedLocalProducts.get(j));
        }
    }
}

感谢。

2 个答案:

答案 0 :(得分:0)

您需要拆分名称并比较每个部分。

String foreign = capturedForeginProduct.get(i);
String local = capturedLocalProducts.get(j);

String[] foreignWords = foreign.split(" ");
String[] localWords = local.split(" ");

最后迭代两个数组并比较每个单词。

答案 1 :(得分:0)

您可以检查两个句子中是否有相互的单词如下:

String sentence1 = "Washington Apple Drink";
String sentence2 = "SUNFRESH Apple Drink RTS 1L";

List<String> sen1 = Arrays.asList(sentence1.split(" "));
List<String> sen2 = Arrays.asList(sentence2.split(" "));

for (String s : sen1) {
    if (sen2.contains(s)) {
        System.out.println("The word " + s + " appears in both sentences");
    }
}

<强>输出

The word Apple appears in both sentences
The word Drink appears in both sentences