Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a data structure in Java that can hold 4 or more values

Is there a data structure in Java which can hold more than 4 values?

So something along the lines of

Map<String, String, String, String>;

This is needed to be able to reduce the number of if else statements I have. I would like to be able to do the following.

check if the data structure contains an element which matches a certain value, if it does then it assigns a link(which is string) to a variable and then adds a code and message to another variable which is related to that link.

if not is there a good workout around to achieve this?

like image 272
user6248190 Avatar asked Dec 10 '22 14:12

user6248190


2 Answers

Is there a data structure in Java which can hold more than 4 values?

There are lots of them.

The simplest is probably String[] which can hold 4 strings if you instantiate it like this:

new String[4]

And other Answers give other data structures that might meet your actual (i.e. unstated) requirements.

However, it is probably possible ... let alone sensible ... for us to enumerate all of the possible data structures that can meet your stated requirement.

Hint: you should try to explain how this data structure needs to work.

Hint 2: "the lines of Map<String, String, String, String>" does not help us understand your real requirement because we don't know what you mean by that.


UPDATE - Your explanation is still extremely vague, but I think you need something like this:

Map<String, MyRecord>;

public class MyRecord {
   private String link;
   private String code;
   private String message;

   // add constructor, getters, setters as required
}
like image 177
Stephen C Avatar answered Dec 13 '22 02:12

Stephen C


There is nothing in the standard libraries, but Guava has a nice implementation; called

Multimap

If Guava is not an option in your environment, you will have to re-invent the wheel though.

like image 36
GhostCat Avatar answered Dec 13 '22 04:12

GhostCat