标签:package 单词 blog ola for obj main object int
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.*; public class Statistics { public static void 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.*; import static net.mindview.util.Print.*; public class PetMap { public static void 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.*; import static net.mindview.util.Print.*; public class MapOfList { public static 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"))); } public static void 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 *///:~
标签:package 单词 blog ola for obj main object int
原文地址:https://www.cnblogs.com/jiangfeilong/p/10269239.html