Search code examples
javasortingmapsrelationships

java best data structure for two to many relations


So I have three important factors, filenames which there are many, there will also be duplicates, violation types which there are 6 of, and the data relating to them.

I was thinking of using a Map for this but it only accepts two types, so I want to sort the data by the filename and for every entry under that filename, i want to retrieve the violation type, from what i want it to retrieve all the matches from the data, so say it's a map I could of said map.get(filename, violation) and it will retrieve all the results that match that.

Is there a data structure that can allow me to do this? or am I being lazy and should just sort the data myself when it comes to outputting it.


Solution

  • One other way to approach this would be to use a custom Class for holding the needed data. Essentially 'building' your own node that you can iterate over.

    For example! you could create the following class object: (Node.java)

    import java.util.*;
    
    public class Node
    {
        private String violationType;
        private String dataInside;
    
        public Node()
        {   
            this("", "");
        }
        public Node(String violationType)
        {
            this(violationType, "");
        }
        public Node(String violationType, String dataInside)
        {
            this.violationType = violationType;
            this.dataInside = dataInside;
        }
        public void setViolationType(String violationType)
        {
            this.violationType = violationType;
        }
        public void setDataInside(String dataInside)
        {
            this.dataInside = dataInside;
        }
        public String getViolationType()
        {
            return violationType;
        }
        public String getDataInside()
        {
            return dataInside;
        }
    }
    

    ok, great, so we have this 'node' thing with some setters, some getters, and some constructors for ease of use. Cool. Now lets see how to use it:

    import java.util.*;
    
    public class main{
        public static void main(String[] args){
            Map<String, Node> customMap = new HashMap<String, Node>();
            customMap.put("MyFilename", new Node("Violation 1", "Some Data"));
            System.out.println("This is a test of the custom Node: " + customMap.get("MyFilename").getViolationType());
        }
    }
    

    Now we have a map that relates all of the data you need it to. Now, you'll get a lot of people saying 'Don't reinvent the wheel" when it comes to things like this, because built in libraries are far more optimized. That is true! If you can find a data structure that is built into java that suits your needs, USE IT. That's always a good policy to follow. That being said, if you have a pretty custom situation, sometimes it calls for a custom approach. Don't be afraid to make your own objects like this, it's easy to do in Java, and it could save you a lot of time and headache!

    EDIT

    So, after re-reading the OP's question, I realize you want an entire list of associated data for the given violation of a given filename. In which case, you would switch the private String dataInside to something like private ArrayList<String> dataInside; which would allow you to associate as much data as you wanted, still inside that node, just inside of an arraylist. Also note, you'd have to switch up the getters/setters a little to accomodate a list, but that's not too bad.