gpt4 book ai didi

java - 在Java中如何创建关联数组结构并排序,同时保留具有不同值对的重复键

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:39 24 4
gpt4 key购买 nike

存在两个逗号分隔的字符串。

第一个字符串本质上是键,第二个字符串是链接的值,

第一个字符串需要按升序排列,同时保留任何重复的值,第二个字符串需要遵循升序顺序以保持顺序。

到目前为止,查看了 HashMap 和元组,但没有成功。系统是Java 6

String A = "3, 4, 1, 2, 3"
String B = "19, 24, 32, 68, 50"

需要结果输出

String A = "1, 2, 3, 3, 4"
String B = "32, 68, 19, 50, 24"

最佳答案

您有多种可能性来实现您的需求。以下是两个示例(在示例代码中并行存在):

  1. 您可以使用Map<Integer, List<Integer>>包含一个键及其在 List 中拥有的所有值
  2. 您可以创建一个只包含一个键和一个值的 POJO 类,但您需要使其可排序/可比较并使用合适的数据结构

查看此示例并注意代码注释:

public class StackoverflowMain {

public static void main(String[] args) {
String a = "3, 4, 1, 2, 3";
String b = "19, 24, 32, 68, 50";

// Map-approach: use a map that maps a key to a list of values
Map<Integer, List<Integer>> ab = new TreeMap<>();
// Pair-approach: make a sortable POJO that holds a key and a value only
// and create a data structure that holds them sorted
SortedSet<Pair> pairList = new TreeSet<Pair>();

// split both Strings by comma
String[] aSplit = a.split(",");
String[] bSplit = b.split(",");

// check if the length of the resulting arrays is the same
if (aSplit.length == bSplit.length) {
// if yes, go through the arrays of numbers
for (int i = 0; i < aSplit.length; i++) {
int key = Integer.parseInt(aSplit[i].trim());
int value = Integer.parseInt(bSplit[i].trim());

// in the Pair-approach, you just have to create a Pair with the value found
Pair pair = new Pair(key, value);
// and add it to the set of pairs
pairList.add(pair);

// the following check is only needed for the Map-solution
if (ab.containsKey(key)) {
// if the key is already present,
// just add the new value to its value list
ab.get(key).add(value);
// sort the value list each time a new value has been added
ab.get(key).sort(Comparator.naturalOrder());
} else {
// if the key is not present in the Map so far,
// create a new List for the value
List<Integer> valueList = new ArrayList<>();
// add the value to that list
valueList.add(value);
// and put both into the Map
ab.put(key, valueList);
}
}
} else {
System.err.println("The Strings have different amounts of elements!");
}

// print what's in the Map
System.out.println("Map-approach:");
for (int key : ab.keySet()) {
List<Integer> value = ab.get(key);
for (int val : value) {
System.out.println(key + " : " + val);
}
}

System.out.println("————————————————");

System.out.println("Pairs-approach:");
for (Pair pair : pairList) {
System.out.println(pair.key + " : " + pair.val);
}
}

/**
* This class is needed for the Pair-approach.
* It is comparable (and by that, sortable) and will be sorted by key
* and if the keys are equal, it will sort by value.
*/
static class Pair implements Comparable<Pair> {
int key;
int val;

Pair(int key, int value) {
this.key = key;
this.val = value;
}

@Override
public int compareTo(Pair otherPair) {
if (key == otherPair.key) {
if (val == otherPair.val) {
return 0;
} else if (val < otherPair.key) {
return -1;
} else {
return 1;
}
} else if (key < otherPair.key) {
return -1;
} else {
return 1;
}
}
}
}

此代码产生以下输出:

Map-approach:
1 : [32]
2 : [68]
3 : [19, 50]
4 : [24]
————————————————
Pairs-approach:
1 : 32
2 : 68
3 : 19
3 : 50
4 : 24

编辑

Pair -方法排序不正确,我想出了这个Map -方法:

public class StackoverflowMain {

public static void main(String[] args) {
String a = "3, 4, 1, 3, 3, 2, 3";
String b = "5, 24, 35, 99, 32, 68, 19";

// Map-approach: use a map that maps a key to a list of values
Map<Integer, List<Integer>> ab = new TreeMap<>();

// split both Strings by comma
String[] aSplit = a.split(",");
String[] bSplit = b.split(",");

// check if the length of the resulting arrays is the same
if (aSplit.length == bSplit.length) {
// if yes, go through the arrays of numbers
for (int i = 0; i < aSplit.length; i++) {
int key = Integer.parseInt(aSplit[i].trim());
int value = Integer.parseInt(bSplit[i].trim());

// the following check is only needed for the Map-solution
if (ab.containsKey(key)) {
// if the key is already present, just add the new value to its value list
ab.get(key).add(value);
// sort the value list each time a new value has been added
ab.get(key).sort(Comparator.naturalOrder());
} else {
// if the key is not present in the Map so far, create a new List for the value
List<Integer> valueList = new ArrayList<>();
// add the value to that list
valueList.add(value);
// and put both into the Map
ab.put(key, valueList);
}
}
} else {
System.err.println("The Strings have different amounts of elements!");
}

// print what's in the Map
System.out.println("Map-approach:");
for (int key : ab.keySet()) {
List<Integer> value = ab.get(key);
for (int val : value) {
System.out.println(key + " : " + val);
}
}
}
}

它更短并使用 Map<Integer, List<Integer>>并对 List<Integer> 进行排序每次添加新值时(除了第一个值,它不需要排序)。这需要在输出代码中进行另一个循环,但您不必创建新类。

它产生以下输出:

Map-approach:
1 : 35
2 : 68
3 : 5
3 : 19
3 : 32
3 : 99
4 : 24

关于java - 在Java中如何创建关联数组结构并排序,同时保留具有不同值对的重复键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com