<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## [映射Map](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=%e6%98%a0%e5%b0%84map) 將對象映射到其他對象的能力是解決編程問題的有效方法。例如,考慮一個程序,它被用來檢查 Java 的**Random**類的隨機性。理想情況下,**Random**會產生完美的數字分布,但為了測試這一點,則需要生成大量的隨機數,并計算落在各種范圍內的數字個數。**Map**可以很容易地解決這個問題。在本例中,鍵是**Random**生成的數字,而值是該數字出現的次數: ~~~ // collections/Statistics.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Simple demonstration of HashMap import java.util.*; public class Statistics { public static void main(String[] args) { Random rand = new Random(47); Map<Integer, Integer> m = new HashMap<>(); for(int i = 0; i < 10000; i++) { // Produce a number between 0 and 20: int r = rand.nextInt(20); Integer freq = m.get(r); // [1] m.put(r, freq == null ? 1 : freq + 1); } System.out.println(m); } } /* Output: {0=481, 1=502, 2=489, 3=508, 4=481, 5=503, 6=519, 7=471, 8=468, 9=549, 10=513, 11=531, 12=521, 13=506, 14=477, 15=497, 16=533, 17=509, 18=478, 19=464} */ ~~~ * **\[1\]**自動包裝機制將隨機生成的**int**轉換為可以與**HashMap**一起使用的**Integer**引用(不能使用基本類型的集合)。如果鍵不在集合中,則`get()`返回**null**(這意味著該數字第一次出現)。否則,`get()`會為鍵生成與之關聯的**Integer**值,然后該值被遞增(自動包裝機制再次簡化了表達式,但實際上確實發生了對**Integer**的裝箱和拆箱)。 接下來的示例將使用一個**String**描述來查找**Pet**對象。它還展示了通過使用`containsKey()`和`containsValue()`方法去測試一個**Map**,以查看它是否包含某個鍵或某個值: ~~~ // collections/PetMap.java import typeinfo.pets.*; import java.util.*; public class PetMap { public static void main(String[] args) { Map<String, Pet> petMap = new HashMap<>(); petMap.put("My Cat", new Cat("Molly")); petMap.put("My Dog", new Dog("Ginger")); petMap.put("My Hamster", new Hamster("Bosco")); System.out.println(petMap); Pet dog = petMap.get("My Dog"); System.out.println(dog); System.out.println(petMap.containsKey("My Dog")); System.out.println(petMap.containsValue(dog)); } } /* Output: {My Dog=Dog Ginger, My Cat=Cat Molly, My Hamster=Hamster Bosco} Dog Ginger true true */ ~~~ **Map**與數組和其他的**Collection**一樣,可以輕松地擴展到多個維度,只需要創建一個值為**Map**的**Map**(這些**Map**的值可以是其他集合,甚至是其他**Map**)。因此,能夠很容易地將集合組合起來以快速生成強大的數據結構。例如,假設你正在追蹤有多個寵物的人,只需要一個**Map>**即可: ~~~ // collections/MapOfList.java // {java collections.MapOfList} package collections; import typeinfo.pets.*; import java.util.*; public class MapOfList { public static final Map<Person, List< ? extends Pet>> petPeople = new HashMap<>(); 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"), 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) { System.out.println("People: " + petPeople.keySet()); System.out.println("Pets: " + petPeople.values()); for(Person person : petPeople.keySet()) { System.out.println(person + " has:"); for(Pet pet : petPeople.get(person)) System.out.println(" " + pet); } } } /* Output: People: [Person Dawn, Person Kate, Person Isaac, Person Marilyn, Person Luke] Pets: [[Cymric Molly, Mutt Spot], [Cat Shackleton, Cat Elsie May, Dog Margrett], [Rat Freckly], [Pug Louie aka Louis Snorkelstein Dupree, Cat Stanford, Cat Pinkola], [Rat Fuzzy, Rat Fizzy]] Person Dawn has: Cymric Molly Mutt Spot Person Kate has: Cat Shackleton Cat Elsie May Dog Margrett Person Isaac has: Rat Freckly Person Marilyn has: Pug Louie aka Louis Snorkelstein Dupree Cat Stanford Cat Pinkola Person Luke has: Rat Fuzzy Rat Fizzy */ ~~~ **Map**可以返回由其鍵組成的**Set**,由其值組成的**Collection**,或者其鍵值對的**Set**。`keySet()`方法生成由在**petPeople**中的所有鍵組成的**Set**,它在*for-in*語句中被用來遍歷該**Map**。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看