

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a TreepMap in Java and add elements
Let us create TreeMap in Java. It stores unique elements in ascending order −
TreeMap<Integer,String> m = new TreeMap<Integer,String>();
Now let us add some elements −
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");
The following is an example to create a TreeMap −
Example
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()); } } }
Output
1 PHP 2 jQuery 3 JavaScript 4 Ruby 5 Java 6 AngularJS 7 ExpressJS
- Related Questions & Answers
- Add elements to a Vector in Java
- Create a TreeMap in Java and add key-value pairs
- Add elements to HashSet in Java
- Add elements to HashMap in Java
- Add elements at beginning and end of LinkedList in Java
- Java Program to create a HashMap and add key-value pairs
- Java Program to Add elements to a LinkedList
- Add elements to LinkedHashMap collection in Java
- How to add elements in Java CopyOnWriteArrayList?
- How to add elements in Java ArrayBlockingQueue?
- Add elements at the middle of a Vector in Java
- Add elements at the end of a Vector in Java
- Can we add null elements to a Set in Java?
- Add and Remove Elements in Perl Hashes
- Reduce a multi-dimensional array and add elements in Numpy
Advertisements