<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java `SortedMap`示例 > 原文: [https://javatutorial.net/java-sortedmap-example](https://javatutorial.net/java-sortedmap-example) `SortedMap`接口擴展了[映射](https://docs.oracle.com/javase/7/docs/api/java/util/Map.html),并確保所有條目都按升序排列(因此,`SortedMap`)。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) 如果要按降序排列它,則需要重寫`SortedMap`中的`Compare`方法,我們將在稍后進行操作。[`TreeMap`](https://javatutorial.net/java-treemap-example)實現`SortedMap`,并按其自然順序或指定的比較器對鍵進行排序。在[`TreeMap`](https://javatutorial.net/java-treemap-example) 中,不允許使用空鍵和空值。 ![SortedMap java example](https://img.kancloud.cn/bd/f1/bdf1999eddcd543d6a202472ccbbbe52_185x442.jpg) ## 方法摘要 1. `Comparator <? super K> comparator()`:返回用于對當前映射中的鍵進行排序的比較器;如果當前映射使用其鍵的自然順序,則返回`null`。 2. `Set<Map.Entry<K,V>> entrySet()`:返回當前映射中包含的映射的`Set`視圖。 3. `K firstKey()`:返回映射中當前的第一個鍵(最低還是最高,取決于實現映射的方式(升序還是降序)。 4. `SortedMap<K,V> headMap(K toKey)`:返回當前映射中其鍵嚴格小于`toKey`的部分的視圖。 5. `Set<K> keySet()`:返回當前映射中包含的鍵的`Set`視圖 6. `K lastKey()`:返回映射中當前的最后一個(最高或最低)鍵 7. `SortedMap<K,V> subMap(K fromKey, K toKey)`:返回當前映射的部分視圖,其有效范圍從`fromKey`到`toKey` 8. `SortedMap<K,V> tailMap(K fromKey)`:返回當前映射中鍵大于或等于`fromKey`的部分的視圖 9. `Collection <V> values()`:返回當前映射中包含的值的[集合](https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html)視圖 有關這些方法的更多詳細信息,請查閱官方 [Oracle 文檔](https://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html)。 **代碼實現** ```java import java.util.*; public class SortedHashMapExample { public static void main(String args[]) { Map<Double, String> players = new TreeMap<Double, String>(); // health, name players.put(new Double(100.00), "Hill"); players.put(new Double(120.00), "John"); players.put(new Double(150.00), "Sabrina"); players.put(new Double(105.00), "Caitlyn"); players.put(new Double(110.00), "Rachel"); players.put(new Double(130.00), "Michael"); players.put(new Double(140.00), "Mark"); // get a set of the entries Set setOfEntries = players.entrySet(); // get an iterator Iterator iterator = setOfEntries.iterator(); while(iterator.hasNext()) { // create an entry of the map Map.Entry entry = (Map.Entry)iterator.next(); System.out.println("Key: " + entry.getKey()); System.out.println("Value: " + entry.getValue()); } } } ``` **輸出** ```java Key: 100.0 Value: Hill Key: 105.0 Value: Caitlyn Key: 110.0 Value: Rachel Key: 120.0 Value: John Key: 130.0 Value: Michael Key: 140.0 Value: Mark Key: 150.0 Value: Sabrina ``` 如您所見,它將自動按升序對它們進行分組。 它的生命值從 100.00 開始,直到 150.00。 我將健康作為關鍵,并將名稱作為值的原因只是為了向您表明它提升了他們。 但是,如果我們希望按降序排列它們怎么辦? **使用降序實現** ```java import java.util.*; public class SortedHashMapExample { public static void main(String args[]) { Map<Double, String> players = new TreeMap<Double, String>(new Comparator<Double>() { @Override public int compare(Double x, Double y) { return y.compareTo(x); } }); // name, health players.put(new Double(100.00), "Hill"); players.put(new Double(120.00), "John"); players.put(new Double(150.00), "Sabrina"); players.put(new Double(105.00), "Caitlyn"); players.put(new Double(110.00), "Rachel"); players.put(new Double(130.00), "Michael"); players.put(new Double(140.00), "Mark"); // get a set of the entries Set setOfEntries = players.entrySet(); // get an iterator Iterator iterator = setOfEntries.iterator(); while(iterator.hasNext()) { // create an entry of the map Map.Entry entry = (Map.Entry)iterator.next(); System.out.println("Key: " + entry.getKey()); System.out.println("Value: " + entry.getValue()); } } } ``` **輸出** ```java Key: 150.0 Value: Sabrina Key: 140.0 Value: Mark Key: 130.0 Value: Michael Key: 120.0 Value: John Key: 110.0 Value: Rachel Key: 105.0 Value: Caitlyn Key: 100.0 Value: Hill ``` 走你,再簡單不過了吧? 我們所做的只是覆蓋比較方法,而不是`x => y`(升序),我們將其更改為`y => x`(降序)。
                  <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>

                              哎呀哎呀视频在线观看