Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Travas: gay
      April 17, 2019, 05:15:02 PM
    • lare69: ruined this site
      April 17, 2019, 04:45:50 PM
    • lare69: k u c f dr house
      April 17, 2019, 04:45:40 PM
    • lare69: fudge dr house
      April 17, 2019, 04:45:29 PM
    • Whackatre: my old friend
      April 17, 2019, 04:28:47 AM
    • arham 4: hello darkness
      April 15, 2019, 06:35:08 PM
    • Travas: fdsa
      April 15, 2019, 05:02:21 PM
    • drubrkletern: asdf
      April 09, 2019, 02:22:52 PM
    • Smokey_: currently looking for ambitious people for staff and server owners to feature. We offer incentive voting program as well
      April 07, 2019, 11:38:29 PM
    • Smokey_: fudge, my bad heres the link: [link] and toplists here: [link]
      April 07, 2019, 11:36:48 PM
    • Smokey_: Hey guys, I dont wanna bring anyone away from this community but in the community i just released we plan to link to other forums tutorials rather than the old fashioned leech method. We also have toplists and user shops integrated into the forums for rsps of different categories to speak to a specific audience, we also have toplists for youtubers, gfx artists,programmers, runescape services and more all for free and we also offer paid ads. website just opened today [link]
      April 07, 2019, 11:36:06 PM
    • jme: Ask mopman for free stuff [link]
      April 07, 2019, 11:06:55 PM
    • veer: is there still an irc server
      April 06, 2019, 10:04:57 PM
    • Travas: stfu
      April 06, 2019, 09:20:05 AM
    • Codar:[link] WE ARE LIVE!! PK/ECO BASED RSPS! PK FROM ALMOST ANYWHERE? YESSIR! COME TRY US NOW! WE ARE CURRENTLY LOOKING FOR ACTIVE STAFF MEMBERS, AND WILL BE REWARDING PLAYERS THAT ARE ACTIVE IN-GAME! SO COME TRY US? :heart:
      April 05, 2019, 12:05:49 AM
    • Codar:[link] WE ARE LIVE!! PK/ECO BASED RSPS! PK FROM ALMOST ANYWHERE? YESSIR! COME TRY US NOW! WE ARE CURRENTLY LOOKING FOR ACTIVE STAFF MEMBERS, AND WILL BE REWARDING PLAYERS THAT ARE ACTIVE IN-GAME! SO COME TRY US? :heart:
      April 05, 2019, 12:05:45 AM
    • Codar:[link] WE ARE LIVE!! PK/ECO BASED RSPS! PK FROM ALMOST ANYWHERE? YESSIR! COME TRY US NOW! WE ARE CURRENTLY LOOKING FOR ACTIVE STAFF MEMBERS, AND WILL BE REWARDING PLAYERS THAT ARE ACTIVE IN-GAME! SO COME TRY US? :heart:
      April 05, 2019, 12:05:42 AM
    • Codar:[link] WE ARE LIVE!! PK/ECO BASED RSPS! PK FROM ALMOST ANYWHERE? YESSIR! COME TRY US NOW! WE ARE CURRENTLY LOOKING FOR ACTIVE STAFF MEMBERS, AND WILL BE REWARDING PLAYERS THAT ARE ACTIVE IN-GAME! SO COME TRY US? :heart:
      April 05, 2019, 12:05:37 AM
    • Codar:[link] WE ARE LIVE!! PK/ECO BASED RSPS! PK FROM ALMOST ANYWHERE? YESSIR! COME TRY US NOW! WE ARE CURRENTLY LOOKING FOR ACTIVE STAFF MEMBERS, AND WILL BE REWARDING PLAYERS THAT ARE ACTIVE IN-GAME! SO COME TRY US? :heart:
      April 05, 2019, 12:05:32 AM
    • Codar:[link] WE ARE LIVE!! PK/ECO BASED RSPS! PK FROM ALMOST ANYWHERE? YESSIR! COME TRY US NOW! WE ARE CURRENTLY LOOKING FOR ACTIVE STAFF MEMBERS, AND WILL BE REWARDING PLAYERS THAT ARE ACTIVE IN-GAME! SO COME TRY US? :heart:
      April 05, 2019, 12:05:23 AM

    Author Topic: [Help] Loop over multiple maps  (Read 553 times)

    0 Members and 1 Guest are viewing this topic.

    Offlinepenguinimp

    • Member
    • **
    • Posts: 55
    • Thanks: +0/-0
      • View Profile
    [Help] Loop over multiple maps
    « on: April 10, 2018, 05:59:19 PM »
    I'm not sure how to loop over multiple maps, does anyone have a solution for this?

    I've created a couple Tree Maps, but I'd like to loop over all of them and do the same action on each of them.

    in my mind I want something like
    Code: Java
    1.             object[]? maps =[accountData, characterData];
    2.             for(map IN maps){
    3.                 for(Entry<String, String> entry : map.entrySet()){
    4.                     if(k !=null){
    5.                         if(v !=null){
    6.                             characterfile.write(entry.getKey()+" = ", 0, entry.getKey().length()+3);
    7.                             characterfile.write(entry.getValue(), 0, entry.getValue().length());
    8.                             characterfile.newLine();
    9.                         }else{
    10.                             p.sendMessage("k: "+ k +" v: "+ v);
    11.                         }
    12.                     }
    13.                 }
    14.             }
    15.  

    This is my crappy solution of 2 loops (which could be even more)
    Code: Java
    1.             //TODO there has got to be a way to loop over multiple maps
    2.             p.sendMessage("./Data/characters/"+ p.playerName+".txt");
    3.             characterfile.write("[ACCOUNT]", 0, 9);
    4.             characterfile.newLine();
    5.             for(Entry<String, String> entry : accountData.entrySet()){
    6.                 if(k !=null){
    7.                     if(v !=null){
    8.                         characterfile.write(entry.getKey()+" = ", 0, entry.getKey().length()+3);
    9.                         characterfile.write(entry.getValue(), 0, entry.getValue().length());
    10.                         characterfile.newLine();
    11.                     }else{
    12.                         p.sendMessage("k: "+ k +" v: "+ v);
    13.                     }
    14.                 }
    15.             }
    16.  
    17.             characterfile.write("[CHARACTER]", 0, 11);
    18.             characterfile.newLine();
    19.             for(Entry<String, String> entry : characterData.entrySet()){
    20.                 if(k !=null){
    21.                     if(v !=null){
    22.                         characterfile.write(entry.getKey()+" = ", 0, entry.getKey().length()+3);
    23.                         characterfile.write(entry.getValue(), 0, entry.getValue().length());
    24.                         characterfile.newLine();
    25.                     }else{
    26.                         p.sendMessage("k: "+ k +" v: "+ v);
    27.                     }
    28.                 }
    29.             }
    30.             characterfile.close();
    31.  



    Edit: here is my solution
    Code: Java
    1.             characterfile =newBufferedWriter(newFileWriter("./Data/characters/"+ p.playerName+".txt"));
    2.             p.sendMessage("./Data/characters/"+ p.playerName+".txt");
    3.             TreeMap[] dataMaps   ={accountData, characterData};
    4.             String[] map_strings ={"ACCOUNT",   "CHARACTER"};
    5.             for(int i =0; i < dataMaps.length; i++){
    6.                 characterfile.write("["+map_strings[i]+"]", 0, map_strings[i].length()+2);
    7.                 characterfile.newLine();
    8.                 //java think this is unsafe, maybe investigate a better solution in the future
    9.                 Set<Entry<String, String>> entries = dataMaps[i].entrySet();
    10.                 for(Entry<String, String> entry : entries){
    11.                     if(entry.getKey()!=null){
    12.                         if(entry.getValue()!=null){
    13.                             characterfile.write(entry.getKey()+" = ", 0, entry.getKey().length()+3);
    14.                             characterfile.write(entry.getValue(), 0, entry.getValue().length());
    15.                             characterfile.newLine();
    16.                         }else{
    17.                             p.sendMessage("k: "+ entry.getKey()+" v: "+ entry.getValue());
    18.                         }
    19.                     }
    20.                 }
    21.                 characterfile.newLine();
    22.             }
    23.             characterfile.close();
    24.  
    « Last Edit: April 11, 2018, 02:53:43 PM by penguinimp »
    Respect: Claws Fury
    meh i lose my password too often, it's me ddragonimp
    Runescape Gambling

    Offlinelare69

    • Member
    • ****
    • *
    • Posts: 5,322
    • Thanks: +0/-0
      • View Profile
    Re: [Help] Loop over multiple maps
    « Reply #1 on: April 11, 2018, 03:35:21 PM »
    A better solution would just be to create a function that does the looping logic for you.

    Code: Java
    1. Consumer<Map<String, String>> loopFunc = map ->{
    2.            for(Entry<String, String> entry : map.entrySet()){
    3.                     if(k !=null){
    4.                         if(v !=null){
    5.                             characterfile.write(entry.getKey()+" = ", 0, entry.getKey().length()+3);
    6.                             characterfile.write(entry.getValue(), 0, entry.getValue().length());
    7.                             characterfile.newLine();
    8.                         }else{
    9.                             p.sendMessage("k: "+ k +" v: "+ v);
    10.                         }
    11.                     }
    12.                 }
    13. };
    14.  
    15. for(Map<String, String> m : maps)
    16.    loopFunc.accept(m)

    You could also just make "loopFunc" a private method in the class or its own class which is probably a more elegant solution...

    Code: Java
    1. privatevoid loopFunc(Map<String, String> map){
    2.    ...
    3. }

    Code: Java
    1. publicfinalclass LoopFunc implements Consumer<Map<String, String>>{
    2.    ...
    3. }
    hi. check out luna:)
    Runescape Gambling

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal