【问题标题】:Find sub collection not adjacent but in the same order in java在java中查找不相邻但顺序相同的子集合
【发布时间】:2017-03-14 19:44:56
【问题描述】:

如何在 Java (8) 中找到一个集合(列表或集合)是否是另一个集合的子集,尽管元素不相邻,例如 [1,2,3,4,5] 作为大集合,如果我想搜索 [2,3,4] 返回 true,[2,5] 返回 true 但 [4,2] 返回 false 虽然 4 和 2 在集合中但顺序不同

是否有实用程序可以帮助我执行此操作? 或者一段代码正确地做到了这一点?

谢谢

【问题讨论】:

  • 你怎么看[1,2,3,4,1,2,3,4]。它按顺序包含[3,4,2],这是真的吗?

标签: java search collections


【解决方案1】:

如果 a 包含 b,则此函数返回 true。

此函数将集合转换为数组,如果您的集合类没有实现.toArray() 函数,它将不起作用!

public class CollectionUtils {
    private CollectionUtils() {
    }

    /**
     * @return true if A contains B in order
     */
    public static <T> boolean checkAcontainsB(Collection<T> a, Collection<T> b) {
        if (a == null || b == null || b.size()>a.size()) {
            return false;
        }
        if (b.isEmpty()) {
            return true;
        }
        final Object[] aElements = a.toArray();
        final Object[] bElements = b.toArray();

        for (int i = 0; i < aElements.length; i++) {

            int bIndex = 0;
            for(int j = i; j< aElements.length; j++) {
                if(aElements[j] == bElements[bIndex]) {
                    bIndex++;
                    if(bIndex>=bElements.length) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

}

你可以测试一下:

@Test
public void test() {
    Assert.assertFalse(CollectionUtils.contains(Arrays.asList(1,2,3,4), Arrays.asList(2,3,4,5)));
    Assert.assertTrue(CollectionUtils.contains(Arrays.asList(1,2,3,4), Arrays.asList(2,3,4)));
    Assert.assertTrue(CollectionUtils.contains(Arrays.asList(1,2,3,4), Arrays.asList(2,4)));
    Assert.assertTrue(CollectionUtils.contains(Arrays.asList(1,2,3,4,1,2,3,4), Arrays.asList(3,4,2)));
    Assert.assertFalse(CollectionUtils.contains(Arrays.asList(1,2,3,4), Arrays.asList(2,3,4,5,6)));
}

【讨论】:

    【解决方案2】:

    您可以轻松使用 Collections 提供的一种实用方法,该方法完全符合您的意图。

        Collections.disjoint(c1, c2)
    

    如果传递的两个集合没有任何共同项,则上述方法返回 true,反之亦然。正是你想要的。

    【讨论】:

    • 这不是你需要做的吗?
    【解决方案3】:

    尚未完全测试,但您可以尝试类似的方法,

        int[] x = {1,2,3,4,5};
        int[] y = {2,5};
        int yIndex = 0;
    
        for(int i: x){
            if(y[yIndex] == i){
                yIndex++;
                if(yIndex >= y.length){
                    break;
                }
            }
        }
    
        System.out.println(yIndex == y.length ? "Match" : "Not Match");
    

    【讨论】:

      【解决方案4】:

      我不知道有任何通用解决方案可以解决您的问题,但您可以在下面找到针对您的问题的定制解决方案。

      public static boolean containsArray(int[] a, int[] s) {
          if (a == null || s == null) return false;
          if (a.length < s.length) return false;
      
          int i = -1;
          for (int current : s) {
              do {
                  i++;
                  if (i == a.length) {
                      return false;
                  }
              } while (a[i] != current);
          }
      
          return true;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2023-03-15
        相关资源
        最近更新 更多