Skip to content

Create java-Tree-Map examples #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions java-Tree-Map examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import java.util.Map;
import java.util.TreeMap;

public class AccessEntriesFromTreeMapExample {
public static void main(String[] args) {
TreeMap<Integer, String> employees = new TreeMap<>();

employees.put(1003, "Rajeev");
employees.put(1001, "James");
employees.put(1002, "Sachin");
employees.put(1004, "Chris");

System.out.println("Employees map : " + employees);

// Finding the size of a TreeMap
System.out.println("Total number of employees : " + employees.size());

// Check if a given key exists in a TreeMap
Integer id = 1004;
if(employees.containsKey(id)) {
// Get the value associated with a given key in a TreeMap
String name = employees.get(id);
System.out.println("Employee with id " + id + " : " + name);
} else {
System.out.println("Employee does not exist with id : " + id);
}

// Find the first and last entry
System.out.println("First entry in employees map : " + employees.firstEntry());
System.out.println("Last entry in employees map : " + employees.lastEntry());

// Find the entry whose key is just less than the given key
Map.Entry<Integer, String> employeeJustBelow = employees.lowerEntry(1002);
System.out.println("Employee just below id 1002 : " + employeeJustBelow);

// Find the entry whose key is just higher than the given key
Map.Entry<Integer, String> employeeJustAbove = employees.higherEntry(1002);
System.out.println("Employee just above id 1002 : " + employeeJustAbove);
}
}

***********************************************************************************************************************************************************************

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

public class CreateTreeMapCaseInsensitiveOrderExample {
public static void main(String[] args) {
// TreeMap with keys sorted by ignoring case
SortedMap<String, String> fileExtensions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

/*
The above statement is the short form of -
SortedMap<String, String> fileExtensions = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
*/

fileExtensions.put("PYTHON", ".py");
fileExtensions.put("c++", ".cpp");
fileExtensions.put("KOTLIN", ".kt");
fileExtensions.put("Golang", ".go");

// The keys will be sorted ignoring the case (Try removing String.CASE_INSENSITIVE_ORDER and see the output)
System.out.println(fileExtensions);
}
}

***********************************************************************************************************************************************************************

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

public class CreateTreeMapCustomComparatorExample {
public static void main(String[] args) {
// Creating a TreeMap with a Custom comparator (Descending order)
SortedMap<String, String> fileExtensions = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
});

/*
The above TreeMap with custom Comparator can be simply written as -
SortedMap<String, String> fileExtensions = new TreeMap<>(Comparator.reverseOrder());
*/

// Adding new key-value pairs to a TreeMap
fileExtensions.put("python", ".py");
fileExtensions.put("c++", ".cpp");
fileExtensions.put("kotlin", ".kt");
fileExtensions.put("golang", ".go");
fileExtensions.put("java", ".java");

// Printing the TreeMap (The keys will be sorted based on the supplied comparator)
System.out.println(fileExtensions);

}
}

***********************************************************************************************************************************************************************

import java.util.Map;
import java.util.TreeMap;

public class RemoveEntriesFromTreeMapExample {
public static void main(String[] args) {
TreeMap<String, String> countryISOCodeMapping = new TreeMap<>();

countryISOCodeMapping.put("India", "IN");
countryISOCodeMapping.put("United States of America", "US");
countryISOCodeMapping.put("China", "CN");
countryISOCodeMapping.put("United Kingdom", "UK");
countryISOCodeMapping.put("Russia", "RU");
countryISOCodeMapping.put("Japan", "JP");

System.out.println("CountryISOCodeMapping : " + countryISOCodeMapping);

// Remove the mapping for a given key
String countryName = "Japan";
String isoCode = countryISOCodeMapping.remove(countryName);
if(isoCode != null) {
System.out.println("Removed (" + countryName + " => " + isoCode + ") from the TreeMap. New TreeMap " + countryISOCodeMapping);
} else {
System.out.println(countryName + " does not exist, or it is mapped to a null value");
}

// Remove the mapping for the given key only if it is mapped to the given value
countryName = "India";
boolean isRemoved = countryISOCodeMapping.remove(countryName, "IA");
System.out.println("Was the mapping removed for " + countryName + "? : " + isRemoved);

// Remove the first entry from the TreeMap
Map.Entry<String, String> firstEntry = countryISOCodeMapping.pollFirstEntry();
System.out.println("Removed firstEntry : " + firstEntry + ", New TreeMap : " + countryISOCodeMapping);

// Remove the last entry from the TreeMap
Map.Entry<String, String> lastEntry = countryISOCodeMapping.pollLastEntry();
System.out.println("Removed lastEntry : " + lastEntry + ", New TreeMap : " + countryISOCodeMapping);
}
}