2017-06-10 56 views
0

如何從給定對象值的Java映射中移除元素。從java中給定對象值的映射中移除鍵值對

示例數據:

{ 
3cc612eqwdwc3-4dce-11e7-b4a2-fe984uyucc15272:[code=5 
    user_key=dcfb5f5f-588e-11e6-85b9-fe984cc15272 
    message=You have successfully logged in... 
    roles_name=PLATFORM_ADMIN 
    access_token=3cc612c3-4dce-11e7-b4a2-fe984cc15272 
    user_id=superadmin 
    utc_time=2017-06-10 11:16:23.0 
    roles_id=7 
    token_type=0 
    access_key=d3cd4e3f-5946-11e6-9bb0-fe984cc15272 
    status=true 
    is_valid=null 
    ] 
3cc61fdfff2c3-4dce-11e7-b4atuy2-fe984cc15272:[code=5 
    user_key=dcfb5f5f-588e-11e6-85b9-fe984cc15272 
    message=You have successfully logged in... 
    roles_name=PLATFORM_ADMIN 
    access_token=3cc612c3-4dce-11e7-b4a2-fe984cc15272 
    user_id=superadmin 
    utc_time=2017-06-10 11:16:23.0 
    roles_id=7 
    token_type=0 
    access_key=d3cd4e3f-5946-11e6-9bb0-fe984cc15272 
    status=true 
    is_valid=null 
    ] 
3cc612cddf3-4dce-11e7-b4a562-fe984cc15272:[code=5 
    user_key=dcfb5f5f-5ddd88e-11e6-85b9-fe984cc15272 
    message=You have successfully logged in... 
    roles_name=PLATFORM_ADMIN 
    access_token=3cc612c3-4dce-11e7-b4a2-fe984cc15272 
    user_id=superadmin 
    utc_time=2017-06-10 11:16:23.0 
    roles_id=7 
    token_type=0 
    access_key=d3cd4e3f-5946-11e6-9bb0-fe984cc15272 
    status=true 
    is_valid=null 
    ] 
3cc6f12c3-4dce-11e7-b4oopa2d-fe984cc15272:[code=5 
    user_key=dcfb5f5f-588e-11e6-85b9-fe984cc15272 
    message=You have successfully logged in... 
    roles_name=PLATFORM_ADMIN 
    access_token=3cc612c3-4dce-11e7-b4a2-fe984cc15272 
    user_id=superadmin1 
    utc_time=2017-06-10 11:16:23.0 
    roles_id=7 
    token_type=0 
    access_key=d3cd4e3f-5946-11e6-9bb0-fe984cc15272 
    status=true 
    is_valid=null 
    ] 
3cc61vc2c3-4dcdse-11ep[7-b4a2-fe984cc15272:[code=5 
    user_key=dcfb5f5f-5ddd88e-11e6-85b9-fe984cc15272 
    message=You have successfully logged in... 
    roles_name=PLATFORM_ADMIN 
    access_token=3cc612c3-4dce-11e7-b4a2-fe984cc15272 
    user_id=superadmin1 
    utc_time=2017-06-10 11:16:23.0 
    roles_id=7 
    token_type=0 
    access_key=d3cd4e3f-5946-11e6-9bb0-fe984cc15272 
    status=true 
    is_valid=null 
    ] 
} 

對於上面的樣本數據,是否有任何方式除去含目的值user_key=dcfb5f5f-588e-11e6-85b9-fe984cc15272所有地圖要素,而不使用環或一些其它最優化的方式。

我已經通過使用remove()方法嘗試了此方法,但它只是使用密鑰刪除。

+1

只需循環瀏覽您的地圖。這是唯一的方法。 –

+1

可能重複https://stackoverflow.com/questions/9527565/how-to-remove-entry-from-hashmap-by-value – dehasi

+0

@dehasi:感謝您的支持。它適用於字符串鍵和字符串值。請幫助刪除地圖中的對象值。 –

回答

0

嗯,可能我不是正確的。但似乎你無法通過關鍵或價值去除。我建議使用過濾代替。基於Java8的僞代碼:

Map<Key, Value> filteredMap = map.entrySet() 
.stream() 
.filter(e -> !e.getValue().getUserKey().equals("dcfb5f5f-588e-11e6-85b9-fe984cc15272")) 
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));