Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • nervo: wow its been awhile since i've logged in
      December 18, 2018, 04:33:19 PM
    • Salayor: o.O
      December 13, 2018, 01:02:33 AM
    • ZHer0kool: This is by far one of my oldest existing forum accounts
      December 12, 2018, 08:40:43 PM
    • DeathsChaos9::|
      December 12, 2018, 06:41:54 PM
    • TheMane221: Dont know if this is allowed but I have an OSRS account I need to sell asap. 95 att, 92 str, 93 def, 94 hp, 77 range and 73 magic. Dm me please.
      December 09, 2018, 04:27:28 PM
    • runescape3dude: I haven't been on here in about 10 years
      December 08, 2018, 11:14:56 PM
    • runescape3dude: holy crap
      December 08, 2018, 11:14:47 PM
    • howi: Gosh i never been on this site since... ever
      December 08, 2018, 01:36:16 AM
    • TinyScape: ur gay
      December 07, 2018, 09:03:26 PM
    • Travas: drub is gay
      December 02, 2018, 11:48:32 PM
    • drubrkletern: asdf
      November 27, 2018, 02:44:00 PM
    • Black Paw: TokHaar aims to give you the best OSRS features in Real High Definition with a mixture of 550 content! (Quality Driven RSPS) -- Come join us for → PvP, PvMing, Skilling, Prosperous Economy, Minigames/Bosses & Much more.. [link]
      November 21, 2018, 06:55:31 PM
    • i5hy: AllstarLegends - Oldschool Nostalgia @ Allstarlegends.eu Long Lasting server been around since 2013-2014
      November 21, 2018, 01:49:59 PM
    • lordvirius: Is there a mobile private server?
      November 21, 2018, 09:34:33 AM
    • Coldmedicine: FREE ADVERTISEMENT ON DISCORD
      November 20, 2018, 03:30:50 AM
    • Coldmedicine:[link]
      November 20, 2018, 03:30:42 AM
    • Coldmedicine: ...
      November 20, 2018, 03:24:02 AM
    • dylanwill: message me bros
      November 19, 2018, 04:49:29 PM
    • dylanwill: I got banned from runeserver
      November 19, 2018, 04:49:08 PM
    • Wayne.RSPS: TokHaar aims to give you the best OSRS features in Real High Definition with a mixture of 550 content! (Quality Driven RSPS) -- Come join us for → PvP, PvMing, Skilling, Prosperous Economy, Minigames/Bosses & Much more.. [link]
      November 16, 2018, 11:24:39 AM

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

    0 Members and 1 Guest are viewing this topic.

    Offlinepenguinimp

    • Member
    • **
    • Posts: 54
    • 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