我将简要描述我所做的事情,然后展示我得到的结果与我想要的结果有何不同。
预先感谢您的任何建议。
Java 代码是在 java 在线编译器中创建的,并且可以在其中执行,并且在任何情况下都不会出现错误消息。
Java online compiler, which was used
简短描述:
我想为方阵的行索引和列索引之间的每个可能的组合分配一个 boolean 值。
只有主对角线要排除在外。为此,我使用 TreeMap <String, Boolean>
我形成TreeMap
的键通过在for循环中链接{行索引+“-”+列索引}。
一开始,键的所有值都被分配为{false}。
在第二个循环中,选择所选索引 basicL
的每种可能组合与其他索引(=键)将与 boolean 值 true 链接(同样没有主对角线的值)。结果,TreeMap.keySet()
将以排序顺序打印(按键排序)。示例为 basicL = 4;
矩阵说明 ( boolean true=绿色, boolean false=白色,排除=灰色):
Java 代码:
import java.util.TreeMap;
public class Main {
static TreeMap<String, Boolean> booleanMap = new TreeMap<>();
public static void main(String[] args) {
//all values of symmetric matrix without diagonal axis
int max = 16;
int basicL = 4;
for (int i = 1; i <= max; i++) {
for (int j = 1; j <= max; j++) {
if (j != i) {
booleanMap.put(i + "-" + j, Boolean.valueOf(false));
}
}
}
System.out.println("-----------------------------------------------------");
System.out.println("TreeMap has size: " + booleanMap.size());
System.out.println("TreeMap values: " + booleanMap.values());
System.out.println("TreeMap keySet: " + booleanMap.keySet());
System.out.println("-----------------------------------------------------");
// setting true-conditions
for (int i = 1; i <= max; i++) {
if (i != basicL) {
for (int j = 1; j <= max; j++) {
if (j == basicL) {
booleanMap.replace(i + "-" + j, Boolean.valueOf(true));
}
}
}
if (i == basicL) {
for (int j = 1; j <= max; j++) {
if (j != i) {
booleanMap.replace(i + "-" + j, Boolean.valueOf(true));
}
}
}
}
System.out.println("TreeMap values after looping: " + booleanMap.values());
System.out.println("TreeMap keySet after looping: " + booleanMap.keySet());
System.out.println("-----------------------------------------------------");
}
}
booleanMap.keySet();
取得的结果
[1-10, 1-11, 1-12, 1-13, 1-14, 1-15, 1-16, 1-2, 1-3, 1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 10-1, 10-11, 10-12, 10-13, 10-14, 10-15, 10-16, 10-2 .... ]
期望结果:
[1-2, 1-3, 1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 1-10, 1-11, 1-12, 1-13, 1-14, 1-15, 1-16, 2-1, 2-3, 2-4, 2-5, 2-6, 2-7, 2-8, 2-9, 2-10, 2-11, 2-12 .... ]
最佳答案
索引按字典顺序排序,而不是根据数字的值排序。解决方案是在前面添加零 (1-12
-> 001-012
)。另一种方法是将自定义比较器传递给 TreeMap 的构造函数,如 @samabcde 的答案所示。
我建议您为矩阵中的位置创建一个自定义类。您可以实现compareTo,让TreeMap 自动为您处理排序。此外,您可以显着简化第二个循环并删除 boolean 值的装箱。
TreeMap<Position, Boolean> booleanMap = new TreeMap<>();
int max = 16;
int basicL = 4;
for (int i = 1; i <= max; i++) {
for (int j = 1; j <= max; j++) {
if (j != i) {
booleanMap.put(new Position(i, j), Boolean.FALSE);
}
}
}
for (int i = 1; i <= max; i++) {
for (int j = 1; j <= max; j++) {
if ((i != basicL && j == basicL) || (i == basicL && j != i)) {
booleanMap.replace(new Position(i, j), Boolean.TRUE);
}
}
}
static class Position implements Comparable<Position> {
private final int x;
private final int y;
public Position(final int x, final int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public int compareTo(final Position o) {
final int res = Integer.compare(getX(), o.getX());
return (res != 0) ? res : Integer.compare(getY(), o.getY());
}
@Override
public String toString() {
return x + "-" + y;
}
}
关于java - 如何使用包含数字的键对 TreeMap<String, Boolean> 的 keySet() 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67807332/