Partially match strings in case of List.contains(String)

后端 未结 9 881
夕颜
夕颜 2020-12-10 11:23

I have a List

List list = new ArrayList();
list.add(\"ABCD\");
list.add(\"EFGH\");
list.add(\"IJ KL\")         


        
9条回答
  •  青春惊慌失措
    2020-12-10 11:59

    This is not a direct answer to the given problem. But I guess this answer will help someone to compare partially both given and the elements in a list using Apache Commons Collections.

    final Equator equator = new Equator() {
            @Override
            public boolean equate(String o1, String o2) {
                final int i1 = o1.lastIndexOf(":");
                final int i2 = o2.lastIndexOf(":");
                return o1.substring(0, i1).equals(o2.substring(0, i2));
            }
    
            @Override
            public int hash(String o) {
                final int i1 = o.lastIndexOf(":");
                return o.substring(0, i1).hashCode();
            }
        };
        final List list = Lists.newArrayList("a1:v1", "a2:v2");
        System.out.println(IteratorUtils.matchesAny(list.iterator(), new EqualPredicate("a2:v1", equator)));
    

提交回复
热议问题