Discord JDA IdLong Arraylist<Arraylist<Long>

Viewed 22

I'm trying to figure out how can i store Long info about a Discord user in an array.

Tommy on discord type: ;create then Mike on discord type : ;create

public class Create extends ListenerAdapter {
  public void onMessageReceived(MessageReceivedEvent event) {
    if (!event.isFromGuild()) return;
    String[] messageSent = event.getMessage().getContentRaw().split(" "); //for further code
    String name = event.getMember().getUser().getName();
    long idLong = event.getMember().getUser().getIdLong();
    String idString = event.getMember().getUser().getId(); //not for Stackoverflow question
    long base = 1L; //everyone start with 1 (L is because we are using Long version of int value)
    if (messageSent[0].equalsIgnoreCase(";Create")) {
        ArrayList<Long> dcbase = new ArrayList<>(); //Long is used to store getIdLong value
        dcbase.add(idLong); //
        dcbase.add(base); // 1L is default value
        event.getChannel().sendMessage("Here is the array "+ dcbase).queue();}}

Now the problem is if i want my ArrayList to be for many user I would need an ArrayList of ArrayList. Arraylist<ArrayList<Long>> But to search through them I would like to do search using the IdLong value. I tried to replace dcbase as idLong but its already defined.

Is there any way i can do that?

Because what i want to do next is have a method that goes to the idLong of Tommy and pull out the [1] of the Tommy ArrayList.

I plan to store the info to a file that way and will have longer Arrays:

177877878787 1 0 0 //Tommy IdLong, base Long, stuff i'll add, stuff i'll add 
121244777778 1 //Mike IdLong, base Long
//New line for new member.

Since I don't know on which line the required IdLong will be stored in the file, i need a reference to search it. I am self-thaught, I hope I am clear enough.

1 Answers

You want a Map. The key is the user id (as a long) and the value is your number:

private final Map<Long, Long> map = new HashMap<>();

@Override
public void onMessageReceived(MessageReceivedEvent event) {
  ...
  map.put(id, base);
  ...
}

You can then simply access the value by using map.get(id). Instead of using lists, I would recommend using proper objects with defined fields for whatever data you want to store.

For the case of persistence, instead of just writing your data to some file, use a relational database like PostgreSQL or SQLite. This way you can easily update them at runtime without having to read/write the entire map every time you want to access it.

A very helpful thing to use for this is the Java Persistence API (JPA).

@Entity
@Table
public class UserInfo {
  @Id
  private long id;
  private long base;

  public UserInfo(long id, long base) {
    this.id = id;
    this.base = base;
  }

  public void setId(long id) {
    this.id = id;
  }

  public long getId() { return this.id; }

  public void setBase(long base) {
    this.base = base;
  }

  public long getBase() { return this.base; }
}
Related