您现在的位置是:首页 > 网站制作 > gogo

Go 语言Map(集合)

胜于蓝2013-12-30【go】人已围观

简介Map 是一种无序的键值对的集合。Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值。Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map

Map 是一种无序的键值对的集合。Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值。bDJ胜于蓝|优秀个人博客

Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它。不过,Map 是无序的,我们无法决定它的返回顺序,这是因为 Map 是使用 hash 表来实现的。bDJ胜于蓝|优秀个人博客

定义 Map

可以使用内建函数 make 也可以使用 map 关键字来定义 Map:bDJ胜于蓝|优秀个人博客

/* 声明变量,默认 map 是 nil */
var map_variable map[key_data_type]value_data_type

/* 使用 make 函数 */
map_variable := make(map[key_data_type]value_data_type)

如果不初始化 map,那么就会创建一个 nil map。nil map 不能用来存放键值对bDJ胜于蓝|优秀个人博客

实例

下面实例演示了创建和使用map:bDJ胜于蓝|优秀个人博客

实例

package mainbDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
import "fmt"bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
func main() {bDJ胜于蓝|优秀个人博客
    var countryCapitalMap map[string]string /*创建集合 */bDJ胜于蓝|优秀个人博客
    countryCapitalMap = make(map[string]string)bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
    /* map插入key - value对,各个国家对应的首都 */bDJ胜于蓝|优秀个人博客
    countryCapitalMap [ "France" ] = "巴黎"bDJ胜于蓝|优秀个人博客
    countryCapitalMap [ "Italy" ] = "罗马"bDJ胜于蓝|优秀个人博客
    countryCapitalMap [ "Japan" ] = "东京"bDJ胜于蓝|优秀个人博客
    countryCapitalMap [ "India " ] = "新德里"bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
    /*使用键输出地图值 */ bDJ胜于蓝|优秀个人博客
    for country := range countryCapitalMap {bDJ胜于蓝|优秀个人博客
        fmt.Println(country, "首都是", countryCapitalMap [country])bDJ胜于蓝|优秀个人博客
    }bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
    /*查看元素在集合中是否存在 */bDJ胜于蓝|优秀个人博客
    capital, ok := countryCapitalMap [ "American" ] /*如果确定是真实的,则存在,否则不存在 */bDJ胜于蓝|优秀个人博客
    /*fmt.Println(capital) */bDJ胜于蓝|优秀个人博客
    /*fmt.Println(ok) */bDJ胜于蓝|优秀个人博客
    if (ok) {bDJ胜于蓝|优秀个人博客
        fmt.Println("American 的首都是", capital)bDJ胜于蓝|优秀个人博客
    } else {bDJ胜于蓝|优秀个人博客
        fmt.Println("American 的首都不存在")bDJ胜于蓝|优秀个人博客
    }bDJ胜于蓝|优秀个人博客
}

以上实例运行结果为:bDJ胜于蓝|优秀个人博客

France 首都是 巴黎
Italy 首都是 罗马
Japan 首都是 东京
India  首都是 新德里
American 的首都不存在

delete() 函数

delete() 函数用于删除集合的元素, 参数为 map 和其对应的 key。实例如下:bDJ胜于蓝|优秀个人博客

实例

package mainbDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
import "fmt"bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
func main() {bDJ胜于蓝|优秀个人博客
        /* 创建map */bDJ胜于蓝|优秀个人博客
        countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
        fmt.Println("原始地图")bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
        /* 打印地图 */bDJ胜于蓝|优秀个人博客
        for country := range countryCapitalMap {bDJ胜于蓝|优秀个人博客
                fmt.Println(country, "首都是", countryCapitalMap [ country ])bDJ胜于蓝|优秀个人博客
        }bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
        /*删除元素*/ delete(countryCapitalMap, "France")bDJ胜于蓝|优秀个人博客
        fmt.Println("法国条目被删除")bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
        fmt.Println("删除元素后地图")bDJ胜于蓝|优秀个人博客
bDJ胜于蓝|优秀个人博客
        /*打印地图*/bDJ胜于蓝|优秀个人博客
        for country := range countryCapitalMap {bDJ胜于蓝|优秀个人博客
                fmt.Println(country, "首都是", countryCapitalMap [ country ])bDJ胜于蓝|优秀个人博客
        }bDJ胜于蓝|优秀个人博客
}

以上实例运行结果为:bDJ胜于蓝|优秀个人博客

原始地图
India 首都是 New delhi
France 首都是 Paris
Italy 首都是 Rome
Japan 首都是 Tokyo
法国条目被删除
删除元素后地图
Italy 首都是 Rome
Japan 首都是 Tokyo
India 首都是 New delhi

基于 go 实现简单 HashMap,暂未做 key 值的校验。bDJ胜于蓝|优秀个人博客

package main

import (
    "fmt"
)

type HashMap struct {
    key string
    value string
    hashCode int
    next *HashMap
}

var table [16](*HashMap)

func initTable() {
    for i := range table{
        table[i] = &HashMap{"","",i,nil}
    }
}

func getInstance() [16](*HashMap){
    if(table[0] == nil){
        initTable()
    }
    return table
}

func genHashCode(k string) int{
    if len(k) == 0{
        return 0
    }
    var hashCode int = 0
    var lastIndex int = len(k) - 1
    for i := range k {
        if i == lastIndex {
            hashCode += int(k[i])
            break
        }
        hashCode += (hashCode + int(k[i]))*31
    }
    return hashCode
}

func indexTable(hashCode int) int{
    return hashCode%16
}

func indexNode(hashCode int) int {
    return hashCode>>4
}

func put(k string, v string) string {
    var hashCode = genHashCode(k)
    var thisNode = HashMap{k,v,hashCode,nil}

    var tableIndex = indexTable(hashCode)
    var nodeIndex = indexNode(hashCode)

    var headPtr [16](*HashMap) = getInstance()
    var headNode = headPtr[tableIndex]

    if (*headNode).key == "" {
        *headNode = thisNode
        return ""
    }

    var lastNode *HashMap = headNode
    var nextNode *HashMap = (*headNode).next

    for nextNode != nil && (indexNode((*nextNode).hashCode) < nodeIndex){
        lastNode = nextNode
        nextNode = (*nextNode).next
    }
    if (*lastNode).hashCode == thisNode.hashCode {
        var oldValue string = lastNode.value
        lastNode.value = thisNode.value
        return oldValue
    }
    if lastNode.hashCode < thisNode.hashCode {
        lastNode.next = &thisNode
    }
    if nextNode != nil {
        thisNode.next = nextNode
    }
    return ""
}

func get(k string) string {
    var hashCode = genHashCode(k)
    var tableIndex = indexTable(hashCode)

    var headPtr [16](*HashMap) = getInstance()
    var node *HashMap = headPtr[tableIndex]

    if (*node).key == k{
        return (*node).value
    }

    for (*node).next != nil {
        if k == (*node).key {
            return (*node).value
        }
        node = (*node).next
    }
    return ""
}

//examples 
func main() {
    getInstance()
    put("a","a_put")
    put("b","b_put")
    fmt.Println(get("a"))
    fmt.Println(get("b"))
    put("p","p_put")
    fmt.Println(get("p"))
}

Tags:

很赞哦! ()

文章评论

当前时间

快速排名

  • 网站建设|万词霸屏,企业软文推广,刷下拉框
  • 快速排名:不用再等SEO三个月,只需3-7天即可把行业关键词覆盖百度搜索引擎首页,点击不收费,排名报表,真实访问量报表一目了然。

合作加盟

  • 扫码请注明来意,否则不会通过
  • 填写商户姓名不要带有“超市”,“便利店” ,“百货”等
  • 扫码成为快钱代理
  • 扫码加站长微信,为您推荐快钱总部负责人
  • 快钱POSS机(电签版)
  • 1,免押版:签约费率快捷交易0.38%,常规交易0.65%
  • 贷记卡单笔≥3000元视为激活
  • 2,,有押版:签约快捷交易0.38%,常规交易0.65%
  • 激活首刷≥99元,扣除99元系统服务费,多出部分shishi到账
  • 电签版ipos参与每月扶持奖励
  • 电签版ipos与Mpos单独考核台均
  • 30台以上有效激活奖励3000元扶持金
  • 当月交易额≥3000元的为活跃用户

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计7074篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 扫描二维码:请注明来意,否则不会通过
  • 微信号:扫描二维码,关注我们
歌名 - 歌手
0:00