简体   繁体   中英

HashMap of a and ArrayList of a Class

I am new to Java and I am trying to build up a set of tuples like this:

Slegs = {<1,2,500> , <2,5,400>, <5,7,850>, <2,3,450>} and etc.

Then, to define other arrays like:

int u [Slegs][G];

for instance :

u[<1,2,500>][1] = 80
u[<2,5,400>][1] = 80
u[<1,2,500>][2] = 65
u[<2,5,400>][2] = 130

and etc.

For this, I have coded as follows:

    public class Model {
    public static int Y = 7;
    public static int K = 42;
    public static int G = 3;


class Sleg {
        public int i;
        public int j;
        public double l;


        List<Sleg> Slegs = new ArrayList<Sleg>();           

        Map <Integer, Sleg> slTup = new HAshMap<Integer, Sleg>();

        int[][] u = new int [key][G]; /* I want to define and use a key here*/
    }

My problem is that I don't know how to add those tuples into the slTup a for loop, and how to assign a key to them so that I can use the key to define the u[key][G]; As well, how to assert / define that i and j are in Y and i !=j in each tuple <i,j,l> ;

I would really appreciate if you could instantiate and printout u[key][g], using the given theoretical data above.

I didn't understand your problem completely, but as per my understanding to implements Slegs = {<1,2,500> , <2,5,400>, <5,7,850>, <2,3,450>} and etc. like List Slegs = new ArrayList(); create separate class like below:

class slegs{
    int i, j, k;
    slegs(int i, int j, int k){
        this.i = i;
        this.j = j;
        this.k = k;
    }
}

You can also add your logic in the class like for assert / define or while creating an instance you can add checks. Then you can create instance of these class and add them to list (slegs) using slegs.add(s1). To insert it in map just use the instance map.put(key, s1).

First, to use Sleg as a key of the HasMap or HasSet you have to corerctly define hashCode() and equals() . Eg like this one:

class Sleg {

    private final UUID id;
    private final int i;
    private final int j;
    private final double l;

    public Sleg(int i, int j, double l) {
        id = UUID.randomUUID();
        this.i = i;
        this.j = j;
        this.l = l;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!(obj instanceof Sleg))
            return false;
        Sleg sleg = (Sleg)obj;
        return i == sleg.i && j == sleg.j && Double.compare(sleg.l, l) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(i, j, l);
    }
} 

Note: this class Sleg is ready to be a key of the HashMap .

Second, you want to assign an unique integer key with each Sleg instance. I could reccomend you to add this id directly into Skeg instance and increment it each time when you create new object:

class Sleg {

    private final int id;

    private static final AtomicInteger nextId = new AtomicInteger();

    public Sleg(int i, int j, double l) {
        id = nextId.incrementAndGet();
    }
}

I have some code with these following as a requirement. Not sure what this application is all about, but here is my attempt to answer the questions posed:

Problem:

  • how to add those tuples into the slTup in a for loop
  • how to assign a key to them so that I can use the key to define the u[key][G];
  • could instantiate and printout u[key][g]
  • want to have a map of integers and Slegs like <1,<1,2,500>> and <2,<2,5,400>>, where the integer is the key and I can then use it in: u[Key][G]
  • how to assert / define that i and j are in Y and i !=j in each tuple .

Note that its not clear about what it means by "assert i and j are in Y ". But, i != j can be asserted by throwing a run time exception (see Sleg class code constructor).

The program with possible answers:

import java.util.*;
public class SlegProgram {

    private static int key;

    public static void main(String [] args)  {

        // Create some Slegs

        List<Sleg> slegs = Arrays.asList(new Sleg(1,2,500), new Sleg(2,5,400), new Sleg(1,2,500), new Sleg(2,5,400));

        System.out.println(slegs);

        // Create a Sleg Map with keys

        Map <Integer, Sleg> slegMap = new HashMap<>();

        for (Sleg sleg : slegs) {
            slegMap.put(++key, sleg);
        }

        System.out.println(slegMap);

        // Instantiate and printout u [key][G]

        for (Map.Entry<Integer, Sleg> entry : slegMap.entrySet()) {

            int [][] u = new int [entry.getKey()][Model.G];
            System.out.println(Arrays.deepToString(u)); // prints array with initial values: 0
            assignValuesToU(u);
        }

        // Assert i != j in each Sleg

        Sleg illegal = new Sleg(1, 1, 90); // This will throw an exception as i and j are equal
    }

    private static void assignValuesToU(int [][] u) {

        for (int i = 0; i < u.length; i++) {
            for (int j = 0; j < u[i].length; j++) {
                u [i][j] = 80; // 80 or whatever it needs to be
            }
        }
        System.out.println(Arrays.deepToString(u)); // prints array with assigned values
    }
}

class Sleg {
    private int i;
    private int j;
    private int k;
    public Sleg(int i, int j, int k) {
        if (i == j) {
            throw new IllegalArgumentException("Invalid sleg parameters: " + Integer.toString(i));
        }
        this.i = i;
        this.j = j;
        this.k = k;
    }
    public String toString() {
        return Integer.toString(i) + "-" + Integer.toString(j) + "-" + Integer.toString(k);
    }
}

class Model {
    public static final int Y = 7;
    public static final int K = 42;
    public static final int G = 3;
}

Catching the exception:

try {
    Sleg illegal = new Sleg(1, 1, 90);
}
catch (IllegalArgumentException e) {
    System.out.println(e.getMessage()); // This prints something like: Invalid sleg parameters: 1
}

You can use a structure like this:

HashMap<ArrayList<Integer>,ArrayList<Integer>> map =  new HashMap<>();

ArrayList<Integer> sleg = new ArrayList<>();
sleg.add(1);
sleg.add(2);
sleg.add(500);  // or add in one step as List
ArrayList<Integer> g =  new ArrayList<>();
g.add(80);
g.add(65);

map.put(sleg, g);

You can then use the Iterator to navigate each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM