如何比较两个集合然后使用组合字符串过滤到新集合?

时间:2019-07-16 10:41:35

标签: java hashset

我正在为比较2哈希集构建一些短代码。

  

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

     

SET 2 =来源:[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);

    }

我喜欢设置diff1的输出,就像这样

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

但实际输出与SET 2相同。

以简单的方式,我需要将SET 2与组合进行比较,第一个10位数字是帐号,然后下一个字符1位数字是代码,然后自动生成其余的数字。这意味着SET 2的长度组合为15位,而SET 1的组合长度为10位,则set 1为帐号数据,我需要从set 2的帐号中获取所有交易。

SET 1是帐户和 SET 2是交易组合的数据

1 个答案:

答案 0 :(得分:2)

您可以使用流和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());
相关问题