Ignatius Samuel Megis
Ignatius Samuel Megis

Reputation: 185

How to compare two set then filter to new set with combination string?

I'm build some short code for compare 2 hashset.

SET 1 = noRek : [1234567892, 1234567891, 1234567890]

SET 2 = Source : [1234567890U0113, 1234567894B0111, 1234567890U0112, 1234567891B0111, 1234567890U0115, 1234567890U0114, 1234567892B0113, 1234567893B0111, 1234567890U0111, 1234567890B0111, 1234567892B0112, 1234567892B0111]

public class diff {
    public static void main(String args[]) {

        String filename = "C:\\abc.txt";
        String filename2 = "C:\\xyz.txt";
        HashSet<String> al = new HashSet<String>();
        HashSet<String> al1 = new HashSet<String>();
        HashSet<String> source = new HashSet<String>();
        HashSet<String> noRek = new HashSet<String>();
        HashSet<String> diff1 = new HashSet<String>();
        HashSet<String> diff2 = new HashSet<String>();
        String str = null;
        String str2 = null;
        Integer digitRek = 10;
        Integer digitTransaksi = 15;
        //GET REKDATA FROM TARGET
        try {
            String message = new Scanner(new File(filename2)).useDelimiter("\\Z").next();
            for (int i = 0; i < message.length(); i += digitRek) {
               noRek.add(message.substring(i, Math.min(i + digitRek, message.length())));
            }
            System.out.println("noRek : " + noRek);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            String message2 = new Scanner(new File(filename)).useDelimiter("\\Z").next();

            for (int i = 0; i < message2.length(); i += digitTransaksi) {
               source.add(message2.substring(i, Math.min(i + digitTransaksi, message2.length())));
            }
            System.out.println("Source : " + source);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (String str3 : source) {
            if (source.contains(noRek.substring(digitRek)) {
                diff1.add(str3);
            }

        }

       System.out.println("Final : " + diff1);

    }

I excpet the output of the set diff1 is like this

    SET 3 = [1234567890U0111, 1234567890U0112, 1234567890U0113,1234567890U0114, 1234567890U0115, 1234567890B0111, 1234567891B0111, 1234567892B0113, 1234567892B0112, 1234567892B0111]

but actual output is same like SET 2.

In simple way I need compare SET 2 with combination, first 10 digit is account number, then next charachter 1 digit is code, then the rest of number is auto generated. That's mean the length combination SET 2 is 15 digit, and combination SET 1 is 10 digit, then set 1 is data of account number, I need get all transaction from account number in set 2.

SET 1 is data all of account and SET 2 is data of transaction combination

Upvotes: 1

Views: 117

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 52088

You can solve this by using stream and filter

Set<String> diff1 = source.stream().filter(str -> {
    if (str.length() > 10) {
        String account = str.substring(0, 10);
        return noRek.contains(account);
    }
    return false;
}).collect(Collectors.toSet());

Upvotes: 2

Related Questions