哈希映射中不必要地重复的值

Values repeating in the hash map unnecessarily

主要计算:

for (String keyElement : mKeys) {

            int index = str_mFormatedString.indexOf(keyElement);
            sendTheDataToMap(keyElement, page, index);

            while (index >= 0) {  // indexOf returns -1 if no match found
                index = str_mFormatedString.indexOf(keyElement, index + 1);


                if (index >= 0) {
                    sendTheDataToMap(keyElement, page, index);
                }
            }
        }

sendDataToMap 函数:

private void sendTheDataToMap(String key, int page, int index) {
    Pair mPair = new Pair(page, index);
    map.putIfAbsent(key, new ArrayList<>());
    map.get(key).add(mPair);
    //  System.out.println("Entry added to the map....");
}

读图函数:

 private void readMap() {
    for (Map.Entry<String, ArrayList<Pair<Integer, Integer>>> ee : map.entrySet()) {
        String key = ee.getKey();
        ArrayList<Pair<Integer, Integer>> values = ee.getValue();
        // Process the values

        System.out.print(key + " | ");
        for (Pair value : values)
            System.out.print(" " + value.getPage() + "." + value.getIndex());
        System.out.println();
    }
}

方法很简单,我从字符串中读取一个键的多个索引,然后使用String,ArrayList<Pair<Integer, Integer>>将其添加到地图中。 我知道我在主要计算或读取导致值重复的地图时犯了一些小错误。

示例输出:

can | 5.167 5.223 5.167 5.223 7.157 7.338 7.751 7.157 7.338 7.751 7.157 7.338 7.751 15.558 16.209 16.436

突出显示的是重复部分。

Point is, I don't wanna write multiple values at first place and if that's not happening here then I don't wanna read multiple values.

有什么帮助吗?

编辑 1: 输入:在 space 上滑动的字符串(基本上是任何字符串)。 例如:你好,你好吗? => ['Hello','how','are','you?']

主要计算前一行:

 mKeys = splitTextToArray(str_mFormatedString);

和函数 splitTextToArray()

private ArrayList<String> splittingTextToArray(String formattedTextInput) {
    String[] tempKeys = formattedTextInput.split("\s+");

    //convert to Arraylist
    ArrayList<String> mKeys = new ArrayList<>(Arrays.asList(tempKeys));
    return mKeys;
}

为您的地图值使用 Set 而不是 List 以避免重复:

Map<String, Set<Pair<Integer, Integer>>> map = new HashMap<>();
// ...
map.putIfAbsent(key, new HashSet<>()); // Or LinkedHashSet to preserve insertion order
map.get(key).add(mPair);

如果您真的 hell-bent 使用列表,请在添加之前检查列表是否尚未包含该值:

Map<String, List<Pair<Integer, Integer>>> map = new HashMap<>();
// ...
map.putIfAbsent(key, new ArrayList<>());

if (!map.get(key).contains(mPair)) {
    map.get(key).add(mPair);
}

// probably should optimize this to get rid of the multiple calls to map.get(key)

必须确保 equals()hashCode() 已为您的 Pair class 正确实施。

Arraylist 可以包含重复值,因此您可以使用 Set 或在 放入 arraylist 之前检查它,如下所示。

1) 声明 Set 而不是 Arraylist

map.putIfAbsent(key, new HashSet<>());

2) 在添加 arralist 之前检查。 (为此你需要覆盖 Pair Class 中的 hascode 和 equals。)

private void sendTheDataToMap(String key, int page, int index) {
    Pair mPair = new Pair(page, index);
    map.putIfAbsent(key, new ArrayList<>());
  if(!map.get(key).contains(mPair){
    map.get(key).add(mPair);
    //  System.out.println("Entry added to the map....");
  }
}