在 Java 中创建 TreeMap 并添加元素
我们将在 Java 中创建 TreeMap。它按照升序存储唯一的元素 -
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
现在让我们添加一些元素 -
m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
下面是创建 TreeMap 的示例 -
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
m.put(1,"PHP");
m.put(2,"jQuery");
m.put(3,"JavaScript");
m.put(4,"Ruby");
m.put(5,"Java");
m.put(6,"AngularJS");
m.put(7,"ExpressJS");
for(Map.Entry e:m.entrySet()){
System.out.println(e.getKey()+" "+e.getValue());
}
}
}输出
1 PHP 2 jQuery 3 JavaScript 4 Ruby 5 Java 6 AngularJS 7 ExpressJS
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP