0

I'm trying to store pairs of names and addresses in a collection. Is there a way these values can be grouped together in a tuple? If so, which collection should I use to fulfill this? Whatever collection I use, it should be able to add a tuple with these values, remove the tuple, and list the tuples in the collection.

Thanks.

  • 1
    Is creating a class not an option? – akuzminykh Jul 08 '20 at 17:38
  • @akuzminykh I'm a beginner beginner, so I'm not sure what you mean. –  Jul 08 '20 at 17:40
  • Could you post the code? – akuzminykh Jul 08 '20 at 17:41
  • 1
    Creating a class is the correct way to do this. If you do not know how to do that, learn it. –  Jul 08 '20 at 17:42
  • @akuzminykh, well I don't have any code right now. I'm trying to find a way to store these pairs first, and then write code with that. –  Jul 08 '20 at 17:43
  • @Taschi, thanks, I'll look into it. –  Jul 08 '20 at 17:43
  • 1
    Okay then could you explain the semantical relation between names and addresses? E.g. if it's a person's name and the person's address you could combine those in a class `Person` with the attributes `name` and `address`. Then your list contains instances of that class. – akuzminykh Jul 08 '20 at 17:45
  • @akuzminykh, that sounds like a good idea, thanks –  Jul 08 '20 at 17:49

3 Answers3

1

2 options:

  1. Make a value class that holds the 2 things you need.
  2. Or, for simple cases, just use a Pair class which comes with plenty of libraries, including the famous Apache Commons library. i.e. Pair<UUID, Event> pair = Pair.of(id, event)
Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
0

A possible solution may be to create a class Pair. Then you can create instance of Pairs and add them to your ArrayList.

import java.util.ArrayList;

public class Test {
  public static void main(String[] args) {
    Pair pair1 = new Pair(1, 2);
    Pair pair2 = new Pair(2, 3);
    ArrayList<Pair> arrayOfPair = new ArrayList();
    arrayOfPair.add(pair1);
    arrayOfPair.add(pair2);
    for (Pair p : arrayOfPair) {
      System.out.println(p);
    }
  }
}

class Pair<T> {
  T fst;
  T snd;

  public Pair(T fst, T snd) {
    this.fst = fst;
    this.snd = snd;
  }

  T getFst() { return fst; }

  T getSnd() { return snd; }

  public String toString() { return "(" + getFst() + "," + getSnd() + ")"; }
}
  • 1
    [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – akuzminykh Jul 08 '20 at 18:02
0

You can create a class

class Data {
    private String name;
    private String address;

    public Data(String name, String address) {
        this.name = name;
        this.address = address;
    }
    @Override
    public String toString() {
        return "Name: "+name+" and Address: "+address; 
    }
}

And in the main to add the name and address to the arraylist and print it.

public static void main(String[] args) throws IOException {
    List<Data> information = new ArrayList<>();
    information.add(new Data("John Doe", "Mars"));
    System.out.println(information);

}

An output example: [Name: John Doe and Address: Mars]

Ridwan
  • 214
  • 4
  • 17
  • 1
    Class names should begin with an upper case letter according to Java programming conventions. – NomadMaker Jul 08 '20 at 18:18
  • Thanks, and if I wanted to change the attribute `name` or `address` of a pair in `information`, how would I do that? –  Jul 08 '20 at 18:22
  • 1
    This object is immutable and you can only create a new one. You should also override equals() and hashCode(). – NomadMaker Jul 08 '20 at 18:24
  • 1
    It is advisable also to implement Comparable, in case it is intended to use a HashMap for the collection. See [the HashMap API](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html): _To ameliorate impact, when keys are Comparable, this class may use comparison order among keys to help break ties_. – Jonathan Rosenne Jul 08 '20 at 18:37
  • @lionrocker221, Suppose you want to delete the information where `address=="Mars"`. You can do this in a loop. `if(d.getAddress()=="Mars"){ information.remove(d); }` – Ridwan Jul 08 '20 at 18:39