简体   繁体   English

将(UniqueString,String)存储在Java中

[英]Store (UniqueString, String) in java

To store a pair of values with only one value as unique. 存储只有一个值唯一的一对值。

Input: add(String, String) 输入:add(String,String)

49.205.119.239, hello
14.192.212.57, yollo
49.205.119.239, tello
14.192.212.57, bella

Expected Output: 预期产量:

49.205.119.239, hello
14.192.212.57, yollo

I tried the below code. 我尝试了以下代码。 The uniqueIp.putIfAbsent(line, actualLine); uniqueIp.putIfAbsent(line, actualLine); takes all the values even repeated keys (line). 取所有值,甚至重复的键(行)。

public static void processLine(String actualLine) throws IOException {
  GetLocationExample obj = new GetLocationExample();
  Map<String, String> uniqueIp = new HashMap<String, String>();
  Pattern pattern = Pattern.compile("([(]100.0)[)][\\]][:]*");
  Matcher matcher = pattern.matcher(actualLine);
  String ip = null;
  String line = null ;
  if (matcher.find())
  {
       ip= "100%";
       Pattern pattern1 = Pattern.compile("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
      Matcher matcher1 = pattern1.matcher(actualLine);

      if (matcher1.find())
      { line= (matcher1.group(0));
        uniqueIp.putIfAbsent(line, actualLine);  
      }

      else { System.out.println("Not Present"); }
  }
  else { return; }

  for (String name: uniqueIp.keySet())
  { 
      String key =name.toString();
      String value = uniqueIp.get(name).toString();  
      System.out.println(key + " " + value);  
  } 

Tried if(!uniqueIp.containsKey(key)) . 尝试过if(!uniqueIp.containsKey(key)) Did not work 不工作

Instead of guarding with a call to containsKey() as everyone suggests, simply use the putIfAbsent() method: 与其像每个人所建议的那样通过调用containsKey()来保护,不如使用putIfAbsent()方法:

Map<String,String> map = new HashMap<>();
map.putIfAbsent("a1", "hello");
map.putIfAbsent("b1", "yollo");
map.putIfAbsent("a1", "tello");
map.putIfAbsent("b1", "bella");

Looks like you implement Apache Ant-like logic: the property is constant if once set, further changes have no effect (once a1="hello", setting "tello" later does not matter). 看起来您实现了类似Apache Ant的逻辑:如果设置了属性,则该属性为常数,则进一步的更改将不起作用(一旦a1 =“ hello”,则以后设置“ tello”都没有关系)。

Then simple: 那么简单:

  Map<String, String> map = new HashMap<>();

... ...

  if (!map.containsKey(key)) 
    map.put(key, value);

Change the signature of your uniqueIp Map from 从更改您的uniqueIp地图的签名

HashMap<Set<String>,String> 

to

HashMap<String,String>

Furthermore, I assume you don't want to update a value if the key is already in your map. 此外,如果该键已经在地图中,我假设您不想更新值。

If so, you have to check if the map already contains the key before putting a new key value pair into your map. 如果是这样,则必须在将新的键值对放入映射中之前检查映射是否已包含密钥。

if(!uniqueMap.contains(key)) {
     uniqueMap.put(key, value);
}

As other answers have pointed out, you are trying to only set the value of a key once and after that the value for a key should not be updated. 正如其他答案所指出的那样,您尝试只设置一次键的值,然后再不更新键的值。 Therefore, you can accomplish this with a simple Map<String,String> . 因此,您可以使用简单的Map<String,String>完成此操作。

Here is a complete code sample showing your input and expected output: 这是显示您的输入和预期输出的完整代码示例:

import java.util.HashMap;
import java.util.Map;

public class TestUniqueStrings {
    Map<String, String> storage = new HashMap<>();

    /**
     * Attempts to add a unique value under key.
     * @param key
     * @param value
     * @return True if the key did not exist
     */
    boolean add(String key, String value){
        if(!storage.containsKey(key)){
            storage.put(key, value);
            return true;
        }
        return false;
    }

    public void printStorage(){
        System.out.println("Contents of storage:\n" + storage);
    }

    public static void main(String[] args) {
        TestUniqueStrings tus = new TestUniqueStrings();
        tus.add("a1", "hello");
        tus.add("b1", "yollo");
        tus.add("a1", "tello");
        tus.add("b1", "bella");

        tus.printStorage();
    }
}

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

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