✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • TreeMap
  • Initialize
  • Iteration
  • TreeSet
  • Initialization
  • Iteration
  • TreeMap/TreeSet of a custom Java type

Was this helpful?

  1. Java
  2. Data Structures

TreeSet and TreeMap

Plant a tree everywhere

TreeMap

Initialize

TreeMap<Integer, String> treeMap = new TreeMap<>();

treeMap.put(10, "ten");
treeMap.put(4, "four");
treeMap.put(1, "one");
treeSet.put(12, "twelve");
System.out.println(treeMap.firstEntry()); 
// Prints 1=one
System.out.println(treeMap.lastEntry()); 
// Prints 12=twelve
System.out.println(treeMap.size()); 
// Prints 4, since there are 4 elemens in the map
System.out.println(treeMap.get(12)); 
// Prints twelve
System.out.println(treeMap.get(15)); 
// Prints null, since the key is not found in the map

Iteration

for (Entry<Integer, String> entry : treeMap.entrySet()) {
    System.out.print(entry + " "); //prints 1=one 4=four 10=ten 12=twelve
}

Iterator<Entry<Integer, String>> iter = treeMap.entrySet().iterator();
while (iter.hasNext()) {
    System.out.print(iter.next() + " "); //prints 1=one 4=four 10=ten 12=twelve
}

TreeSet

Initialization

TreeSet treeSet = new TreeSet<>();

treeSet.add(10);
treeSet.add(4);
treeSet.add(1);
System.out.println(treeSet.first()); 
// Prints 1
System.out.println(treeSet.last()); 
// Prints 12
System.out.println(treeSet.size()); 
// Prints 4, since there are 4 elemens in the set
System.out.println(treeSet.contains(12)); 
// Prints true
System.out.println(treeSet.contains(15)); 
// Prints false

Iteration

for (Integer i : treeSet) {
    System.out.print(i + " "); //prints 1 4 10 12
}

Iterator<Integer> iter = treeSet.iterator();
while (iter.hasNext()) {
    System.out.print(iter.next() + " "); //prints 1 4 10 12
}

TreeMap/TreeSet of a custom Java type

Since TreeMap and TreeSet s maintain keys/elements according to their natural ordering. Therefor TreeMap keys and TreeSet elements have to comparable to one another. you will run into the following error

Exception in thread "main" java.lang.ClassCastException: Person cannot be cast to
java.lang.Comparable
  • One solution is to modify Object so it would implement the Comparable interface.

  • Another solution is to provide the TreeSet with a Comparator

PreviousSet in JavaNextObject Oriented Programming

Last updated 3 years ago

Was this helpful?