java Map
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java Map,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3521字,纯文字阅读大概需要6分钟。
内容图文
https://www.cnblogs.com/lzq198754/p/5780165.html
Map.一组成对的"键值对"对象,允许你使用键来查找值,ArrayList允许你使用数字来查找值,因此在某种意义上讲,它将数字与对象关联在了一起.映射表允许我们使用另一个对象,它也被称为"关联数组",因为它将某些对象与另外一些对象关联在了一起,或者被称为"字典",因为你可以使用键对象来查找值对象,就像在字典中使用单词来定义一样,Map是强大的编程工具
将对象映射到其它对象的能力是一种解决编程问题的杀手锏,例如考虑一个程序,它将接受来自Java的Random类到随机性,在理想状态下,Random可以产生理想的数字分布,但要向测试它,则需要产生大量的随机数,不对落入各种不同范围的数字进行计数,Map很容易可以解决该问题,
// : holding/Statistics.java // Simple demonstration of HashMap. package object; import java.util.*; publicclass Statistics { publicstaticvoid main(String[] args) { Random rand = new Random(47); Map<Integer,Integer> m = new HashMap<Integer,Integer>(); for(int i = 0; i < 10000; i++) { // Produce a number between 0 and 20:int r = rand.nextInt(20); Integer freq = m.get(r);//get()返回键的值,put()设置键和值 m.put(r, freq == null ? 1 : freq + 1);//键是Random产生的值,值是该数字的字数 } System.out.println(m); } } /* Output: {15=497, 4=481, 19=464, 8=468, 11=531, 16=533, 18=478, 3=508, 7=471, 12=521, 17=509, 2=489, 13=506, 9=549, 6=519, 1=502, 14=477, 10=513, 5=503, 0=481} *///:~
下列示例演示了String
// : holding/PetMap.java package object; import typeinfo.pets.*; import java.util.*; importstatic net.mindview.util.Print.*; publicclass PetMap { publicstaticvoid main(String[] args) { Map<String,Pet> petMap = new HashMap<String,Pet>(); petMap.put("My Cat", new Cat("Molly")); petMap.put("My Dog", new Dog("Ginger")); petMap.put("My Hamster", new Hamster("Bosco")); print(petMap); Pet dog = petMap.get("My Dog"); print(dog); print(petMap.containsKey("My Dog"));//检查容器中是否包含键"My Dog" print(petMap.containsValue(dog)); //检查容器中是否包含value"dog"
} } /* Output: {My Cat=Cat Molly, My Hamster=Hamster Bosco, My Dog=Dog Ginger} Dog Ginger true true */// :~
map可以很容易扩展到多维,而我们只需要将其值设置为map(这些Map的值可以是其它容器,甚至是map),因此我们能很容易将容器组合起来从而快速生成强大的数据结构,下面是一个示例
// : holding/MapOfList.java package object; import typeinfo.pets.*; import java.util.*; importstatic net.mindview.util.Print.*; publicclass MapOfList { publicstatic Map<Person, List<? extends Pet>> petPeople = new HashMap<Person, List<? extends Pet>>(); static { petPeople.put(new Person("Dawn"), Arrays.asList(new Cymric("Molly"),new Mutt("Spot"))); petPeople.put(new Person("Kate"), Arrays.asList(new Cat("Shackleton"), new Cat("Elsie May"), new Dog("Margrett"))); petPeople.put(new Person("Marilyn"), Arrays.asList( new Pug("Louie aka Louis Snorkelstein Dupree"), new Cat("Stanford aka Stinky el Negro"), new Cat("Pinkola"))); petPeople.put(new Person("Luke"), Arrays.asList(new Rat("Fuzzy"), new Rat("Fizzy"))); petPeople.put(new Person("Isaac"), Arrays.asList(new Rat("Freckly"))); } publicstaticvoid main(String[] args) { print("People: " + petPeople.keySet()); //返回键的Set print("Pets: " + petPeople.values()); //返回值的Collection for(Person person : petPeople.keySet()) { print(person + " has:"); for(Pet pet : petPeople.get(person)) print(" " + pet); } } } /* Output: People: [Person Luke, Person Marilyn, Person Isaac, Person Dawn, Person Kate] Pets: [[Rat Fuzzy, Rat Fizzy], [Pug Louie aka Louis Snorkelstein Dupree, Cat Stanford aka Stinky el Negro, Cat Pinkola], [Rat Freckly], [Cymric Molly, Mutt Spot], [Cat Shackleton, Cat Elsie May, Dog Margrett]] Person Luke has: Rat Fuzzy Rat Fizzy Person Marilyn has: Pug Louie aka Louis Snorkelstein Dupree Cat Stanford aka Stinky el Negro Cat Pinkola Person Isaac has: Rat Freckly Person Dawn has: Cymric Molly Mutt Spot Person Kate has: Cat Shackleton Cat Elsie May Dog Margrett *///:~
原文:https://www.cnblogs.com/jiangfeilong/p/10269239.html
内容总结
以上是互联网集市为您收集整理的java Map全部内容,希望文章能够帮你解决java Map所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。