Circular collection with 2 values
Circular collection with 2 values
Is there some collection in Java in which I can put 2 objects and then by calling a method on one to return the other one?
Example: myColection [o1,o2] myCollection.getNext(o1) to retunr o2 and viceversa.
Such a collection wouldn't make much sense and it would be trivial to roll something yourself (it would be a simple
if(param == o1) return o2; else if(param == o2) return o1; else throw Exception;
anyways). - What's the use case for that?– Thomas
Sep 2 at 18:18
if(param == o1) return o2; else if(param == o2) return o1; else throw Exception;
trying to hit play() on this 2 element collection of 2 players so that each player can move one after another
– Daniel
Sep 2 at 18:48
In that case I'd recommend rolling your own solution as
next()
will probably not be the only thing you'll need.– Thomas
Sep 3 at 9:09
next()
4 Answers
4
There is no collection available in Java which can solve this problem directly. I guess writing own custom logic would be the solution.
I tried something like below:
import java.util.*;
import java.lang.*;
import java.io.*;
class Node
String data;
Node otherNode;
Node(String data)
this.data=data;
Node getOtherNode()
return otherNode;
@Override
public String toString()
return this.data;
class Ideone
public static void main (String args) throws java.lang.Exception
Node first = new Node("FIRST");
Node second = new Node("SECOND");
first.otherNode = second;
second.otherNode = first;
System.out.println(first.getOtherNode());
Link to code:https://www.ideone.com/MA7v6L
thanks. then I'll do it myself :)
– Daniel
Sep 2 at 18:18
How about using a Map?
public static <T> Map<T, T> alternate(T s1, T s2)
Map<T, T> map = new HashMap<>();
map.put(s1, s2);
map.put(s2, s1);
return map;
public static void main(String args)
Map<String, String> map = alternate("hello", "world");
System.out.println(map.get("hello"));
System.out.println(map.get("world"));
If you want a circular collection of any number of elements, the easiest would be to extend ArrayDeque
with the methods you want.
ArrayDeque
This allows you to still use all the normal Deque
methods for adding and removing elements to/from the collection.
Deque
public class CircularArrayDeque<E> extends ArrayDeque<E>
public CircularArrayDeque()
super();
public CircularArrayDeque(Collection<? extends E> c)
super(c);
public CircularArrayDeque(int numElements)
super(numElements);
/**
* Retrieves the first element of this deque and moves it to the end, or
* returns @code null if this deque is empty.
*
* @return the head of this deque, or @code null if this deque is empty
*/
public E getNext()
E elem = pollFirst();
if (elem != null)
addLast(elem);
return elem;
/**
* Retrieves the last element of this deque and moves it to the front, or
* returns @code null if this deque is empty.
*
* @return the tail of this deque, or @code null if this deque is empty
*/
public E getPrevious()
E elem = pollLast();
if (elem != null)
addFirst(elem);
return elem;
In case you are looking by for something like a Pair in C++, you need to implement it on your own.
class Pair<K, V>
private K object1;
private V object2;
Pair(K key, V value)
this.object1 = key;
this.object2 = value;
public K getObject1() return this.object1;
public V getObject2() return this.object2;
// object initialization
Pair<String, Integer> p = new Pair("age", 24);
Using the above class, you can obtain the necessary functionality.
If you are looking for a circular list, you can use ArrayList and modify the get method.
class CircularList<T>
private List<T> list;
public CircularList()
list = new ArrayList();
public getNext(int index)
return list.get((index+1) % list.size());
Already exists.
– user1803551
Sep 2 at 18:19
I didn't know they added it in Java10. But yeah, if you are not yet there, here is the implementation.
– swayamraina
Sep 2 at 18:24
It was added to the JDK in Java 8.
– user1803551
Sep 2 at 18:28
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
No. What are you trying to do? What collections did you look at?
– user1803551
Sep 2 at 18:12