2016-04-11 6 views
0

1つの値だけを持つ値のペアを一意に格納する。JavaのStore(UniqueString、String)

入力: 追加(文字列、文字列)

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

予想される出力:

49.205.119.239, hello 
14.192.212.57, yollo 

私は以下のコードを試してみました。 uniqueIp.putIfAbsent(line, actualLine);は、すべての値を繰り返しキー(線)で均等に取ります。

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

if(!uniqueIp.containsKey(key))を試してみました。 did not work

+0

キーをあなたのハッシュマップは 'Set'で、あなたは' String'を保存しようとしています、どうしますか? –

+0

代わりにHashMap >を使用してください –

+0

まあ...上記のサンプルコードで 'line'は初期化されておらず、nullです。 – pczeus

答えて

2

、単にputIfAbsent()方法を使用しますの

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

Apache Antのようなロジックを実装しているように見えます。一度設定するとプロパティは一定ですが、それ以上の変更は効果がありません(一度a1 = "hello"、後で "tello"を設定しても問題ありません)。その後

シンプル:

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

...

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

変更

HashMap<Set<String>,String> 

から

HashMap<String,String> 
にご uniqueIp地図の署名

さらに、キーがマップ内にある場合、値を更新したくないと仮定します。

もしそうなら、マップに新しいキー値のペアをマップに入れる前に、マップに既にキーが含まれているかどうかを確認する必要があります。他の回答として

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

あなただけのキーの値が更新されるべきではないときっぱりとした後、キーの値を設定しようとしている、指摘しています。したがって、単純なMap<String,String>でこれを達成できます。ここで

は、あなたの入力と期待される出力を示す完全なコードサンプルです:代わりに、誰もが示唆するようcontainsKey()を呼び出して警備の

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