Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Buy OSRS Gold

Sell OSRS Gold
Sign in to follow this  
iRoc

A simple tutorial to HashMap's

Recommended Posts

I just would like to state that I am in no way a java expert, and the information I provide here is just MY understanding of how to use a hashmap. <3

What is a hashmap?

For the simplicity of this guide, and to the limitations that we will use it. It is a method to relate two data types. ie RSTiles, Booleans, Strings and Integers.

More information can be found at: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

Why are they important and what can they be used for?

They are important because they can greatly simplify a script, improving readability and improving efficiency. I personally use them in my OgreRanger class for telegrabbing items that are not at the top of a stack.

Imports and declaring our HashMap.

import java.util.HashMap;

 //HashMap nameOfMap = new HashMap(Size of HashMap);
HashMap<Integer, String> map = new HashMap<Integer, String>(11);

Our variables


// This is just a list of my integers, you do not have to add this however,
// I do to improve readability and to make my script look clean. I do
// not like to have random numbers throughout my scripts.
private final int AVANTOE = 5298, CADANTINE = 5301, HARRALANDER = 5294,
IRIT = 5297, TOADFLAX = 5296, RANARR = 5295, DWARF = 5303,
LANTADYME = 5302, KWUARM = 5299, WATERMELON = 5321,
LIMPWURT = 5100;

// Array of integers of the previous ints above
private final int loot[] = { AVANTOE, CADANTINE, HARRALANDER, IRIT,
TOADFLAX, RANARR, DWARF, LANTADYME, KWUARM, WATERMELON, LIMPWURT };

// Array of String's. Notice how they are arranged the same as the above array
private String[] names = { "Avantoe seed", "Cadantine seed",
"Harralander seed", "Irit seed", "Toadflax seed", "Ranarr seed",
"Dwarf weed seed", "Lantadyme seed", "Kwuarm seed",
"Watermelon seed", "Limpwurt seed" };

Filling our HashMap

We are assigning a predetermined amount. Useful for loot lists.

Because we created two arrays to hold our information (int's and String's), we can now use a for loop to assign the HashMap its values.


// Remember our map is HashMap is . Because we are going to recieve a String value back, our key is int and value is String
//map.put(key, value);
public void putMap() {
for (int i = 0; i < 11; i++) {
map.put(loot[i], names[i]);
}
}

Looting using our new HashMap


// Because our HashMap is , we enter a Int value in key to receive a String back.
//map.get(key);
public void lootStuff() {
RSGroundItem[] gItem = GroundItems.findNearest(loot);
String str = map.get(gItem[0].getID());
if (gItem.length > 0) {
gItem[0].click(str);
}
}

All our information together

import java.util.HashMap;

HashMap map = new HashMap(11, 11);

private final int AVANTOE = 5298, CADANTINE = 5301, HARRALANDER = 5294,
IRIT = 5297, TOADFLAX = 5296, RANARR = 5295, DWARF = 5303,
LANTADYME = 5302, KWUARM = 5299, WATERMELON = 5321,
LIMPWURT = 5100;

private final int loot[] = { AVANTOE, CADANTINE, HARRALANDER, IRIT,
TOADFLAX, RANARR, DWARF, LANTADYME, KWUARM, WATERMELON, LIMPWURT };

private String[] names = { "Avantoe seed", "Cadantine seed",
"Harralander seed", "Irit seed", "Toadflax seed", "Ranarr seed",
"Dwarf weed seed", "Lantadyme seed", "Kwuarm seed",
"Watermelon seed", "Limpwurt seed" };

public void putMap() {
for (int i = 0; i < 11; i++) {
map.put(loot[i], names[i]);
}
}

public void lootStuff() {
RSGroundItem[] gItem = GroundItems.findNearest(loot);
String str = map.get(gItem[0].getID());
if (gItem.length > 0) {
gItem[0].click(str);
}
}

,>,>,>,>,>,>,>,>

 

If you have any questions or comments just post or pm me.

Edited by iRoc

Share this post


Link to post
Share on other sites

Nice scripter rank! Grats.

And, yes. You can get the String at least..

So say you have a stack of IDs:

1000

1001

1002

If you try to loot the bottom item (1002) without the String, you will just keep clicking until you loot it. Therefore looting 1000, 1001, 1002..

By using a hashmap, you can relate the ID to the String.

And by accessing your hashmap with the ID, you can return the string. Therefore the clicking method will right click and select the item. :D

Edit: You can also use ArrayLists and Array's similar.. But this is just cleaner I think.

Share this post


Link to post
Share on other sites

Nice scripter rank! Grats.

And, yes. You can get the String at least..

So say you have a stack of IDs:

1000

1001

1002

If you try to loot the bottom item (1002) without the String, you will just keep clicking until you loot it. Therefore looting 1000, 1001, 1002..

By using a hashmap, you can relate the ID to the String.

And by accessing your hashmap with the ID, you can return the string. Therefore the clicking method will right click and select the item. :D

Edit: You can also use ArrayLists and Array's similar.. But this is just cleaner I think.

thanks!

excellent, thanks for the explanation

Share this post


Link to post
Share on other sites

You are using hashmaps as enums, look into enums, or if you want to make them dynamically make an object.

He's using HashMap's correctly. Map's are used when you want to associate a value with another.

Hash maps arnt for items you already have the info to which he does

Share this post


Link to post
Share on other sites

You are using hashmaps as enums, look into enums, or if you want to make them dynamically make an object.

He's using HashMap's correctly. Map's are used when you want to associate a value with another.

Hash maps arnt for items you already have the info to which he does

He can use it for whatever he wants? lol he can just create an unmodifiableMap if he wanted to. Using a HashMap allows him the flexibility to add more items on the fly. Don't try to disprove someones method if your reasoning is unsubstantial..

Share this post


Link to post
Share on other sites

You are using hashmaps as enums, look into enums, or if you want to make them dynamically make an object.

He's using HashMap's correctly. Map's are used when you want to associate a value with another.

Hash maps arnt for items you already have the info to which he does

When I wrote this guide, we did not have access to #getName() in the API.


You are using hashmaps as enums, look into enums, or if you want to make them dynamically make an object.

He's using HashMap's correctly. Map's are used when you want to associate a value with another.

Hash maps arnt for items you already have the info to which he does

He can use it for whatever he wants? lol he can just create an unmodifiableMap if he wanted to. Using a HashMap allows him the flexibility to add more items on the fly. Don't try to disprove someones method if your reasoning is unsubstantial..

Thank you.

Share this post


Link to post
Share on other sites

You are using hashmaps as enums, look into enums, or if you want to make them dynamically make an object.

He's using HashMap's correctly. Map's are used when you want to associate a value with another.

Hash maps arnt for items you already have the info to which he does

When I wrote this guide, we did not have access to #getName() in the API.


You are using hashmaps as enums, look into enums, or if you want to make them dynamically make an object.

He's using HashMap's correctly. Map's are used when you want to associate a value with another.

Hash maps arnt for items you already have the info to which he does

He can use it for whatever he wants? lol he can just create an unmodifiableMap if he wanted to. Using a HashMap allows him the flexibility to add more items on the fly. Don't try to disprove someones method if your reasoning is unsubstantial..

Thank you.

#getName() has nothing to do with what im talk about

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Our picks

    • Hello everyone,

      Last week we tried to roll out Auth0 Login, but we lost that battle. Now it's time to win the war!

      Important changes

      When logging into the client, you'll now have to enter your Auth0 account credentials instead of your forums credentials

      Note: 2FA is still handled through your forums account (for the time being)



      Changes for existing users

      You'll have to link your Auth0 account to your forums account here: https://tribot.org/forums/settings/login/?service=11


      Auth0 accounts have been created for most existing users. Please use your forums email address and password to login.



      Important notes

      Make sure to verify your email address upon creating a new Auth0 account


      When we mention your Auth0 account, we mean your account used for auth.tribot.org as displayed below
      • 41 replies
    • To better support the upcoming changes (TRiBot X, new repository), we're switching our login handler to Auth0. Instead of logging in with the standard form, you'll now be required to login through our Auth0 application.

      All existing accounts which have been used within approximately the past year have been imported into Auth0 using the same email and password combination which has been stored on the forums.

      What does this mean for users?

      Your account credentials are now even more securely stored


      You'll be able to login via Facebook, Google, and others in the future


      Is there anything users have to do differently now?

      Existing users: You'll have to login with the standard login, open your Account Settings, then link your Auth0 account


      New users: You'll be redirected to our Auth0 app (auth.tribot.org) where you'll be able to create an account


      Why was this change made?

      The new apps we are creating (such as the new repository) aren't able to use the forums to handle user logins


      To centralize all user accounts in one area


      To ensure that the client login doesn't go down when the forums are having problems


      To speed up our development


      Other considerations

      There's no documentation or official support for using Invision Community combined with Auth0, so there are still a few kinks we're working out


      We're in the works of creating an account management panel specifically for Auth0 accounts (ETA August)


      It's not possible to change email addresses for the time being (this will be resolved this August)


      Changing passwords is a weird process for the time being. To change your password, you'll have to use the "Don't remember your password" tool on the Auth0 login page
      • 10 replies
    • Over the past month, we've been working hard on TRiBot's new repository - a much needed update. This change has been deemed necessary for TRiBot X, and will allow us to really speed up development of all aspects of TRiBot.

      Today we are going to share what we've been working on!


      Now you must be wondering what kind of features the new repository will have.... well, you'll have to be patient for a little while longer. We're still figuring out various technical aspects so we can't provide answers to all possible questions. We're also focusing on development rather than writing about it so that everyone can get access to our latest developments at lightning speed. I will however answer a few users' questions.

      We're planning on a release of this early to mid August, giving users some goodies before TRiBot X's release.

      Thank you all for being patient. I hope everyone is excited as much as I am!
      • 17 replies
    • Over the past few months, I’ve been working diligently on a new project - TRiBot X. Everything has been written from the ground up, with all of the best practices of software engineering. Every aspect of TRiBot has been re-imagined to support three main goals: flexibility, useability, and reliability.
      • 50 replies
    • Come give us feedback on the next version of TRiBot!
      • 86 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...