Posted January 4, 20169 yr 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.
January 4, 20169 yr You want a tree map if you want your map sorted by keys. Just note the String comparator is case sensitive. I think its my java of the variables.
January 4, 20169 yr Author 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.
January 4, 20169 yr Why do you ask what you alredy know? Just use the damn Comparator. 1.7.10 is no longer supported by forge, you are on your own.
January 4, 20169 yr Author 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. 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.
January 4, 20169 yr Author How I do it now, is with a LinkedHashmap, that I fill after I sort my ArrayList of item registry name's. 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.
January 4, 20169 yr Author Well, I needed a mutable map that keeps the Objects in the order in which they were inserted. So, it all works now. 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.
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.