java: List of String Arrays and remove

前端 未结 4 682
我在风中等你
我在风中等你 2021-01-11 19:01

In a test like this:

    @Test
    public void test() {
        List l = new LinkedList();
        l.add(new String [] {\"tes         


        
相关标签:
4条回答
  • 2021-01-11 19:17

    By the way, Java 8 introduces a new removeIf method that removes all of the elements of this collection that satisfy the given predicate

    It can be used to easily remove an array of strings from the list:

    List<String[]> l = new LinkedList<String[]>();
    l.add(new String [] {"test", "123"});
    l.add(new String [] {"test", "456"});
    l.add(new String [] {"test", "789"});
    
    String[] newArray = new String[] {"test", "456"};
    l.removeIf(array -> Arrays.equals(array, newArray));
    
    0 讨论(0)
  • 2021-01-11 19:24

    Using Guava, there is. You will need to implement an Equivalence<String[]>:

    public final class MyEquivalence
        extends Equivalence<String[]>
    {
        @Override
        protected boolean doEquivalent(final String[] a, final String[] b)
        {
            return Arrays.equals(a, b);
        }
    
        @Override
        protected int doHash(final String[] t)
        {
            return Arrays.hashCode(t);
        }
    }
    

    You would then need to have your list being a List<Equivalence.Wrapper<String[]>>, and insert/remove/etc using your Equivalence's .wrap() method:

    final Equivalence<String[]> eq = new MyEquivalence();
    list.add(eq.wrap(oneValue));
    list.remove(eq.wrap(anotherValue));
    

    Use Guava. Repeat after me. Use Guava :p

    0 讨论(0)
  • 2021-01-11 19:31

    The List method are often based on the Object method equals(Object o) which compare the object's reference by default. You may store the tab if you want remove it later, or create your own class and override equals(Object o) ;)

        @Test
        public void test() {
            List<String[]> l = new LinkedList<String[]>();
            l.add(new String [] {"test", "123"});
            String[] tab = new String [] {"test", "456"};
            l.add(tab);
            l.add(new String [] {"test", "789"});
    
            assertEquals(3, l.size());
    
            l.remove(tab);
    
            assertEquals(2, l.size());
        }
    
    0 讨论(0)
  • 2021-01-11 19:32

    You are creating a new Object reference and passing it to the remove() method . From the data you posted , it looks like you can create a custom class with two properties and override its equals() and hashCode() instead of storing them as String[] OR Keep the reference to the String[] object inserted and use that reference to remove.

    0 讨论(0)
提交回复
热议问题