java - 具有 2 个值的循环集合
问题描述
Java 中是否有一些集合,我可以在其中放置 2 个对象,然后通过调用其中一个对象的方法来返回另一个对象?示例:myColection [{o1},{o2}] myCollection.getNext(o1) 以返回 o2,反之亦然。
解决方案
使用地图怎么样?
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"));
}
推荐阅读
- c - 无法理解这段代码的工作原理“binaryString[i] = ((number & 1) + '0')”
- wordpress - 将简码添加到店面标题操作
- python - 冻结除输出/logits 之外的所有层
- android - 当我将 ListView 放在列中时,我得到“引发了另一个异常:未布置 RenderBox”
- python-2.7 - 如何从与当前脚本的父目录平行的目录导入 .py 文件?
- ruby - Ruby 的块语法的起源是什么?
- javascript - 在返回的内部函数中访问参数对象
- r - 我可以用一个表达式引用多个正则表达式项吗?
- api - 使用 fetch api 调用带有 keepalive 的 POST 请求时,预检请求失败
- batch-file - 如何使用命令行更改 SRP 安全级别