Hash of Hashes of Arrays in Java - java

I'm trying to implement Hash of Hashes of Arrays in Java and thought it would be nice if I will use anonymous blah blah(i forgot the exact term/I dont know how to call it).
HashMap<String, HashMap<String, String[]>> teams =
new HashMap<String, HashMap<String, String[]>>(){{
put("east", new HashMap<String, String[]>(){{
put("atlantic", new String[] { "bkn", "bos", "phi","tor", "ny" });
put("central", new String[] { "chi", "cle", "det", "ind", "mil" });
put("southeast", new String[] { "atl", "cha", "mia", "orl", "wsh" });
}});
put("west", new HashMap<String, String[]>(){{
put("northwest", new String[] { "den", "min", "okc", "por", "utah" });
put("pacific", new String[] { "gs", "lac", "lal", "phx", "sac" });
put("southwest", new String[] { "dal", "hou", "mem", "no", "sa" });
}});
}};
My question is if there is a another way to implement taking readability into consideration or completely perhaps completely change the implementation?
I know java is not the right tool but my boss told me to do so.
Also, please let me know of the right term. TIA

As long as we're not caring about run speed, why not use a language designed to express tiered data structures like JSON? JAVA has great external library support for it ...
Gson to the rescue!
#SuppressWarnings("unchecked")
HashMap teams =
new Gson().fromJson(
"{'east' : { 'atlantic' : ['bkn', 'bos', 'phi','tor', 'ny']," +
" 'central' : ['chi', 'cle', 'det', 'ind', 'mil']," +
" 'southeast' : ['atl', 'cha', 'mia', 'orl', 'wsh']}," +
" 'west' : { 'northwest' : ['den', 'min', 'okc', 'por', 'utah']," +
" 'pacific' : ['gs', 'lac', 'lal', 'phx', 'sac']," +
" 'southwest' : ['dal', 'hou', 'mem', 'no', 'sa']}}",
HashMap.class
);
http://code.google.com/p/google-gson/

Using a helper method
private void addTeams(String area, String codes) {
String[] areas = area.split("/");
Map<String, String[]> map = teams.get(areas[0]);
if (map == null) teams.put(areas[0], map = new HashMap<String, String[]>());
map.put(areas[1], codes.split(", ?"));
}
Map<String, Map<String, String[]>> teams = new HashMap<String, Map<String, String[]>>();{
addTeams("east/atlantic", "bkn, bos, phi, tor, ny");
addTeams("east/central", "chi, cle, det, ind, mil");
addTeams("east/southeast", "atl, cha, mia, orl, wsh");
addTeams("west/northwest", "den, min, okc, por, utah");
addTeams("west/pacific", "gs, lac, lal, phx, sac");
addTeams("west.southwest", "dal, hou, mem, no, sa");
}
You can replace
new String[] { "bkn", "bos", "phi","tor", "ny" }
with
"bkn,bos,phi,tor,ny".split(",");

Related

Aggregation and grouping using Hazelcast jet

I am trying to do grouping and aggregation using Hazelcast Jet but it is getting little bit slow as I have to loop through data twice one for creating groupingKey and after that aggregating all data so is there any better and feasible way to it, Please help
Here is my code first I am creating a groupingKey from using my data as grouping is done by multiple keys:
// Fields with which I want to do grouping.
List<String> fields1 = {"Field1", "Field4"};
List<String> cAggCount = {"CountFiled"};
List<String> sumField = {"SumFiled"};
BatchStage<Map<Object, List<Object>>> aggBatchStageDataGroupBy = batchStage
.aggregate(AggregateOperations.groupingBy(jdbcData -> {
Map<String, Object> m = ((Map<String, Object>)jdbcData);
Set<String> jset = m.keySet();
StringBuilder stringBuilder = new StringBuilder("");
fields1.stream().forEach(dataValue -> {
if (!jset.contains(dataValue.toString())) {
stringBuilder.append(null+"").append(",");
} else {
Object k = m.get(dataValue.toString());
if (k == null) {
stringBuilder.append("").append(",");
} else {
stringBuilder.append(k).append(",");
}
}
});
return stringBuilder.substring(0, stringBuilder.length() - 1);
}));
And after that I am doing aggregation on it as below:
BatchStage<List<Map<String, Object>>> aggBatchStageData = aggBatchStageDataGroupBy
.map(data -> {
data.entrySet().stream().forEach(v -> {
Map<String, Object> objectMap = new HashMap<>();
IntStream.range(0, cAggCount).forEach(k -> {
objectMap.put(countAlias.get(k), v.getValue().stream().mapToLong(dataMap -> {
return new BigDecimal(1).intValue();
}).count());
});
}
return mapList;
});
So can we do this whole process in one go instead of doing loop twice like groupigByKey first and than aggregating it.

Improving code design to enhance performance

I am trying to do something like below. I don't like the design of this as I am using 4 for loops to achieve this. Can I further enhance the design to achieve this?
Creating a map with dates as keys.
Sort the list values inside the map on dates(dates have hours and minutes here)
Giving an incremental id to each dto.
int serialNumber = 1;
if (hList != null && !hList.isEmpty()) {
// create a Map with dates as keys
HashMap<String, ArrayList<BookDTO>> mapObj = new HashMap<>();
for (int count = 0; count < hList.size(); count++) {
BookDTO bookDTO = (BookDTO) hList.get(count);
ArrayList<BookDTO> list = new ArrayList<>();
list.add(bookDTO);
Calendar depDate = bookDTO.getDepartureDate();
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
if (depDate != null) {
String formattedDate = format.format(depDate.getTime());
if (mapObj.containsKey(formattedDate)) {
mapObj.get(formattedDate).add(bookDTO);
} else {
mapObj.put(formattedDate, list);
}
}
}
// Sort the values inside the map based on dates
for (Entry<String, ArrayList<BookingDTO>> entry : mapObj.entrySet()) {
Collections.sort(entry.getValue(), new BookDTOComparator(DATES));
}
for (Entry<String, ArrayList<BookDTO>> entry : mapObj.entrySet()) {
serialNumber = setItinerarySerialNumber(entry.getValue(), serialNumber);
}
I believe you can merge two last loops. So, we will see only two loops. (for now I can see three loops)
You can also try Arrays.parallelSort(entry.getValue()) if lists are too large and it is applicable
Also, if it applicable, see code below:
int serialNumber = 1;
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
ArrayList<BookDTO> hListCopy = new ArrayList<>(hList);
Collections.sort(hListCopy, new NewBookDTOComparator()); // 2. sorting
HashMap<String, ArrayList<BookDTO>> mapObj = new HashMap<>();
for (BookDTO bookDTO : hListCopy) {
serialNumber = setItinerarySerialNumber(bookDTO, serialNumber); // 3. serialNumber
Calendar depDate = bookDTO.getDepartureDate();
if (depDate != null) {
String formattedDate = format.format(depDate.getTime());
if (mapObj.containsKey(formattedDate)) {
mapObj.get(formattedDate).add(bookDTO);
} else {
ArrayList<BookDTO> list = new ArrayList<>();
list.add(bookDTO);
mapObj.put(formattedDate, list);
}
}
}
So, only one loop (and one sorting algorithm).
For list cope-constructor used System.arraycopy internally, you can google performance of.
You can sort hList instead without creating new 'hListCopy' if applicable.
Beware of NewBookDTOComparator, you should sort not only by minutes and hours, but also by 'DepartureDate'
I think SimpleDateFormat format should be static field or class field.
You can also try Arrays.parallelSort(hListCopy) if lists are too large and it is applicable

Transferring data from double array to HashMap

Creating a double array (one row for states, and one for capitols), I am trying to use 'map.put' in a for loop to save the arrays 'key(states)' and 'value(capitols)' to the HashMap. When using a key from user input after assigning a new HashMap (hMap = getInfo();, My output returns "null". Im not quite sure what it is im doing wrong, but I have a feeling I made an error in the for loop.
public class HashMapProgram {
public static void main (String[]args) {
Scanner input = new Scanner(System.in);
//Assign contents of map in getInfo to hMap
HashMap<String, String> hMap = getInfo();
//Prompting user to input a state (key)
System.out.print("Enter a state, or \"done\" when finished: ");
String state = input.next();
if(hMap.get(state) != "done")
System.out.println("The capital is "+ hMap.get(state));
}
public static HashMap<String, String> getInfo(){
//HashMap to save contents in
HashMap<String, String> map = new HashMap<>();
String x[][] = {
{"Alabama","Alaska","Arizona" ,"Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia",
"Hawaii" ,"Idaho" ,"Illinois" ,"Indiana" ,"Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
"Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington","West Virginia","Wisconsin","Wyoming"},
{"Montgomery","Juneau","Phoenix","Little Rock","Sacramento","Denver","Hartford","Dover","Tallahassee","Atlanta",
"Honolulu","Boise","Springfield","Indianapolis","Des Moines","Topeka","Frankfort","Baton Rouge","Augusta", "Annapolis",
"Boston","Lansing","St. Paul","Jackson","Jefferson City","Helena","Lincoln","Carson City","Concord","Trenton",
"Santa Fe","Albany","Raleigh","Bismarck","Columbus","Oklahoma City","Salem","Harrisburg","Providence","Columbia",
"Pierre","Nashville","Austin","Salt Lake City","Montpelier","Richmond","Olympia","Charleston","Madison","Cheyenne"}
};
//Saving contents in 'map'
for(int i = 0; i < x.length; i++) {
map.put(x[0][i], x[1][i]);
}
return map;
}
}
There are a few errors:
1) In your for-loop, change i < x.length; to i < x[0].length;, otherwise you're running the loop just 2 times.
2) Don't compare strings using !=. Use equals() instead. See this for more details.
3) You don't have a loop to ask for user input repeatedly. Change your code in main() to:
Scanner input = new Scanner(System.in);
HashMap<String, String> hMap = getInfo();
String state = "";
do {
System.out.print("Enter a state, or \"done\" when finished: ");
state = input.next();
System.out.println("The capital is " + hMap.get(state));
} while (!state.equals("done"));
4) Work with interface, not class. So change
HashMap<String, String> hMap = getInfo();
to
Map<String, String> hMap = getInfo();
and also update the method signature to return Map<String, String>.
5) Since Java 9, you can directly create a map like this:
Map<String, String> m = Map.of(
"Alabama", "Montgomery",
"Alaska", "Juneau",
"Arizona", "Phoenix"
//and so on...
);

How to retrieve common keys from two hashMaps and put the result into a ArrayList

I am trying to retrieve common 'keys' from two hashMaps and put the result in a ArrayList. To do that, I have come up with following logic.
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);
}
}
}
This is the data I added in the maps: 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");
There are a total of 219 entries in mapA & 210 in mapB. When I run the logic from the beginning of this question, I get the output of total 79 entries whereas Microsoft Excel with a Macro gives me 210 common keys.
How can I check both the HashMaps: mapA & mapB and retrieve the common keys in both the HashMaps ?
Could anyone let me know the mistake I am doing here ?
In your current code, you are actually comparing the values for a given key, from the two maps. In fact, you really just want to take the intersection of the two keysets here, so do just that:
Set<String> s1 = mapA.keySet();
Set<String> s2 = mapB.keySet();
s1.retainAll(s2);
List<String> result = new ArrayList<>();
result.addAll(s1);
If we are concerned about not destroying the original mapA map, then we can instead make a copy of the key set from that map:
TreeSet<String> s1 = new TreeSet<String>(mapA.keySet());
Set<String> s2 = mapB.keySet();
s1.retainAll(s2);
List<String> result = new ArrayList<>();
result.addAll(s1);
The problem you check the equality of the values according to the keys and not the keys themselves.
List<String> commonList = new ArrayList<String>();
for(String key : mapA.keySet()) {
if (mapB.containsKey(key)) {
commonList.add(key);
}
}
Since the keys are by definition unique, I suggest you use HashSet. The method retainAll retains only the elements in the set that are contained in another one.
Set<String> keys = new HashSet<>(mapA.keySet()).retainAll(mapB.keySet());
You can make use of the retainAll() method:
Set<String> commonSet = new HashSet<>(mapA.keySet());
Set<String> setB = mapB.keySet();
commonSet.retainAll(setB);
You're checking equal values, not equal keys. You can do your check if you write if(mapA.containsKey(key))) (and change the != null check to containsKey as well, since it's usually faster)
You can also just use Set.retainAll and pass in both keySets of the Maps
Set<String> keys = new HashSet<>( mapA.keySet() );
keys.retainAll( mapB.keySet() );
You can also do this with Java8's stream.
List<String> intersection = mapA.keySet()
.stream()
.filter(mapB.keySet()::contains)
.collect(Collectors.toList());
System.out.println(intersection.size());
The mapB has only 209 entries, because the two last have the same key.
Your code compares the data as well, so 79 is the common keys that have the same data in both maps. Is that what do you want?
If you want only the common keys, you simply need to do the intersection of the two sets os keys. You can do this with the method "retainAll" as the example below. This method retains in the SET only all values that are in the other SET too. The result is 209 (instead of 210), as the mapB has only 209 unique keys.
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
}
}

Populate ComboBox with HashMap?

I'm trying populate a combobox of vaadin7 with informations hashmap. I create a class that return a HashMap when I get the return I use a for each to populate this combobox but does show me only numbers and not the keys and values of hashmap.
I'm trying this.
/** states of brasil class */
public class EstadosBrasil {
private static final HashMap<String, String> uf = new HashMap();
/** return all states of brasil */
public static HashMap<String, String> getEstados(){
uf.put("AC", "AC");
uf.put("AL", "AL");
uf.put("AM", "AM");
uf.put("AP", "AP");
uf.put("BA", "BA");
uf.put("CE", "CE");
uf.put("DF", "DF");
uf.put("ES", "ES");
uf.put("FN", "FN");
uf.put("GO", "GO");
uf.put("MA", "MA");
uf.put("MG", "MG");
uf.put("MS", "MS");
uf.put("MT", "MT");
uf.put("PA", "PA");
uf.put("PB", "PB");
uf.put("PE", "PE");
uf.put("PI", "PI");
uf.put("PR", "PR");
uf.put("RJ", "RJ");
uf.put("RN", "RN");
uf.put("RO", "RO");
uf.put("RR", "RR");
uf.put("RS", "RS");
uf.put("SC", "SC");
uf.put("SE", "SE");
uf.put("SP", "SP");
uf.put("TO", "TO");
return uf;
}
}
// my combobox
private ComboBox comboEstado;
comboEstado = new ComboBox("States");
comboEstado.setWidth("100px");
HashMap<String, String> estados = EstadosBrasil.getEstados();
for(Entry<String, String> e : estados.entrySet()){
Object obj = comboEstado.addItem();
comboEstado.setItemCaption(e.getKey(), e.getValue());
comboEstado.setValue(obj);
}
mainLayout.addComponent(comboEstado);
Any idea ?
thanks
Change-
Object obj = comboEstado.addItem();
comboEstado.setItemCaption(e.getKey(), e.getValue());
comboEstado.setValue(obj);
To-
comboEstado.addItem(e.getKey());
comboEstado.setItemCaption(e.getKey(), e.getValue());
If you want both key and value pair to appear, something like this can be done-
comboEstado.setItemCaption(e.getKey(), e.getKey() + " : " + e.getValue());
By the way, I hope you are going to change the values. If both key and value are the same, you can just use a Set.
In the new Vaadin 8 API, there is no addItems method on Combobox.
The code below works:
Map<String, String> map = new HashMap<>();
ComboBox combobox = new ComboBox<>("My Combobox");
combobox.setItems(map);
combobox.setItemCaptionGenerator(new ItemCaptionGenerator() {
#Override
public String apply(Object o) {
HashMap m = (HashMap) o;
return m.keySet().stream().findFirst().get().toString();
}
});

Categories

Resources