2017-08-12 43 views
0

我有两个要求,我只能使用地图。我应该使用哪个地图来满足我的要求?

  1. 重复键是允许的。(这个我使用多重映射)

  2. 我必须寻找一个外卡。 例如:如果keyset包含“Dave”和“Dave bautista”,它将返回两者。

如何实现两者?

+0

从你写的我可以说你应该使用搜索字段作为地图值,而不是键。所以你可以用通配符重复和实现这样的搜索。无论如何,由于您没有特定的编程问题,因此您的帖子不在此处。 –

回答

0
// Returning Map 
    Map<String, Object> filterObjects = new HashedMap(); 

    // Input List Map 
    Map<String, Object> map = new HashedMap(); 
    map.put("Dave", null); 
    map.put("Dave bautista", null); 

    // User requesting Key Name 
    String userInput = "Dave bautista"; 

    for (int i = 0; i < map.keySet().size(); i++) { 
     //KeyNames retrieve as element by element from the map 
     String keyName = map.keySet().toArray()[i].toString(); 

     //checks the availability of the userInput with element name 
     if (keyName.contains(userInput)) { 
      // If matched, then put it in to the return Map 
      filterObjects.put(keyName, map.get(keyName)); 
     } 

    } 

    return filterObjects; 
相关问题