Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • King Bosnia: Any recommendations to private servers?
      October 18, 2018, 04:47:35 PM
    • FightMexoxo: oi i have the files if anyone whats em
      October 18, 2018, 12:44:42 AM
    • The Wanderer: Whoops
      October 15, 2018, 12:56:18 AM
    • The Wanderer: Making own operating system
      October 15, 2018, 12:56:11 AM
    • Travas: no
      October 07, 2018, 04:03:36 PM
    • Saltyspade10: Server still up and running?
      October 05, 2018, 07:13:41 PM
    • Wesam: Can someone upload moparscape.jar?
      October 05, 2018, 11:43:43 AM
    • Ligoe: yo
      September 30, 2018, 12:48:15 PM
    • Ligoe: how come i can't login the game
      September 30, 2018, 12:48:03 PM
    • fuzzout: Anybody here with experience in using Luna source? I'm looking to develop something, but can't seem to get any of my clients to work with it...
      September 26, 2018, 10:47:27 AM
    • `Discardedx2`: ?
      September 21, 2018, 12:24:09 PM
    • Pookey71: ARE YOU HERE TO RECEIVE MY LIMP PENIS!?
      September 20, 2018, 11:55:24 PM
    • Pookey71: THIS IS DEMOCRACY MANIFESTO
      September 20, 2018, 11:55:07 PM
    • Vasi28: Гранд
      September 03, 2018, 07:06:22 PM
    • Rash: test
      August 30, 2018, 09:09:18 PM
    • Guruu:[link]
      August 30, 2018, 12:04:45 AM
    • Guruu: am i allowed to post links shoutbox?
      August 30, 2018, 12:03:46 AM
    • drubrkletern:[link] feedback wanted
      August 28, 2018, 03:55:17 PM
    • bugz000: Anyone got a link for the old ass silab client and cache.zip? from mopar 3.2 era :) 317
      August 25, 2018, 12:48:15 AM
    • cholland2015: Does anyone know where I can download the old moparscape client/server?
      August 24, 2018, 02:30:51 PM

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