如何根据key取出map中的value值

如何根据key取出map中的value值,第1张

你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?使用MapEntry类,你可以得到在同一时间得到所有的信息。

标准的Map访问方法如下:

Set

keys

=

mapkeySet(

);

if(keys

!=

null)

{

Iterator

iterator

=

keysiterator(

#include<iostream>

#include<string>

#include<map>

usingnamespacestd;

intmain()

{

multimap<string,int>m_map;

strings("中国"),s1("美国");

m_mapinsert(make_pair(s,50));

m_mapinsert(make_pair(s,55));

m_mapinsert(make_pair(s,60));

m_mapinsert(make_pair(s1,30));

m_mapinsert(make_pair(s1,20));

m_mapinsert(make_pair(s1,10));

//方式1

intk;

multimap<string,int>::iteratorm;

m=m_mapfind(s);

for(k=0;k!=m_mapcount(s);k++,m++)

cout<<m->first<<"--"<<m->second<<endl;

//方式2

multimap<string,int>::iteratorbeg,end;

beg=m_maplower_bound(s1);

end=m_mapupper_bound(s1);

for(m=beg;m!=end;m++)

cout<<m->first<<"--"<<m->second<<endl;

//方式3

beg=m_mapequal_range(s)first;

end=m_mapequal_range(s)second;

for(m=beg;m!=end;m++)

cout<<m->first<<"--"<<m->second<<endl;

return0;

}

扩展资料

map构造函数;

map<string,int>mapstring;map<int,string>mapint;

map<sring,char>mapstring;map<char,string>mapchar;

map<char,int>mapchar;map<int,char>mapint;

如在打枚举中打印“指定值对应的字符串”时,可是采用map<int,string>的STL实现。

JDK 中

view plaincopy to clipboardprint    <FONT color=# ff>Map map = new HashMap();

Iterator it = map entrySet(erator();

while (it hasNext()) {

Map Entry entry = (Map Entry) it next();

Object key = entry getKey();

Object value = entry getValue();

}</FONT>

Map map = new HashMap();

Iterator it = map entrySet(erator();

while (it hasNext()) {

Map Entry entry = (Map Entry) it next();

Object key = entry getKey();

Object value = entry getValue();

}JDK 中 应用新特性For Each循环

view plaincopy to clipboardprint    Map m = new HashMap();

for(Object o : map keySet()){

map get(o);

}

Map m = new HashMap();

for(Object o : map keySet()){

map get(o);

}返回的 set 中的每个元素都是一个 Map Entry 类型

view plaincopy to clipboardprint    <FONT color=# ff>private Hashtable<String String> emails = new Hashtable<String String>();</FONT>

private Hashtable<String String> emails = new Hashtable<String String>();  另外 我们可以先把hashMap 转为集合Collection 再迭代输出 不过得到的对象

view plaincopy to clipboardprint     <FONT color=# ff>//方法一: 用entrySet()

Iterator it = emails entrySet(erator();

while(it hasNext()){

Map Entry m=(Map Entry)it next();

( email + m getKey() + : + m getValue());

}

// 方法二 jdk 支持 用entrySet()和For Each循环()

for (Map Entry<String String> m : emails entrySet()) {

( email + m getKey() + : + m getValue());

}

// 方法三 用keySet()

Iterator it = emails keySet(erator();

while (it hasNext()){

String key;

key=(String)it next();

( email + key + : + emails get(key));

}

// 方法五 jdk 支持 用keySEt()和For Each循环

for(Object m: emails keySet()){

( email + m+ : + emails get(m));

}    </FONT>

//方法一: 用entrySet()

Iterator it = emails entrySet(erator();

while(it hasNext()){

Map Entry m=(Map Entry)it next();

( email + m getKey() + : + m getValue());

}

// 方法二 jdk 支持 用entrySet()和For Each循环()

for (Map Entry<String String> m : emails entrySet()) {

( email + m getKey() + : + m getValue());

}

// 方法三 用keySet()

Iterator it = emails keySet(erator();

while (it hasNext()){

String key;

key=(String)it next();

( email + key + : + emails get(key));

}

// 方法五 jdk 支持 用keySEt()和For Each循环

for(Object m: emails keySet()){

( email + m+ : + emails get(m));

}

Map    aa    =    new    HashMap();      aa put( tmp     new    Object());      //追加      替换用同样的函数       aa remove( temp );                        //删除      for    (Iterator    i    =    aa values(erator();    i hasNext();    )    {              Object    temp    =    i next();      }          //遍历    来个完整的 包含TreeSet的元素内部排序的

view plaincopy to clipboardprint    public static void main(String[] args) {

ArrayList<String> list = new ArrayList<String>();

HashMap<Object Object> hash = new HashMap<Object Object>();

TreeMap<Object Object> treeMap = new TreeMap<Object Object>();

list add( a );

list add( b );

list add( c );

hash put( );

hash put( );

hash put( );

hash put( );

hash put( );

hash put( );

treeMap put( );

treeMap put( );

treeMap put( );

treeMap put( );

treeMap put( );

treeMap put( );

//list遍历

for(String m: list){

System out println(m);

}

// hashmap entrySet() 遍历

for(Map Entry<Object Object> m: hash entrySet()){

System out println(m getKey()+ +m getValue());

}

//hashmap keySet() 遍历

for(Object m: hash keySet()){

System out println(m+ +hash get(m));

}

// treemap keySet()遍历

for(Object m: treeMap keySet()){

System out println(m+ +treeMap get(m));

}

lishixinzhi/Article/program/Java/hx/201311/25783

1

2

3

4

5

6

7

8

public static void main(String[] args) {

Map<String, String> map = new HashMap<String, String>();

mapput("1", "v1");

mapput("2", "v2");

for (String key : mapkeySet()) {

Systemoutprintln("key= " + key + " and value= " + mapget(key));

}

}

取key和value

你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?使用MapEntry类,你可以得到在同一时间得到所有的信息。

标准的Map访问方法如下:

Set

keys

=

mapkeySet(

);

if(keys

!=

null)

{

Iterator

iterator

=

keysiterator(

);

while(iteratorhasNext(

))

{

Object

key

=

iteratornext(

);

Object

value

=

mapget(key);

;…

;}

}

然后,这个方法有一个问题。从Map中取得关键字之后,我们必须每次重复返回到Map中取得相对的值,这是很繁琐和费时的。

幸运的是,这里有一个更加简单的途径。Map类提供了一个称为entrySet()的方法,这个方法返回一个MapEntry实例化后的对象集。

接着,MapEntry类提供了一个getKey()方法和一个getValue()方法,因此,上面的代码可以被组织得更符合逻辑。举例如下:

Set

entries

=

mapentrySet(

);

if(entries

!=

null)

{

Iterator

iterator

=

entriesiterator(

);

while(iteratorhasNext(

))

{

MapEntry

entry

=iteratornext(

);

Object

key

=

entrygetKey(

);

Object

value

=

entrygetValue();

;…

}

}

尽管增加了一行代码,我们却省略了许多对Map不必要的"get"调用。同时,提供给开发人员一个同时保持了关键字和其对应的值的类。MapEntry同时也提供了一个setValue()方法,程序员可以使用它修改map里面的值。

Hashtable内部排列的方式是散列排布,所以当输出信息时会是无序的。为了能保证输出的数据按照顺序排列,不要渴望用java自带的函数来对

Hashtable对象进行调整处理。当我们获取Hashtable里的KEY和VALUE时,一般都运行了MapEntry类来转换,好,现在就用这

个类来作文章,我具体写了一个方法。

代码:

/

方法名称:getSortedHashtable

参数:Hashtable

h

引入被处理的散列表

描述:将引入的hashtableentrySet进行排序,并返回

/

public

static

MapEntry[]

getSortedHashtable(Hashtable

h){

Set

set

=

hentrySet();

MapEntry[]

entries

=

(MapEntry[])settoArray(new

MapEntry[setsize()]);

Arrayssort(entries,new

Comparator(){

public

int

compare(Object

arg0,

Object

arg1)

{

Object

key1

=

((MapEntry)arg0)。getKey();

Object

key2

=

((MapEntry)arg1)。getKey();

return

((Comparable)key1)。compareTo(key2);

}

});

return

entries;

}

调用这个方法:

MapEntry[]

set

=

getSortedHashtable(t);

//perportyTable

for

(int

i=0;i<setlength;i++){

Systemoutprintln(set[i]getKey()。toString());

Systemoutprintln(set[i]getValue()。toString());

}

for(Iterator it=mapkeySet();ithasNext();) { Systemoutprintln(key+":"+itnext()); Systemoutprintln(value+":"+mapget(itnext() ) ); } 哥一向这么狠,呵呵~~~~

hm已是一个HashMap的引用

如果你知道当前的这个key,可以通过hmget(key)方法来获得value

获得key的方法hmkeySet();因为你不知道key是哪个其实该方法就是获得一个key的集合

具体可以结合以下例子看看,里面有个迭代器用于遍历的

Set s=hmkeySet();//通过keySet方法可获得所有key的集合,放在一个容器Set里面

Iterator it=siterator();//获得一个迭代器引用it,通过siterator方法好比使“指针”指向

//set里面的第一个元素的位置

while(ithasNext())//set里面如果有下一个

{

Integer key=itnext();//返回当前set中的这个元素(因为set中都是放的key,“指针”指向下一个

Systemoutprintln(hmget(key));//利用hmget(key)方法获得该key对应的value

}

以上就是关于如何根据key取出map中的value值全部的内容,包括:如何根据key取出map中的value值、c++ 输出map每个元素的值、Java中怎样遍历Map的所有的元素等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/10185599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存