简体   繁体   English

哈希图中不必要重复的值

[英]Values repeating in the hash map unnecessarily

Main computation: 主要计算:

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 function: 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....");
}

Readmap function: Readmap功能:

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

Approach is simple, I read the multiple indices of a key from string and add it in to the map with String,ArrayList<Pair<Integer, Integer>> . 方法很简单,我从字符串读取键的多个索引,然后使用String,ArrayList<Pair<Integer, Integer>>将其添加到映射中。 I know I have done some small mistake either in main computation or while reading the map that is resulting in duplication of values. 我知道我在主要计算中或在读取导致值重复的映射时犯了一些小错误。

Example output: 输出示例:

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 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

Highlighted is repeating part. 突出显示的是重复部分。

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. 重点是,我不想一开始就写多个值,如果这里没有发生,那我就不想读多个值。

Any help? 有什么帮助吗?

Edit 1: Inputs: A string (basically any string) which is being slipt on space. 编辑1:输入:在空间上滑动的字符串(基本上是任何字符串)。 For ex: Hello how are you? 例如:你好,你好吗? => ['Hello','how','are','you?'] => [“你好”,“如何”,“是”,“你?”]

One line before Main computation: 主计算前一行:

 mKeys = splitTextToArray(str_mFormatedString);

and the function splitTextToArray() 和功能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;
}

Use a Set instead of a List for your map values to avoid duplicates: 为地图值使用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);

If you're really hell-bent on using a list, check whether the list doesn't already contain the value before adding it: 如果您真的想使用列表,请在添加列表之前检查列表是否尚未包含该值:

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)

Have to make sure that equals() and hashCode() are correctly implemented for your Pair class though. 但是必须确保为您的Pair类正确实现了equals()hashCode()

Arraylist can contain duplicate values so you can either use Set or check it before put to arraylist like below. Arraylist可以包含重复值,因此您可以使用Set或对其进行检查, 然后将放入如下所示的arraylist中。

1) Declare Set instead of Arraylist 1) 声明集而不是Arraylist

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

or 要么

2) check before add in arralist. 2) 在加入讲师之前先检查一下。 (for this you need to overwrite hascode and equals in Pair Class.) (为此,您需要覆盖Hascode和Pair Class中的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....");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM