Skip to main content

Posts

Collection(TreeSet)

TreeSet:- TreeSet is an implemented class of Set interface where TreeSet is a group of individual objects as a single entity where duplicates are not allowed but insertion should be according to some sorting order and elements must be comparable. If we are not writing some additional code for sorting than automatically elements insert according to the default natural sorting (ascending order for numbers), if we want to customize sorting than we have to implements Comparator interface present in java.util package and implements compare method. Default natural sorting is done by the comparable interface which is present in java.lang package, having only one method compareTo. Example1:-TestTreeSet.java import java.util.*; class TestTreeSet { public static void main(String[]args) { TreeSet ts=new TreeSet(); ts.add(50); ts.add(40); ts.add(60); ts.add(30); ts.add(70); ts.add(20); ts.add(80); ts.add(10); System.out.println(ts); Iterator itr=ts.iterator(); ...

java map example

Map Interface A Map stores data in key and value association. Both key and values are objects. The key must be unique but the values can be duplicate. Although Maps are a part of Collection Framework, they cannot actually be called as collections because of some properties that they posses. Interface Description Map Maps unique key to value. Map.Entry Describe an element in key and value pair in a map. Entry is sub interface of Map. NavigableMap Extends SortedMap to handle the retrienal of entries based on closest match searches SortedMap Extends Map so that key is maintained in an ascending order.   Commonly used Methods defined by Map ·          boolean  containsKey (Object  k ): returns true if map contain  k  as key. Otherwise false. ·          Object  get (Object  k ) : returns value...

LinkedHashMap(Collection)

LinkedHashMap:- LinkedHashMap is child class of HashMap class where object store in the form of key value pair and maintain insertion order. Example1:- TestMap.java import java.util.*; class TestMap { public static void main(String[]args) { Map m=new LinkedHashMap(); m.put(101,"Java"); m.put(102,"Cpp"); m.put(103,"C#"); m.put(104,"C"); m.put("Amit",101); m.put("Neha",102); System.out.println(m); } } Output:- {101=Java, 102=Cpp, 103=C#, 104=C, Amit=101, Neha=102} Example2:-TestMap.java import java.util.*; class TestMap { public static void main(String[]args) { Map<Object,Object> m=new LinkedHashMap<Object,Object>(); m.put(101,"Java"); m.put(102,"Cpp"); m.put(103,"C#"); m.put(104,"C"); m.put("Amit",101); m.put("Neha",102); Set s=m.entrySet(); Iterator itr=s.iterator(); System.out.println(...

java treemap example

TreeMap:- TreeMap is a implemented class of map interface where elements store on the basis of key value pair and according to some sorting order of keys . Example1:- TestMap.java import java.util.*; class TestMap { public static void main(String[]args) { Map<Object,Object> m=new TreeMap<Object,Object>(); m.put(101,"Sumit"); m.put(104,"Sunita"); m.put(102,"Suman"); m.put(107,"Sonu"); Set s=m.entrySet(); Iterator itr=s.iterator(); System.out.println("Key   :::::::::Value"); while(itr.hasNext()) { Map.Entry me=(Map.Entry)itr.next(); System.out.println(me.getKey()+"    :::: "+me.getValue()); } } } Output:- Key :::::::::Value 101    :::: Sumit 102    :::: Suman 104    :::: Sunita 107    :::: Sonu Example2:- TestMap.java import java.util.*; class TestMap { public static void main(String[]args) { Map<Object,Object> m=new...