Jump to content

[1.8][SOLVED] How to organize a HashMap?


HappyKiller1O1

Recommended Posts

So, I am using a HashMap to write things to a json file. It works, but the problem is nothing is organized. I know it's a small thing, but it can have a huge impact on the person attempting to edit it. How would I organize my HashMap alphabetically?

 

Here is my writer (called in my postInit):

package com.happykiller.weightlimit.main.init.config;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.happykiller.weightlimit.api.IWeightedItem;
import com.happykiller.weightlimit.main.ModMain;

import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry;
import net.minecraftforge.fml.common.registry.GameData;

public class WeightLimitJson {

private String name;
private float weightValue;


public static void createJSON() throws IOException {
	Gson json = new GsonBuilder().setPrettyPrinting().create();

	Writer jsonFileWriter = new FileWriter(ModMain.configDirectory + "/WeightLimit.json");

	FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry();

	HashMap<String, Float> items = new HashMap<String, Float>();

	for(Item item : itemRegistry.typeSafeIterable()) {
		String name = itemRegistry.getNameForObject(item).toString();
		float weight;

		if(item instanceof IWeightedItem) {
			weight = ((IWeightedItem)item).getItemWeight();
		}else {
			weight = 0.0F;
		}

		items.put(name, weight);

		//System.out.println(json.toJson(name).toString());
	}

	json.toJson(items, jsonFileWriter);	
	jsonFileWriter.close();
}
}

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I was looking online and I found that I'd need to do this:

Map<String, Person> people = new HashMap<String, Person>();

    Person jim = new Person("Jim", 25);
    Person scott = new Person("Scott", 28);
    Person anna = new Person("Anna", 23);

    people.put(jim.getName(), jim);
    people.put(scott.getName(), scott);
    people.put(anna.getName(), anna);

    // not yet sorted
    List<Person> peopleByAge = new ArrayList<Person>(people.values());

    Collections.sort(peopleByAge, new Comparator<Person>() {

        public int compare(Person o1, Person o2) {
            return o1.getAge() - o2.getAge();
        }
    });

 

I need to make a Map, add my string (mod id) to a List, and sort them with a Comparator?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I found this, but I like to check with other people that have superior knowledge on the subject to see if there is anything more efficient to do. :P Thank you!

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

How I do it now, is with a LinkedHashmap, that I fill after I sort my ArrayList of item registry name's. :P I watched a few videos explaining your way of doing it, and most said it is not needed unless you are organizing frequently.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Well, I needed a mutable map that keeps the Objects in the order in which they were inserted. So, it all works now. :P I'll keep what you said in mind though, never realized how useful Maps are until now.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.