TreeMap 的 tailMap()、headMap()、firstKey() 方法使用

xiaoxiao2021-02-28  25

import java.util.SortedMap; import java.util.TreeMap; /** * Author: heatdeath * Date: 2018/5/13 * Desc: */ public class TreeMapDemo { public static void main(String[] args) { // creating maps TreeMap<Integer, String> treemap = new TreeMap<>(); SortedMap<Integer, String> treemapincl; // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "five"); System.out.println("Getting tail map"); treemapincl = treemap.tailMap(3); System.out.println("Tail map values: " + treemapincl); treemapincl = treemap.headMap(3); System.out.println("Head map values: " + treemapincl); System.out.println("First key is: " + treemap.firstKey()); } } Getting tail map Tail map values: {3=three, 5=five, 6=six} Head map values: {1=one, 2=two} First key is: 1
转载请注明原文地址: https://www.6miu.com/read-2150153.html

最新回复(0)