1

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

  • 1
    `Set` has no method `substring`, please post code that compiles. Besides that, have you looked at the documentation for `Set`? Maybe you could solve this with either `removeAll` or `retainAll` depending on what it is you are trying to achieve. – Joakim Danielson Jul 16 '19 at 10:55
  • Possible duplicate of https://stackoverflow.com/questions/18644579/getting-the-difference-between-two-sets – kidney Jul 16 '19 at 11:52
  • Yeah it same but, I need compare first set with combination, first 10 digit is account number, then next charachter 1 digit is code, then the rest of number is auto generated. And set 2 is data of account number, I need get all transaction from account number in set 2 – Ignatius Samuel Megis Jul 16 '19 at 15:09
  • I think, I must loop 2 set with substring from SET 1, then validate one by one, and if I did like that that need high nemory if file is large – Ignatius Samuel Megis Jul 16 '19 at 15:19

1 Answers1

2

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());
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52