文本中间的交叉点

时间:2015-04-15 09:29:14

标签: java

您好我正在尝试比较两个数组列表。像这样:

public class intersection {
 public static void main(String... args) throws Exception {

    List<String> list1 = new ArrayList<String>(Arrays.asList("This is a test example to help me learn hashsets"));
    List<String> list2 = new ArrayList<String>(Arrays.asList("test example", "Not returned"));

    System.out.println(new intersection().intersection(list1, list2));

}


public <T> List<T> intersection(List<T> list1, List<T> list2) {
    List<T> list = new ArrayList<T>();

    for (T t : list1) {
        if(list2.contains(t)) {
            list.add(t);
        }
    }

    return list;
}
}

我想要&#34;测试示例&#34;返回,因为它在两个arrayLists中的某个地方。抱歉忘了说arrayLists的内容无法更改是否还有这样做?谢谢

3 个答案:

答案 0 :(得分:2)

您需要检查单词

public class intersection {

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

        List<String> list1 = new ArrayList<String>(Arrays.asList("This is a test example to help me learn hashsets".split("\\s+")));
        List<String> list2 = new ArrayList<String>(Arrays.asList("test example Not returned".split("\\s+")));

        System.out.println(new intersection().check(list1, list2));

    }

    public <T> List<T> check(List<T> list1, List<T> list2) {
        List<T> list = new ArrayList<T>();

        for (T t : list1) {
            if (list2.contains(t)) {
                list.add(t);
            }
        }

        return list;
    }
}

或者,您可以选择在整个字符串中搜索短语

公共班交叉{/ p>

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

    String s1 = "This is a test example to help me learn hashsets";
    String[] s2 = new String[] { "test example", "Not returned" };

    System.out.println(new intersection().check(s1, s2));

}

public List<String> check(String s1, String[] s2) {
    List<String> list = new ArrayList<>();

    for (String s : s2) {
        if (s1.contains(s)) {
            list.add(s);
        }
    }

    return list;
}

}

答案 1 :(得分:0)

有一个错误:

List<String> list1 = new ArrayList<String>(Arrays.asList("This is a test example to help me learn hashsets"));

这里的列表只包含一个元素“这是一个帮助我学习hashsets的测试示例”是一个元素。

将字符串除以

 public class intersection {
   public static void main(String... args) throws Exception {

    List<String> list1 = new ArrayList<String>(Arrays.asList("This is a ","test example"," to help me learn hashsets"));
    List<String> list2 = new ArrayList<String>(Arrays.asList("test example", "Not returned"));

    System.out.println(new intersection().intersection(list1, list2));

  }


  public <T> List<T> intersection(List<T> list1, List<T> list2) {
    List<T> list = new ArrayList<T>();

    for (T t : list1) {
        if(list2.contains(t)) {
            list.add(t);
        }
    }

    return list;
  }
}

或者改变交集的逻辑以使用子串。

修改

public <T> List<T> intersection(List<T> list1, List<T> list2) {
    List<T> list = new ArrayList<T>();

    for (T t : list2) {
        for(T l: list1){
            if(((String)l).contains((CharSequence)t)){
                list.add(t);
            }

        }
    }

    return list;
}

答案 2 :(得分:0)

使用Java 8流:

public static <T> List<T> intersection(List<T> list1, List<T> list2) {
    List<T> list = new ArrayList<>();
    Predicate<? super T> con = (t) -> {
        Predicate<? super T> p = (u) -> u.toString().contains((CharSequence) t);
        return list1.stream().anyMatch(p);
    };
    list2.stream().filter(con).forEach(list::add);

    return list;
}