Android-使用不同的数据类型填充哈希图

时间:2018-07-03 04:25:20

标签: android hashmap

是否可以像这样填充哈希图?

final HashMap<String, String> map = new HashMap<>();
            map.put("path", path);
            map.put("tableName", "table");
            map.put("fileType", fileType);

final HashMap<String, String> option = new HashMap<>();
            map.put("option", option.put("header", "true"));

或者还有其他比这更好的方法吗?因为当我尝试打印“地图”时,键“选项”中没有任何值。

预先感谢

3 个答案:

答案 0 :(得分:1)

您不能这样做,必须正确指定数据类型,

final HashMap<String, String> map = new HashMap<>();
        map.put("path", path); //path has to be a variable pointing to a string
        map.put("tableName", "table");
        map.put("fileType", fileType); // filetype has to be a variable pointing to a string

final HashMap<String, String> option = new HashMap<>(); 
        map.put("option", option.put("header", "true")); //this is wrong

HashMap<String,Map<String,String>> option = new HashMap<String,HashMap<String,String>>();
option.put("Key",map);

示例:创建和填充地图

Map<String, Map<String, Value>> outerMap = new HashMap<String, HashMap<String, Value>>();
Map<String, Value> innerMap = new HashMap<String, Value>();    
innerMap.put("innerKey", new Value());

存储地图

outerMap.put("key", innerMap);

获取地图及其值

Map<String, Value> map = outerMap.get("key");
Value value = map.get("innerKey"); 

答案 1 :(得分:1)

var NUM =  readInt("How many cards will you draw from the deck?");
var RANDOM = Randomizer.nextBoolean();
var DIAMONDS = "Diamonds";
var HEARTS = "Hearts";
var SPADES = "Spades";
var CLUBS = "Clubs";

function start(){
    var take = takeCard();
    printArray(take);
    countCards(take);
}

// This function will pick a random card from the deck.
//Needs Work
function takeCard(){
    var pick = [];
    for(var i = 0; i < NUM; i++){
    if(Randomizer.nextBoolean()){
        pick.push(DIAMONDS);
        }else{
            pick.push(HEARTS);
            pick.push(SPADES);
            pick.push(CLUBS);
        }
    }
    return pick;
}

// Displays the array
function printArray(arr){
    for(var i = 0; i < arr.length; i++){
        println("Flip Number " + (i+1) + ": " + arr[i]);
    }
}

//Counts the number of Cards in each suite
//Needs Work
function countCards(take){
    var countOne = 0;
    var countTwo = 0;
    var countThree = 0;
    var countFour = 0;
    for(var i = 0; i < take.length; i++){
        if(take[i] == "Heads"){
        countOne++;
    } else {
        countTwo++;
    }
    }
    println("Number of Diamonds: " + countOne);
    println("Number of Hearts: " + countTwo);
    println("Number of Spades: " + countThree);
    println("Number of Clubs: " + countFour);
}

您可以在 android studio-java 中使用 HashMap 使用不同的数据类型

答案 2 :(得分:0)

您可以通过以下方式存储数据:

    public static void main(String[] args){

      Map<String, String> dataMap = new HashMap<>();
      dataMap.put("key1", "Hello");
      dataMap.put("key2", "Hello2");

      Map<String, Object> map = new HashMap<>();
      map.put("1", 1);
      map.put("2", dataMap);
      map.put("3", "Value3");

      Object obj = map.get("1");
      printData(obj);

      Object obj2 = map.get("2");
      printData(obj2);

      Object obj3 = map.get("3");
      printData(obj3);

   }

    private static void printData(Object obj) {
    if (obj instanceof Integer) {
        Integer integer =convertInstanceOfObject(obj, Integer.class);
        System.out.println(integer);
    }else if( obj instanceof HashMap){

        HashMap<String, String> resMap = convertInstanceOfObject(obj, HashMap.class);

        System.out.println(resMap);
    }else if( obj instanceof String ){
        String data = convertInstanceOfObject(obj, String.class);

        System.out.println(data);
    }
}


public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) {
    try {
        return clazz.cast(o);
    } catch(ClassCastException e) {
        return null;
    }
}

输出:

1
{key1=Hello, key2=Hello2}
Value3

map类型为Map<String, Object>,因为其值可以是任何类型的Object