2016-10-10 98 views
2

我是java 8的新手,並且有創建對象時需要進行一些檢查的情況。 我當然在使用這個流,並且在完成這個過程中經歷了艱難的時間。使用Java 8流處理嵌套的if/else語句

輸入是一個包含鍵/值對的HashMap對象,輸出應該在下面。

| userrole | userid | username | output | 
|------------|--------|----------|----------| 
| "" (blank) | 111 | amathews | 111  | 
| ""   |  | amathews | amathews | 
| Admin  | 111 | amathews | 111  | 
| Admin  | 111 | ""  | 111  | 
| Admin  |  | amathews | Admin | 

這是如何確定userid> userrole>用戶名。

每個HashMap對象都將包含userrole/username/userid作爲鍵和其值以及其他鍵/值對。 我們將有大量嵌套的if/else語句來完成此任務的Java之前的版本。

我們將有一堆嵌套的if/else語句來完成此前的Java版本中的任務。

這是我到目前爲止的代碼。

map.entrySet().stream() 
     .filter(e -> e.getValue() instanceof String || e.getValue() instanceof Integer) 
     .filter(e -> e.getKey().contains("userrole") || e.getKey().contains("userid") || e.getKey().contains("username")) 
     .map(e -> e.getValue()) 
     .collect(Collectors.toList()); 

我知道我在流中寫入map函數的方式也不正確。 如何在java 8中完成此操作?我不知道如何在這裏添加嵌套的if/else部分。

請有人幫我在這裏我卡住了,無法繼續。謝謝。

編輯:對不起,如果我沒有明確說明問題。這裏是代碼snippiet。

public List<UserAction> getUserActionList(Map<String, String> map) 
    { 
     String userRole = map.get("userrole"); 
     String userName = map.get("username"); 
     String userId = map.get("userid"); 

     String output = null; 
     // if userrole, userid and username are not null/empty, then output is userid 
     if(!checkForNullEmpty(userRole) && !checkForNullEmpty(userId) && !checkForNullEmpty(userName)) 
      output = userId; 
     // if userrole and userid are null/empty and username is not empty/null, then output is username 
     else if(checkForNullEmpty(userRole) && checkForNullEmpty(userId) && !checkForNullEmpty(userName)) 
      output = userName; 
     // if userid and username are null/empty and userrole is not empty/null, then output is userrole 
     else if(!checkForNullEmpty(userRole) && checkForNullEmpty(userId) && checkForNullEmpty(userName)) 
      output = userRole; 

     List<UserAction> udList = new ArrayList<>(); 
     // Add the map and output into a UserAction object 
     udList.add(new UserAction(map, output)); 

     return udList; 

    } 

我已經在這裏按照表格處理了三個條件。所以這必須重構爲使用Java 8流。希望現在有意義。

+1

什麼是你的實際問題?您發佈的代碼是否無效?如果不是,爲什麼不呢?如果它確實有效,你究竟在問什麼?也許發佈一些帶有示例數據的可執行代碼以及期望的數據類型應該是什麼 – Bohemian

+0

感謝您的回覆。我已經添加了片段,希望現在清楚。 – 15R6

+2

它是否保證至少有一個值會在地圖中找到? – Bohemian

回答

3

如果值的至少一個保證,你可以重構它是這樣的:

public List<UserAction> getUserActionList(Map<String, String> map) { 
    return Stream.of("userid", "username", "userrole") 
     .map(map::get) 
     .filter(s -> !checkForNullEmpty(s)) 
     .limit(1) 
     .map(output -> new UserAction(map, output)) 
     .collect(Collectors.toList()); 
} 

如果保證至少有一個值將非空,這是一個有點醜陋,但不是太糟糕了:

public List<UserAction> getUserActionList(Map<String, String> map) { 
    return Stream.of("userid", "username", "userrole") 
     .map(map::get) 
     .filter(s -> !checkForNullEmpty(s)) 
     .limit(1) 
     .map(output -> new UserAction(map, output)) 
     .map(Collections::singletonList) 
     .findFirst() 
     .orElseGet(() -> Arrays.asList(new UserAction(map, null))); 
} 
+0

感謝您使用@bohemian。我只是想了解它是如何工作的。因此,首先我們要創建一個必需的鍵的流,然後我們將從地圖對象中獲取這個值,過濾掉空/空值,然後限制其中一個值,創建一個UserAction對象並最終收集所有的對象列表..我不明白的是它將如何優先考慮Ie用戶ID>的UserRole>用戶名?按照我所使用的表格的含義,如我的代碼片段所示。所以這將成爲嵌套if/else right ... – 15R6

+1

@ 15R6你基本上已經知道了,但注意'.limit(1)'。這意味着流在收集第一個元素後停止。第一次擊中後流中的任何物品甚至都沒有穿過。例如,如果'map.get'userid「)'返回一個值,那麼不會再調用'map.get()'。溪流一次通過一個元素一路燃燒。在進入下一步之前,他們不會通過所有元素的步驟。希望這有助於。 – Bohemian

1

這不是真的清楚你需要完成的任務,但在一般情況下,你需要在你的if語句來寫,你可以用filter()方法從Stream API做的一切。然後,在map()方法中,您將具有需要使用數據完成的確切邏輯(例如將其轉換爲其他類型或獲取所需的值)。使用collect()方法來創建來自Stream的結果,例如,列表,設置,地圖,單個對象或其他任何東西。例如:

map.entrySet().stream() 
       .filter(e -> { 
        // filter the data here, so if isStrOrInt or containsUserData is false - we will not have it in map() method 
        boolean isStrOrInt = e.getValue() instanceof String || e.getValue() instanceof Integer; 
        boolean containsUserData = e.getKey().contains("userrole") || e.getKey().contains("userid") || e.getKey().contains("username"); 
        return isStrOrInt && containsUserData; 
       }) 
       .map(e -> { 
        if (e.getKey().contains("userrole")) { 
         // do something 
        } 
        // some more logic here 
        return e.getValue(); 
       }) 
       .collect(Collectors.toList()); 
       // or e.g. .reduce((value1, value2) -> value1 + value2); 

如果你需要創建在最後一個對象,你可能需要reduce()方法。我建議您查看reduction operations,瞭解有關Stream API的一般信息,瞭解它們的工作原理。

+0

感謝您的回覆@ yuriy。是的,我明白你的回答。我也會嘗試這種方式。在使用代碼片段查看編輯後的問題之後的任何其他建議? – 15R6