Last active
November 29, 2019 18:30
-
-
Save saleebm/0e8b69c322eed1bbad324b355d5929a7 to your computer and use it in GitHub Desktop.
Tree maps with dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.text.*; | |
public class TreeMapDemo { | |
public static void main(String[] args) { | |
// creating maps | |
TreeMap<Date, String> treemap = new TreeMap<Date, String>(); | |
SortedMap<Date, String> treemaphead = new TreeMap<Date, String>(); | |
SortedMap<Date, String> treemaptail = new TreeMap<Date, String>(); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); | |
// populating tree map | |
try { | |
treemap.put(sdf.parse("2000/12/14"), "one"); | |
treemap.put(sdf.parse("2004/12/14"), "two"); | |
treemap.put(sdf.parse("2009/12/14"), "three"); | |
treemap.put(sdf.parse("2012/12/14"), "four"); | |
treemap.put(sdf.parse("2023/12/14"), "five"); | |
treemap.put(sdf.parse("2033/12/14"), "six"); | |
} catch(ParseException e) { | |
} | |
Date now = new Date(); | |
System.out.println("now: " + now.toString() + "\r\n"); | |
// getting head map | |
treemaphead = treemap.headMap(now); | |
System.out.println("head: \r\n"); | |
for(Object object : treemaphead.keySet()){ | |
System.out.print(treemaphead.get(object) + "\r\n"); | |
System.out.println(object.toString()); | |
} | |
// getting tail map | |
treemaptail = treemap.tailMap(now); | |
System.out.println("\rtail: \r\n"); | |
for(Object object : treemaptail.keySet()){ | |
System.out.print(treemaptail.get(object) + "\r\n"); | |
System.out.println(object.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment