简体   繁体   中英

Removing Common Values from Two Array Lists

I have two array lists. The first array list contains the IDs that should be deleted from the second array list. The second array list does not contains only the IDs, rather it contains other information(tab separated).

ArrayList1 = [1000, 1001]
ArrayList2 = [1000     Yes     3     33,1001     No     No     22,1002     No     Yes     4,1003     No     No     13]

What I would like to do is that remove all elements in ArrayList2 that has the same IDs. So, the final output should be another arrayList as following: ArrayList2= [1002 No Yes 4,1003 No No 13].

Any idea on how to solve this problem.

You can try code below.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Test {
    public static void main(String args[]){
        Integer idArr[] =  {1000, 1001};
        String unitArr2[] = {"1000  Yes 3   33","1001   No  No  22","1002   No  Yes 4","1003    No  No  13"};

        ArrayList<String> list = new ArrayList<String>(Arrays.asList(unitArr2));

        for(int id: idArr){
            Iterator<String> iterator = list.iterator();
            while(iterator.hasNext()){
                String unit = iterator.next();
                if(Integer.parseInt(unit.split("\t")[0]) == id){
                    iterator.remove();
                    break;
                }
            }
        }

        for(String unit: list){
            System.out.println(unit);
        }
    }
}

Use ListIterator when you want to iterate and also remove the element. Following code should remove common values

    ListIterator<String> iter = list2.listIterator();
    while(iter.hasNext()){
        String value = iter.next();
        String [] tokens = value.split("\\s+");
        if(list1.contains(tokens[0].trim())){
            iter.remove();
        }
    }

Using Streams

List<String> filtered = list2.stream()
            .filter(x -> {
                String [] tokens = x.split("\\s+");
                if(list1.contains(tokens[0].trim())){return false;
                }else{return true;}
            }).collect(Collectors.toList());

If the first four digits of every element is the integer you want to check on, then iterate over the list and split every element with a regex by the character " " (whitespace).

Now you have list elements consisting of Object and you can be sure, that the first entry is an Integer. Simply check the integer then.

In pseudo code:

for (Object obj : ArrayList1) {
  String[] result = Regex.split (obj, " ");
  if (result [0] == myIdToCheck) 
     ArrayList2.remove (obj);
}

If you are using Java 8, you can filter a stream :

import java.util.*;
import java.util.stream.*;

public class Quick {
    private static boolean filter(List<Integer> list, String s) {
        boolean result = false;

        // TODO: improve error checking
        String[] tokens = s.split("\t");

        if (tokens.length > 0) {
            int i = Integer.parseInt(tokens[0]);
            result = ! list.contains(i); 
        }

        return result;
    }

    public static void main(String[] args) {
        // setup

        List<Integer> list1 = new ArrayList<>();
        list1.add(1000);
        list1.add(1001);

        List<String> list2 = new ArrayList<>();
        list2.add("1000\tYes\t3\t33");
        list2.add("1001\tNo\tNo\t22");
        list2.add("1002\tNo\tYes\t4");
        list2.add("1003\tNo\tNo\t13");

        // generate 'result' list
        List<String> result = list2.stream()
                                 .filter(s -> filter(list1,s))
                                 .collect(Collectors.toList());

        // print 'result' list
        for (String s : result) { System.out.println("TRACER " + s); }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM