Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • ragnoroker: Brand new server! Less than 1 week old! Nightmare/Raids 1/2/Hydra/Pk Modes/Exp modes/ Unique  Join today - [link]
      July 04, 2020, 06:42:38 PM
    • ragnoroker: Brand new server! Less than 1 week old! Nightmare/Raids 1/2/Hydra/Pk Modes/Exp modes/ Unique  Join today - [link]
      July 04, 2020, 06:42:32 PM
    • iddsyou: PkOwnage 2 worlds, modern OSRS & 2014 server! [link]  Join us on Discord! [link]
      July 04, 2020, 11:07:29 AM
    • slayerpure: kingdom full blown custom server with over 50 custom bosses released last week. play now here     [link]
      July 02, 2020, 08:44:32 PM
    • jgoet: dodian !!
      July 02, 2020, 07:15:44 PM
    • jgoet: join [link]  its back online !!!!
      July 02, 2020, 07:15:37 PM
    • Protojae: RIP moparscape
      July 02, 2020, 03:01:32 PM
    • Protojae: obviously thiswebsite is dead as fudge
      July 02, 2020, 03:01:14 PM
    • ragnoroker: RuneGuild has just been released! 1 Hour old, Come join the fun - [link]
      June 26, 2020, 02:14:21 PM
    • ragnoroker: RuneGuild has just been released! 1 Hour old, Come join the fun - [link]
      June 26, 2020, 02:14:08 PM
    • ragnoroker: RuneGuild has just been released! 1 Hour old, Come join the fun - [link]
      June 26, 2020, 02:14:00 PM
    • Fox2007: rune-nexus.com - FREE advertising, SEO optimized - no registration required
      June 26, 2020, 06:21:47 AM
    • Decimate Returns: trailer video posted for Project Decimate, Official Launch is in a few weeks! watch here: [youtube]LBorsdI-k1Y[/youtube]
      June 24, 2020, 07:18:03 PM
    • slayerpure: 1 week old rsps we need players and potential staff, customs and goku/vegeta sets play here!    [link] join our discord [link]
      June 24, 2020, 05:23:46 PM
    • Decimate Returns: Project Decimate is still in BETA: join our discord to keep up with the new content added: [link]
      June 24, 2020, 01:09:23 PM
    • Decimate Returns: Project Decimate is still in BETA: join our discord to keep up with the new content added: [link]
      June 24, 2020, 01:09:03 PM
    • dan v jad: PkOwnage OSRS Great server great community!  Join us on Discord! [link]  Download: [link]
      June 21, 2020, 09:23:28 AM
    • Lens: Sick Custom RSPS - CrystalPS. Join us on Discord. [link]
      June 20, 2020, 04:35:44 PM
    • Shady Aftermath: check out skyfire317.com gott 3 custom skills, afk zones
      June 19, 2020, 10:22:22 PM
    • Shady Aftermath: check out skyfire317.com gott 3 custom skills, afk zones
      June 19, 2020, 10:22:14 PM

    Author Topic: [Help] Loop over multiple maps  (Read 995 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:)

     

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