Java 具有2个值的循环集合

Java 具有2个值的循环集合,java,Java,Java中是否有一些集合,我可以在其中放置两个对象,然后通过对其中一个对象调用方法来返回另一个对象? 示例:myCollection[{o1},{o2}]myCollection.getNext(o1)到retunr o2,反之亦然。Java中没有可以直接解决此问题的集合。我想编写自己的自定义逻辑将是解决方案 我尝试了以下方法: import java.util.*; import java.lang.*; import java.io.*; class Node{ String da

Java中是否有一些集合,我可以在其中放置两个对象,然后通过对其中一个对象调用方法来返回另一个对象?
示例:myCollection[{o1},{o2}]myCollection.getNext(o1)到retunr o2,反之亦然。

Java中没有可以直接解决此问题的集合。我想编写自己的自定义逻辑将是解决方案

我尝试了以下方法:

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());
  }
}

链接到代码:

如果你在寻找类似C++中的一对,你需要自己实现。

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);
类对{
私人K对象1;
私人诉反对2;
配对(K键,V值){
this.object1=键;
this.object2=值;
}
public K getObject1(){返回this.object1;}
public V getObject2(){返回this.object2;}
}
//对象初始化
配对p=新配对(“年龄”,24岁);
使用上述类,您可以获得必要的功能。 如果要查找循环列表,可以使用ArrayList并修改get方法。

class CircularList<T> {
  private List<T> list;
  public CircularList() {
     list = new ArrayList();
  }
  public getNext(int index) {
     return list.get((index+1) % list.size());
  }
}
类循环列表{
私人名单;
公共循环列表(){
列表=新的ArrayList();
}
公共getNext(int索引){
返回list.get((索引+1)%list.size());
}
}
使用地图怎么样

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"));
}
公共静态映射替换(ts1,ts2)
{
Map Map=newhashmap();
地图放置(s1,s2);
map.put(s2,s1);
返回图;
}
公共静态void main(字符串[]args)
{
地图=备选(“你好”,“世界”);
System.out.println(map.get(“hello”);
System.out.println(map.get(“世界”);
}

如果需要任意数量的元素的循环集合,最简单的方法是使用所需的方法进行扩展

这允许您仍然使用所有常规方法在集合中添加和删除元素

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;
    }
}
公共类循环raydeque扩展了ArrayDeque{
公共通告{
超级();
}

公共循环raydeque(CollectionNo.你想做什么?你看了哪些集合?这样的集合没有多大意义,自己滚动一些东西也很简单(如果(param==o1)返回o2,那么它将是一个简单的
;如果(param==o2)返回o1,那么它将抛出异常;
)-这样做的用例是什么?尝试在2个玩家的2元素集合上点击play(),这样每个玩家都可以一个接一个地移动。在这种情况下,我建议您将自己的解决方案作为
next()滚动
可能不是您唯一需要的东西。我不知道他们在Java10中添加了它。但是,是的,如果您还没有,这里是实现。它是在Java8中添加到JDK的。