如何从两个hashMap检索公用密钥并将结果放入ArrayList

时间:2018-06-27 12:46:50

标签: java list for-loop arraylist hashmap

我正在尝试从两个hashMap中检索公用的“键”,并将结果放入ArrayList中。为此,我提出了以下逻辑。

    List<String> commonList = new ArrayList<String>();
    for(String key : mapA.keySet()) {
        if(mapB.get(key) !=null ) {
            if(mapA.get(key).equals(mapB.get(key))) {
                commonList.add(key);
            }
        }
    }

这是我在地图中添加的数据:mapA & mapB

mapA.put("abc_ap_bank_accounts,XYZ","6372,43272.6648842593");
mapA.put("abc_ap_bank_accounts_secured_attributes,XYZ","6372,43272.6648829051");
mapA.put("abc_ap_checks,XYZ","56207,43272.676245");
mapA.put("abc_ap_holds,XYZ","9523,43272.6735710995");
mapA.put("abc_ap_holds_dh,XYZ","14,43272.6735710995");
mapA.put("abc_ap_invoice_distributions,XYZ","1573699,43272.6735710995");
mapA.put("abc_ap_invoice_distributions_dh,XYZ","9,43272.6735710995");
mapA.put("abc_ap_invoices,XYZ","141096,43272.6735710995");
mapA.put("abc_ap_invoices_dh,XYZ","47,43272.6735710995");
mapA.put("abc_ap_payment_history,XYZ","454441,43272.6763922106");
mapA.put("abc_ap_payment_methods,XYZ","41,43193.0547537269");
mapA.put("abc_ap_payment_schedules,XYZ","141099,43272.6735710995");
mapA.put("abc_ap_payment_schedules_dh,XYZ","47,43272.6735710995");
mapA.put("abc_ap_terms,XYZ","73,43193.0547620718");
mapA.put("abc_ar_cash_receipts,XYZ","198815,43272.6634247337");
mapA.put("abc_ar_collectors,XYZ","8,43192.4939946643");
mapA.put("abc_ar_customer_contacts,XYZ","4978,43272.6613442824");
mapA.put("abc_ar_customer_site_uses,XYZ","71313,43272.6617516204");
mapA.put("abc_ar_customer_sites,XYZ","38740,43272.6617516204");
mapA.put("abc_ar_customers,XYZ","12521,43272.6617516204");
mapA.put("abc_ar_invoice_distributions,XYZ","2808948,43272.6709406713");
mapA.put("abc_ar_invoice_distributions_dh,XYZ","31412,43272.6709406713");
mapA.put("abc_ar_invoices,XYZ","574107,43272.6709406713");
mapA.put("abc_ar_invoices_dh,XYZ","302,43272.6709406713");
mapA.put("abc_ar_payment_history,XYZ","1408356,43272.6733646759");
mapA.put("abc_ar_payment_schedules,XYZ","717509,43272.6709406713");
mapA.put("abc_ar_payment_schedules_dh,XYZ","8265,43272.6709406713");
mapA.put("abc_ar_receipt_methods,XYZ","2091,43272.660792419");
mapA.put("abc_ar_terms,XYZ","280,43273.3107522222");
mapA.put("abc_bank_branches,XYZ","2876,43272.6490289236");
mapA.put("abc_fa_adjustments,XYZ","861656,43273.1559078472");
mapA.put("abc_fa_adjustments_dh,XYZ","489,43260.0309204977");
mapA.put("abc_fa_asset_books,XYZ","418170,43273.1572042245");
mapA.put("abc_fa_asset_books_dh,XYZ","235,43260.0319078819");
mapA.put("abc_fa_asset_history,XYZ","144660,43273.3568460185");
mapA.put("abc_fa_asset_invoices,XYZ","140527,43277.2888911227");
mapA.put("abc_fa_asset_keywords,XYZ","9,43277.2844361806");
mapA.put("abc_fa_assets,XYZ","140979,43273.3568460185");
mapA.put("abc_fa_assets_dh,XYZ","227,43260.0300720833");
mapA.put("abc_fa_book_controls,XYZ","356,43273.1531339583");
mapA.put("abc_fa_categories,XYZ","331,43231.2057896412");
mapA.put("abc_fa_category_books,XYZ","11137,43231.2057896412");
mapA.put("abc_fa_deprn_detail,XYZ","3023721,43273.1572042245");
mapA.put("abc_fa_deprn_detail_dh,XYZ","792,43273.1572042245");
mapA.put("abc_fa_deprn_summary,XYZ","2978509,43273.1572042245");
mapA.put("abc_fa_deprn_summary_dh,XYZ","792,43273.1572042245");
mapA.put("abc_fa_distribution_history,XYZ","166081,43273.3568460185");
mapA.put("abc_fa_locations,XYZ","849,43271.4159192477");
mapA.put("abc_fa_lookups,XYZ","6776,43192.4939880556");
mapA.put("abc_fa_methods,XYZ","1930,43192.494011713");
mapA.put("abc_fa_periods,XYZ","9661,43273.270426713");
mapA.put("abc_fa_retirements,XYZ","62190,43273.1558409838");
mapA.put("abc_fa_transactions,XYZ","582535,43273.1545858449");
mapA.put("abc_fa_transactions_dh,XYZ","463,43260.0292952546");
mapA.put("abc_fnd_currencies,XYZ","257,43268.6356319097");
mapA.put("abc_fnd_descriptive_flex_column_usages,XYZ","30251,43268.6169408218");
mapA.put("abc_fnd_descriptive_flex_context_labels,XYZ","816,43268.6169408218");
mapA.put("abc_fnd_descriptive_flexs,XYZ","3000,43268.6169408218");
mapA.put("abc_fnd_document_attachments,XYZ","141323,43272.649096169");
mapA.put("abc_fnd_document_categories,XYZ","278,43268.635933831");
mapA.put("abc_fnd_document_category_usages,XYZ","1037,43268.635933831");
mapA.put("abc_fnd_document_entities,XYZ","440,43268.6358883796");
mapA.put("abc_fnd_document_media,XYZ","3044,43272.649096169");
mapA.put("abc_fnd_document_sequence_categories,XYZ","42945,43272.6483789468");
mapA.put("abc_fnd_flex_value_hierarchy,XYZ","27847,43272.6487910069");
mapA.put("abc_fnd_flex_value_sets,XYZ","21808,43272.6487910069");
mapA.put("abc_fnd_flex_value_sets,CBS","936,43180.2485385648");
mapA.put("abc_fnd_flex_values,XYZ","511466,43272.6487910069");
mapA.put("abc_fnd_flex_values,CBS","643030,43180.2485385648");
mapA.put("abc_fnd_flex_values_tl,CBS","643012,43180.2485385648");
mapA.put("abc_fnd_flex_values_tl,XYZ","5626126,43272.6487910069");
mapA.put("abc_fnd_id_flex_segments,CBS","117,43180.2490770023");
mapA.put("abc_fnd_id_flex_segments,XYZ","937,43268.6168802431");
mapA.put("abc_fnd_id_flex_structures,CBS","16,43180.2490770023");
mapA.put("abc_fnd_id_flex_structures,XYZ","219,43268.6168802431");
mapA.put("abc_fnd_lookup_types,CBS","5445,43180.2485133681");
mapA.put("abc_fnd_lookup_types,XYZ","20954,43268.6357395833");
mapA.put("abc_fnd_lookup_values,XYZ","310967,43272.648671331");
mapA.put("abc_fnd_lookup_values,CBS","48696,43180.2485133681");
mapA.put("abc_fnd_profile_option_values,XYZ","25654,43272.6486771875");
mapA.put("abc_fnd_profile_option_values,CBS","1229,43180.248486331");
mapA.put("abc_fnd_profile_options,CBS","694,43180.248486331");
mapA.put("abc_fnd_responsibility,XYZ","3296,43271.3555948032");
mapA.put("abc_fnd_segment_attribute_values,CBS","38,43180.2490770023");
mapA.put("abc_fnd_segment_attribute_values,XYZ","1743,43268.6168802431");
mapA.put("abc_fnd_territories,XYZ","256,43268.6358083449");
mapA.put("abc_fnd_user,XYZ","8045,43272.6486153935");
mapA.put("abc_fnd_user_responsibility,XYZ","51583,43272.6491750463");
mapA.put("abc_geographies,CBS","248,43180.2484406366");
mapA.put("abc_geographies,XYZ","4762,43268.635808125");
mapA.put("abc_gl_alloc_batches,XYZ","133,43272.8261837847");
mapA.put("abc_gl_alloc_formula_lines,XYZ","24940,43272.8261837847");
mapA.put("abc_gl_alloc_formulas,XYZ","4988,43272.8261837847");
mapA.put("abc_gl_auto_alloc_batches,XYZ","34,43268.6176857523");
mapA.put("abc_gl_auto_alloc_sets,XYZ","9,43268.6176857523");
mapA.put("abc_gl_balances,CBS","460591,43180.426733125");
mapA.put("abc_gl_balances,XYZ","531165150,43272.7578364699");
mapA.put("abc_gl_coa_value_sets,CBS","140,43180.2500551736");
mapA.put("abc_gl_coa_value_sets,XYZ","121,43272.6507386227");
mapA.put("abc_gl_coa_values,CBS","3129452,43180.2500551736");
mapA.put("abc_gl_coa_values,XYZ","811785,43272.6507386227");
mapA.put("abc_gl_code_combinations,CBS","68076,43180.2496125116");
mapA.put("abc_gl_code_combinations,XYZ","3451401,43272.6526675232");
mapA.put("abc_gl_daily_conversion_types,CBS","5,43180.2494641898");
mapA.put("abc_gl_daily_conversion_types,XYZ","72,43268.6184428241");
mapA.put("abc_gl_daily_rates,XYZ","122748758,43272.6507345139");
mapA.put("abc_gl_daily_rates,CBS","30318,43180.2494641898");
mapA.put("abc_gl_import_references,XYZ","187095005,43272.6912625694");
mapA.put("abc_gl_import_references,CBS","68692721,43223.6425953125");
mapA.put("abc_gl_import_references_dh,CBS","2419575,43223.6425953125");
mapA.put("abc_gl_import_references_dh,XYZ","2,43145.1443791435");
mapA.put("abc_gl_je_batches,XYZ","2731346,43272.7667763542");
mapA.put("abc_gl_je_batches,CBS","1165,43223.6425953125");
mapA.put("abc_gl_je_batches_dh,XYZ","3382,43272.6912625694");
mapA.put("abc_gl_je_batches_dh,CBS","63,43223.6425953125");
mapA.put("abc_gl_je_categories_tl,CBS","117,43180.2494409607");
mapA.put("abc_gl_je_categories_tl,XYZ","3102,43268.6176579745");
mapA.put("abc_gl_je_headers,XYZ","20096450,43272.7667763542");
mapA.put("abc_gl_je_headers,CBS","4464,43223.6425953125");
mapA.put("abc_gl_je_headers_dh,CBS","100,43223.6425953125");
mapA.put("abc_gl_je_headers_dh,XYZ","592840,43272.6912625694");
mapA.put("abc_gl_je_lines,XYZ","1187455843,43272.7667763542");
mapA.put("abc_gl_je_lines,CBS","3620200,43223.6425953125");
mapA.put("abc_gl_je_lines_dh,XYZ","87553244,43272.6912625694");
mapA.put("abc_gl_je_lines_dh,CBS","69399,43223.6425953125");
mapA.put("abc_gl_je_sources_tl,CBS","43,43180.2494381713");
mapA.put("abc_gl_je_sources_tl,XYZ","3586,43268.6176533565");
mapA.put("abc_gl_ledger_balancing_segments,XYZ","482,43268.6178050579");
mapA.put("abc_gl_ledger_periods,XYZ","49005,43272.6509033681");
mapA.put("abc_gl_ledger_periods,CBS","585,43223.6233088079");
mapA.put("abc_gl_ledger_relationships,CBS","47,43180.2494641088");
mapA.put("abc_gl_ledger_relationships,XYZ","411,43268.6178050579");
mapA.put("abc_gl_ledger_set_assignments,XYZ","843,43268.6178050579");
mapA.put("abc_gl_ledgers,XYZ","418,43268.6178050579");
mapA.put("abc_gl_ledgers,CBS","53,43180.2494641088");
mapA.put("abc_gl_periods,XYZ","678,43189.7832918403");
mapA.put("abc_gl_periods,CBS","65,43223.6233088079");
mapA.put("abc_gl_subledger_distributions,XYZ","34275694,43272.6570907176");
mapA.put("abc_gl_subledger_distributions,CBS","64292586,43224.3809484606");
mapA.put("abc_gl_subledger_distributions_dh,CBS","1293981,43224.3809484606");
mapA.put("abc_gl_subledger_lines,XYZ","14891251,43272.6570907176");
mapA.put("abc_gl_subledger_lines,CBS","34456050,43224.3809484606");
mapA.put("abc_gl_subledger_lines_dh,CBS","721841,43224.3809484606");
mapA.put("abc_gl_subledger_references,CBS","44491269,43224.395238206");
mapA.put("abc_gl_subledger_references,XYZ","85762915,43276.649014456");
mapA.put("abc_hr_assignments,XYZ","561102,43272.7518781019");
mapA.put("abc_hr_assignments_dh,XYZ","8,43242.6042654977");
mapA.put("abc_hr_locations,XYZ","1472,43277.5080027431");
mapA.put("abc_hr_locations_dh,XYZ","1,43180.6461068171");
mapA.put("abc_hr_people,XYZ","561168,43272.7518781019");
mapA.put("abc_hr_people_dh,XYZ","9,43242.6042654977");
mapA.put("abc_hr_pos_structure_elements,XYZ","601,43269.4130085301");
mapA.put("abc_hr_position_structures,XYZ","32,43269.4130085301");
mapA.put("abc_hr_positions,XYZ","449,43269.4130085301");
mapA.put("abc_internal_bank_account_documents,XYZ","252,43272.6610732986");
mapA.put("abc_internal_bank_account_uses,XYZ","1546,43272.7626210532");
mapA.put("abc_internal_bank_accounts,XYZ","1542,43272.7611923843");
mapA.put("abc_internal_bank_accounts_secured_attributes,XYZ","1542,43272.7611923843");
mapA.put("abc_inv_bill_of_materials,XYZ","1124,43269.537615544");
mapA.put("abc_inv_bom_components,XYZ","1892,43269.537615544");
mapA.put("abc_inv_bom_resources,XYZ","38,43268.8932581944");
mapA.put("abc_inv_categories,XYZ","7122,43209.451384456");
mapA.put("abc_inv_item_categories,XYZ","7421625,43272.6554378009");
mapA.put("abc_inv_item_cost_details,XYZ","2449309,43272.6589800926");
mapA.put("abc_inv_item_costs,XYZ","2475153,43272.6589800926");
mapA.put("abc_inv_item_revisions,XYZ","2446153,43272.6585068634");
mapA.put("abc_inv_items,XYZ","2458126,43272.6554378009");
mapA.put("abc_inv_material_transaction_accounts,XYZ","3407129,43272.6614916319");
mapA.put("abc_inv_material_transactions,XYZ","1884933,43272.6614916319");
mapA.put("abc_inv_mtl_transaction_lot_numbers,XYZ","1051994,43272.6614916319");
mapA.put("abc_inv_onhand_quantites,XYZ","35265,43272.6630184722");
mapA.put("abc_inv_parameters,XYZ","960,43272.4247054976");
mapA.put("abc_inv_subinventories,XYZ","2610,43272.4247448727");
mapA.put("abc_inv_units_of_measure,XYZ","110,43192.3447800925");
mapA.put("abc_inv_uom_conversions,XYZ","2936,43192.4352418982");
mapA.put("abc_legal_entities,XYZ","362,43268.6359473032");
mapA.put("abc_legal_entities,CBS","152,43180.2485615972");
mapA.put("abc_legal_entity_registrations,CBS","304,43180.2485615972");
mapA.put("abc_legal_entity_registrations,XYZ","382,43268.6359473032");
mapA.put("abc_om_order_headers,XYZ","78591,43272.6649190509");
mapA.put("abc_om_order_holds,XYZ","182054,43272.6689071065");
mapA.put("abc_om_order_line_history,XYZ","116389,43272.6649190509");
mapA.put("abc_om_order_lines,XYZ","430120,43272.6649190509");
mapA.put("abc_om_price_list_line_attributes,XYZ","537347,43273.4698067477");
mapA.put("abc_om_price_list_lines,XYZ","537460,43273.4698067477");
mapA.put("abc_om_price_list_qualifiers,XYZ","983,43273.4698067477");
mapA.put("abc_om_price_lists,XYZ","1110,43273.4698067477");
mapA.put("abc_om_transaction_types,XYZ","7080,43270.5508769444");
mapA.put("abc_operating_units,XYZ","307,43268.6360601505");
mapA.put("abc_organizations,XYZ","1177,43272.4188616088");
mapA.put("abc_po_action_history,XYZ","168049,43272.6713592014");
mapA.put("abc_po_agents,XYZ","459,43271.6301501505");
mapA.put("abc_po_distributions,XYZ","147056,43272.6676102778");
mapA.put("abc_po_distributions_dh,XYZ","295,43263.7643565741");
mapA.put("abc_po_headers,XYZ","50561,43272.6676102778");
mapA.put("abc_po_headers_dh,XYZ","8,43208.2975749884");
mapA.put("abc_po_lines,XYZ","138843,43272.6676102778");
mapA.put("abc_po_lines_dh,XYZ","96,43272.6676102778");
mapA.put("abc_po_releases,XYZ","675,43272.6676102778");
mapA.put("abc_po_requisition_distributions,XYZ","5098,43272.6653532755");
mapA.put("abc_po_requisition_distributions_dh,XYZ","7,43213.9294613889");
mapA.put("abc_po_requisition_headers,XYZ","4157,43272.6653532755");
mapA.put("abc_po_requisition_headers_dh,XYZ","2,43213.9294613889");
mapA.put("abc_po_requisition_lines,XYZ","4786,43272.6653532755");
mapA.put("abc_po_requisition_lines_dh,XYZ","2,43213.9294613889");
mapA.put("abc_po_vendor_bank_accounts,XYZ","6622,43272.6648829051");
mapA.put("abc_po_vendor_contacts,XYZ","3432,43272.6647480556");
mapA.put("abc_po_vendor_sites,XYZ","17366,43272.6615350694");
mapA.put("abc_po_vendors,XYZ","6840,43272.6615350694");
mapA.put("abc_po_vendors_secured_attributes,XYZ","6840,43272.6615350694");
mapA.put("abc_rcv_shipment_headers,XYZ","63776,43272.6619233333");
mapA.put("abc_rcv_shipment_headers_dh,XYZ","3,43166.9666005787");
mapA.put("abc_rcv_shipment_lines,XYZ","163201,43272.6619233333");
mapA.put("abc_rcv_subledger_lines,XYZ","719678,43272.6638612269");
mapA.put("abc_rcv_transactions,XYZ","334263,43272.6619233333");
mapA.put("abc_tax_accounts,XYZ","3836,43276.6228216088");
mapA.put("abc_tax_codes,XYZ","3046,43276.4820179977");
mapA.put("abc_tax_lines,XYZ","1336810,43272.678131412");
mapA.put("abc_tax_party_profiles,XYZ","623791,43272.6781120949");
mapB.put("abc_ap_bank_accounts_secured_attributes,XYZ","6372,43272.6648829051");
mapB.put("abc_ap_checks,XYZ","56039,43272.676245");
mapB.put("abc_ap_holds,XYZ","9486,43272.6735710995");
mapB.put("abc_ap_holds_dh,XYZ","6,43168.7454254745");
mapB.put("abc_ap_invoice_distributions,XYZ","1568682,43272.6735710995");
mapB.put("abc_ap_invoice_distributions_dh,XYZ","1,43193.0607325115");
mapB.put("abc_ap_invoices,XYZ","137513,43250.3191355903");
mapB.put("abc_ap_invoices_dh,XYZ","32,43193.0607325115");
mapB.put("abc_ap_payment_history,XYZ","452474,43272.6763922106");
mapB.put("abc_ap_payment_methods,XYZ","41,43193.0547537269");
mapB.put("abc_ap_payment_schedules,XYZ","140906,43272.6735710995");
mapB.put("abc_ap_terms,XYZ","73,43193.0547620718");
mapB.put("abc_ar_cash_receipts,XYZ","198605,43272.6634247337");
mapB.put("abc_ar_collectors,XYZ","8,43192.4939946643");
mapB.put("abc_ar_customer_contacts,XYZ","4978,43272.6613442824");
mapB.put("abc_ar_customer_site_uses,XYZ","71231,43272.6617516204");
mapB.put("abc_ar_customer_sites,XYZ","38727,43272.6617516204");
mapB.put("abc_ar_customers,XYZ","12515,43272.6617516204");
mapB.put("abc_ar_invoice_distributions,XYZ","2779621,43262.7610439236");
mapB.put("abc_ar_invoice_distributions_dh,XYZ","26207,43193.8544045255");
mapB.put("abc_ar_invoices,XYZ","573853,43272.6709406713");
mapB.put("abc_ar_invoices_dh,XYZ","235,43193.8544045255");
mapB.put("abc_ar_payment_history,XYZ","21194,43236.3538011806");
mapB.put("abc_ar_payment_schedules,XYZ","717113,43272.6709406713");
mapB.put("abc_ar_payment_schedules_dh,XYZ","6773,43208.2936282523");
mapB.put("abc_ar_receipt_methods,XYZ","2090,43272.660792419");
mapB.put("abc_ar_terms,XYZ","280,43273.3107522222");
mapB.put("abc_bank_branches,XYZ","2875,43272.6490289236");
mapB.put("abc_fa_adjustments,XYZ","861031,43273.1559078472");
mapB.put("abc_fa_adjustments_dh,XYZ","426,43100.865825625");
mapB.put("abc_fa_asset_books,XYZ","417647,43273.1572042245");
mapB.put("abc_fa_asset_books_dh,XYZ","211,43100.8665157407");
mapB.put("abc_fa_asset_history,XYZ","144647,43273.3568460185");
mapB.put("abc_fa_asset_invoices,XYZ","140409,43260.0309616204");
mapB.put("abc_fa_asset_keywords,XYZ","9,43277.2844361806");
mapB.put("abc_fa_assets,XYZ","140838,43260.0300720833");
mapB.put("abc_fa_assets_dh,XYZ","209,43100.8654389931");
mapB.put("abc_fa_book_controls,XYZ","368,43273.1531339583");
mapB.put("abc_fa_categories,XYZ","331,43231.2057896412");
mapB.put("abc_fa_category_books,XYZ","11137,43231.2057896412");
mapB.put("abc_fa_deprn_detail,XYZ","3022367,43273.1572042245");
mapB.put("abc_fa_deprn_detail_dh,XYZ","261,43193.8467169444");
mapB.put("abc_fa_deprn_summary,XYZ","2977196,43273.1572042245");
mapB.put("abc_fa_deprn_summary_dh,XYZ","261,43193.8467169444");
mapB.put("abc_fa_distribution_history,XYZ","166068,43273.3568460185");
mapB.put("abc_fa_locations,XYZ","849,43271.4159192477");
mapB.put("abc_fa_lookups,XYZ","6776,43192.4939880556");
mapB.put("abc_fa_methods,XYZ","1930,43192.494011713");
mapB.put("abc_fa_periods,XYZ","9643,43273.270426713");
mapB.put("abc_fa_retirements,XYZ","62167,43273.1558409838");
mapB.put("abc_fa_transactions,XYZ","581980,43273.1545858449");
mapB.put("abc_fa_transactions_dh,XYZ","420,43100.8651175347");
mapB.put("abc_fnd_currencies,XYZ","257,43268.6356319097");
mapB.put("abc_fnd_descriptive_flex_column_usages,XYZ","30251,43268.6169408218");
mapB.put("abc_fnd_descriptive_flex_context_labels,XYZ","814,43199.6273292361");
mapB.put("abc_fnd_descriptive_flexs,XYZ","3000,43268.6169408218");
mapB.put("abc_fnd_document_attachments,XYZ","140965,43272.649096169");
mapB.put("abc_fnd_document_categories,XYZ","278,43268.635933831");
mapB.put("abc_fnd_document_category_usages,XYZ","1037,43268.635933831");
mapB.put("abc_fnd_document_entities,XYZ","440,43268.6358883796");
mapB.put("abc_fnd_document_media,XYZ","3043,43272.649096169");
mapB.put("abc_fnd_document_sequence_categories,XYZ","42945,43272.6483789468");
mapB.put("abc_fnd_flex_value_hierarchy,XYZ","27563,43235.7319845602");
mapB.put("abc_fnd_flex_value_sets,XYZ","21808,43272.6487910069");
mapB.put("abc_fnd_flex_value_sets,CBS","936,43180.2485385648");
mapB.put("abc_fnd_flex_values,XYZ","511443,43272.6487910069");
mapB.put("abc_fnd_flex_values,CBS","643030,43180.2485385648");
mapB.put("abc_fnd_flex_values_tl,CBS","643012,43180.2485385648");
mapB.put("abc_fnd_flex_values_tl,XYZ","5625873,43272.6487910069");
mapB.put("abc_fnd_id_flex_segments,CBS","117,43180.2490770023");
mapB.put("abc_fnd_id_flex_segments,XYZ","937,43268.6168802431");
mapB.put("abc_fnd_id_flex_structures,CBS","16,43180.2490770023");
mapB.put("abc_fnd_id_flex_structures,XYZ","219,43268.6168802431");
mapB.put("abc_fnd_lookup_types,CBS","5445,43180.2485133681");
mapB.put("abc_fnd_lookup_types,XYZ","20954,43268.6357395833");
mapB.put("abc_fnd_lookup_values,XYZ","310978,43272.648671331");
mapB.put("abc_fnd_lookup_values,CBS","48696,43180.2485133681");
mapB.put("abc_fnd_profile_option_values,XYZ","25652,43272.6486771875");
mapB.put("abc_fnd_profile_option_values,CBS","1229,43180.248486331");
mapB.put("abc_fnd_profile_options,CBS","694,43180.248486331");
mapB.put("abc_fnd_responsibility,XYZ","3296,43271.3555948032");
mapB.put("abc_fnd_segment_attribute_values,CBS","38,43180.2490770023");
mapB.put("abc_fnd_segment_attribute_values,XYZ","1743,43199.6271230556");
mapB.put("abc_fnd_territories,XYZ","256,43268.6358083449");
mapB.put("abc_fnd_user,XYZ","8068,43272.6486153935");
mapB.put("abc_fnd_user_responsibility,XYZ","51559,43272.6491750463");
mapB.put("abc_geographies,CBS","248,43180.2484406366");
mapB.put("abc_geographies,XYZ","4762,43268.635808125");
mapB.put("abc_gl_alloc_batches,XYZ","133,43272.8261837847");
mapB.put("abc_gl_alloc_formula_lines,XYZ","24940,43272.8261837847");
mapB.put("abc_gl_alloc_formulas,XYZ","4988,43272.8261837847");
mapB.put("abc_gl_auto_alloc_batches,XYZ","34,43268.6176857523");
mapB.put("abc_gl_auto_alloc_sets,XYZ","9,43268.6176857523");
mapB.put("abc_gl_balances,CBS","460591,43180.426733125");
mapB.put("abc_gl_balances,XYZ","508033315,43230.3016400347");
mapB.put("abc_gl_coa_value_sets,CBS","140,43180.2500551736");
mapB.put("abc_gl_coa_value_sets,XYZ","121,43265.8065184375");
mapB.put("abc_gl_coa_values,CBS","3129452,43180.2500551736");
mapB.put("abc_gl_coa_values,XYZ","811785,43272.6507386227");
mapB.put("abc_gl_code_combinations,CBS","68076,43180.2496125116");
mapB.put("abc_gl_code_combinations,XYZ","3426235,43256.2275320255");
mapB.put("abc_gl_daily_conversion_types,CBS","5,43180.2494641898");
mapB.put("abc_gl_daily_conversion_types,XYZ","72,43268.6184428241");
mapB.put("abc_gl_daily_rates,XYZ","122019188,43272.6507345139");
mapB.put("abc_gl_daily_rates,CBS","30318,43180.2494641898");
mapB.put("abc_gl_import_references,XYZ","186457872,43264.3415345602");
mapB.put("abc_gl_import_references,CBS","7969012,43181.2955106944");
mapB.put("abc_gl_import_references_dh,XYZ","26604513,43145.1577739005");
mapB.put("abc_gl_je_batches,XYZ","2728068,43272.7667763542");
mapB.put("abc_gl_je_batches,CBS","588,43181.2955106944");
mapB.put("abc_gl_je_batches_dh,XYZ","2042,43168.746250706");
mapB.put("abc_gl_je_categories_tl,CBS","117,43180.2494409607");
mapB.put("abc_gl_je_categories_tl,XYZ","3102,43268.6176579745");
mapB.put("abc_gl_je_headers,XYZ","19215835,43214.4233078356");
mapB.put("abc_gl_je_headers,CBS","1178,43181.2955106944");
mapB.put("abc_gl_je_headers_dh,XYZ","581442,43168.746250706");
mapB.put("abc_gl_je_lines,XYZ","365870724,43193.8699508565");
mapB.put("abc_gl_je_lines_dh,XYZ","852720389,43193.8699508565");
mapB.put("abc_gl_je_sources_tl,CBS","43,43180.2494381713");
mapB.put("abc_gl_je_sources_tl,XYZ","3586,43214.3266307176");
mapB.put("abc_gl_ledger_balancing_segments,XYZ","482,43268.6178050579");
mapB.put("abc_gl_ledger_periods,XYZ","48873,43221.7804195602");
mapB.put("abc_gl_ledger_periods,CBS","1940,43180.2497818055");
mapB.put("abc_gl_ledger_relationships,CBS","47,43180.2494641088");
mapB.put("abc_gl_ledger_relationships,XYZ","411,43268.6178050579");
mapB.put("abc_gl_ledger_set_assignments,XYZ","843,43268.6178050579");
mapB.put("abc_gl_ledgers,XYZ","418,43268.6178050579");
mapB.put("abc_gl_ledgers,CBS","53,43180.2494641088");
mapB.put("abc_gl_periods,XYZ","678,43189.7832918403");
mapB.put("abc_gl_periods,CBS","235,43180.2497818055");
mapB.put("abc_gl_subledger_distributions,XYZ","34117841,43272.6570907176");
mapB.put("abc_gl_subledger_distributions,CBS","17608620,43180.5930337731");
mapB.put("abc_gl_subledger_lines,XYZ","14869523,43272.6570907176");
mapB.put("abc_gl_subledger_references,CBS","11953274,43181.3702182407");
mapB.put("abc_gl_subledger_references,XYZ","63347683,43228.2754828704");
mapB.put("abc_hr_assignments,XYZ","553663,43236.2802898148");
mapB.put("abc_hr_assignments_dh,XYZ","6,43181.3084185185");
mapB.put("abc_hr_locations,XYZ","1472,43272.4184375463");
mapB.put("abc_hr_locations_dh,XYZ","1,43180.6461068171");
mapB.put("abc_hr_people,XYZ","561134,43272.7518781019");
mapB.put("abc_hr_people_dh,XYZ","7,43195.5174862037");
mapB.put("abc_hr_pos_structure_elements,XYZ","601,43269.4130085301");
mapB.put("abc_hr_position_structures,XYZ","32,43269.4130085301");
mapB.put("abc_hr_positions,XYZ","449,43269.4130085301");
mapB.put("abc_internal_bank_account_documents,XYZ","252,43272.6610732986");
mapB.put("abc_internal_bank_account_uses,XYZ","1546,43272.7626210532");
mapB.put("abc_internal_bank_accounts,XYZ","1542,43272.7611923843");
mapB.put("abc_internal_bank_accounts_secured_attributes,XYZ","1542,43272.7611923843");
mapB.put("abc_inv_bill_of_materials,XYZ","1113,43256.9769808102");
mapB.put("abc_inv_bom_components,XYZ","1884,43256.9769808102");
mapB.put("abc_inv_bom_resources,XYZ","38,43268.8932581944");
mapB.put("abc_inv_categories,XYZ","7122,43209.451384456");
mapB.put("abc_inv_item_categories,XYZ","6935747,43236.0593178819");
mapB.put("abc_inv_item_cost_details,XYZ","2302085,43236.3459391898");
mapB.put("abc_inv_item_costs,XYZ","2475112,43272.6589800926");
mapB.put("abc_inv_item_revisions,XYZ","2292783,43236.0599286343");
mapB.put("abc_inv_items,XYZ","2390687,43269.4204171296");
mapB.put("abc_inv_material_transaction_accounts,XYZ","3403795,43272.6614916319");
mapB.put("abc_inv_material_transactions,XYZ","1878570,43271.3665466782");
mapB.put("abc_inv_mtl_transaction_lot_numbers,XYZ","1050852,43272.6614916319");
mapB.put("abc_inv_onhand_quantites,XYZ","35102,43236.348574213");
mapB.put("abc_inv_parameters,XYZ","937,43213.9215269097");
mapB.put("abc_inv_subinventories,XYZ","2578,43262.6031176273");
mapB.put("abc_inv_units_of_measure,XYZ","110,43192.3447800925");
mapB.put("abc_inv_uom_conversions,XYZ","2936,43161.5500064352");
mapB.put("abc_legal_entities,XYZ","357,43221.5564825579");
mapB.put("abc_legal_entities,CBS","152,43180.2485615972");
mapB.put("abc_legal_entity_registrations,CBS","304,43180.2485615972");
mapB.put("abc_legal_entity_registrations,XYZ","382,43268.6359473032");
mapB.put("abc_om_order_headers,XYZ","76573,43228.0120184722");
mapB.put("abc_om_order_holds,XYZ","175925,43236.3503649421");
mapB.put("abc_om_order_line_history,XYZ","115956,43227.6775010648");
mapB.put("abc_om_order_lines,XYZ","423428,43228.0120184722");
mapB.put("abc_om_price_list_line_attributes,XYZ","501017,43219.8392744329");
mapB.put("abc_om_price_list_lines,XYZ","501138,43235.7412509491");
mapB.put("abc_om_price_list_qualifiers,XYZ","850,43145.0203525347");
mapB.put("abc_om_price_lists,XYZ","972,43235.7412509491");
mapB.put("abc_om_transaction_types,XYZ","6758,43168.7371143403");
mapB.put("abc_operating_units,XYZ","304,43199.6260239236");
mapB.put("abc_organizations,XYZ","1162,43243.4052112153");
mapB.put("abc_po_action_history,XYZ","162702,43228.0189270486");
mapB.put("abc_po_agents,XYZ","454,43227.9012679282");
mapB.put("abc_po_distributions,XYZ","143165,43236.3490382523");
mapB.put("abc_po_distributions_dh,XYZ","277,43192.3468581482");
mapB.put("abc_po_headers,XYZ","50111,43269.4335298843");
mapB.put("abc_po_headers_dh,XYZ","7,43166.9742898611");
mapB.put("abc_po_lines,XYZ","137762,43269.3004224306");
mapB.put("abc_po_lines_dh,XYZ","92,43192.3468581482");
mapB.put("abc_po_releases,XYZ","675,43272.6676102778");
mapB.put("abc_po_requisition_distributions,XYZ","4916,43235.8636478704");
mapB.put("abc_po_requisition_distributions_dh,XYZ","1,43162.1648364236");
mapB.put("abc_po_requisition_headers,XYZ","4016,43235.8636478704");
mapB.put("abc_po_requisition_headers_dh,XYZ","1,43162.1648364236");
mapB.put("abc_po_requisition_lines,XYZ","4600,43235.8636478704");
mapB.put("abc_po_requisition_lines_dh,XYZ","1,43162.1648364236");
mapB.put("abc_po_vendor_bank_accounts,XYZ","6623,43272.6648829051");
mapB.put("abc_po_vendor_contacts,XYZ","3432,43272.6647480556");
mapB.put("abc_po_vendor_sites,XYZ","17351,43272.6615350694");
mapB.put("abc_po_vendors,XYZ","6838,43272.6615350694");
mapB.put("abc_po_vendors_secured_attributes,XYZ","6838,43272.6615350694");
mapB.put("abc_rcv_shipment_headers,XYZ","60902,43228.0108643634");
mapB.put("abc_rcv_shipment_headers_dh,XYZ","3,43166.9666005787");
mapB.put("abc_rcv_shipment_lines,XYZ","155335,43228.0108643634");
mapB.put("abc_rcv_subledger_lines,XYZ","48244,43272.6638612269");
mapB.put("abc_rcv_transactions,XYZ","316780,43228.0108643634");
mapB.put("abc_tax_accounts,XYZ","3836,43276.6228216088");
mapB.put("abc_tax_codes,XYZ","3046,43276.4820179977");
mapB.put("abc_tax_lines,XYZ","1335168,43272.678131412");
mapB.put("abc_tax_party_profiles,XYZ","11039,43272.6781120949");
mapB.put("abc_tax_party_profiles,XYZ","11039,6/21/18 16:16");

在mapA中总共有219个条目,而在mapB中总共有210个条目。当我从这个问题的开头开始运行逻辑时,我得到了总共79个条目的输出,而带有宏的Microsoft Excel给了我210个公用键。 如何检查两个HashMaps:mapA和mapB并检索两个HashMaps中的公用密钥? 有人可以让我知道我在这里犯的错误吗?

6 个答案:

答案 0 :(得分:5)

在当前代码中,您实际上是在比较两个地图中给定键的。实际上,您真的只想在这里采用两个键集的交集,所以只需这样做:

Set<String> s1 = mapA.keySet();
Set<String> s2 = mapB.keySet();
s1.retainAll(s2);

List<String> result = new ArrayList<>();
result.addAll(s1);

如果我们担心不破坏原始的mapA映射,则可以从该映射中复制密钥集:

TreeSet<String> s1 = new TreeSet<String>(mapA.keySet());
Set<String> s2 = mapB.keySet();
s1.retainAll(s2);

List<String> result = new ArrayList<>();
result.addAll(s1);

答案 1 :(得分:2)

您根据键而不是键本身检查值的相等性的问题。

List<String> commonList = new ArrayList<String>();
    for(String key : mapA.keySet()) {
    if (mapB.containsKey(key)) {
        commonList.add(key);
    }
}

由于键在定义上是唯一的,因此建议您使用HashSet。方法retainAll仅保留集合中包含在另一个元素中的元素。

Set<String> keys = new HashSet<>(mapA.keySet()).retainAll(mapB.keySet());

答案 2 :(得分:1)

您可以使用retainAll()方法:

    Set<String> commonSet = new HashSet<>(mapA.keySet());
    Set<String> setB = mapB.keySet();
    commonSet.retainAll(setB);

答案 3 :(得分:0)

您正在检查相等的,而不是相等的。如果您写了if(mapA.containsKey(key))),则可以进行检查(也可以将!=空值检查也更改为containsKey,因为它通常更快)

您还可以只使用Set.retainAll并传入两个keySet的地图

Set<String> keys = new HashSet<>( mapA.keySet() );
keys.retainAll( mapB.keySet() );

答案 4 :(得分:0)

您也可以使用Java8的流来做到这一点。

List<String> intersection = mapA.keySet()
                .stream()
                .filter(mapB.keySet()::contains)
                .collect(Collectors.toList());
        System.out.println(intersection.size());

答案 5 :(得分:0)

mapB只有209个条目,因为最后两个条目具有相同的键。

您的代码也比较数据,因此79个是在两个映射中具有相同数据的公用键。那是你想要的吗?

如果只需要公用键,则只需要将两组os键相交即可。您可以使用方法“ retainAll”执行此操作,如下例所示。此方法也只保留SET中所有其他SET中的值。结果为209(而不是210),因为mapB只有209个唯一键。

public class Test {

    public void test() {
        final List<String> commonList = new ArrayList<String>();
        final Map<String, String> mapA = new HashMap<>();
        final Map<String, String> mapB = new HashMap<>();
        fillA(mapA);
        System.out.println("Size A: " + mapA.size());
        fillB(mapB);
        System.out.println("Size B: " + mapB.size());
        for (final String key : mapA.keySet()) {
            if (mapB.get(key) != null) {
                if (mapA.get(key).equals(mapB.get(key))) {
                    commonList.add(key);
                }
            }
        }
        System.out.println("CommonList: " + commonList.size());
        final Set<String> keySetA = mapA.keySet();
        final Set<String> keySetB = mapB.keySet();
        final Set<String> commonKeySet = new HashSet<>(keySetA);
        commonKeySet.retainAll(keySetB);
        System.out.println("CommonKey:" + commonKeySet.size());
    }

    private void fillA(Map<String, String> mapA) {
        // put your A data here
    }

    private void fillB(Map<String, String> mapB) {
        // put your B data here        
    }
}