TreeSet Details part 1 [Collection Framework]

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025

Комментарии • 97

  • @amileyjones9082
    @amileyjones9082 6 лет назад +48

    Well explained! Durga is a great teacher . As this video is 4 years old , i would like to mention here that till java 1.6 null was allowed in TreeSet but it was considered as bug and in java 7 null is not allowed now in TreeSet. From the documentation of TreeSet: NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

  • @ajinkyakottawar9502
    @ajinkyakottawar9502 5 лет назад +4

    Don't have words to say thank you for Durga sir...😊
    They teach us from very basic concepts and in very easy words with live examples, this way it help me to remember in easy way...
    Keep going sir...😊
    Lot's of love to you and your family... ❤️
    God bless you Sir... 🙏😇🙏

  • @RAHULKUMAR-ye7cj
    @RAHULKUMAR-ye7cj 8 лет назад +2

    Durga sir is a very good teacher. In my whole life, i never meet with such type of sir who taught very well. Lots of confusion in java, but now my all dout is cleared. Thanku so much sir. Is there any video for android except sandeep sir. Please help me.

  • @atiqulny
    @atiqulny 9 лет назад

    Very lively, engaging instructor, best in RUclips.

  • @ravikumarsunkhesvarapu9122
    @ravikumarsunkhesvarapu9122 6 лет назад +3

    the way u teach is awesome

  • @jajatisahoo3831
    @jajatisahoo3831 6 месяцев назад +1

    For an empty tree-set, when trying to insert null as the first value, one will get NPE from JDK 7. From JDK 7 onwards, null is not at all accepted by TreeSet. However, up to JDK 6, null was accepted as the first value, but any insertion of more null values in the TreeSet resulted in NullPointerException. Hence, it was considered a bug and thus removed in JDK 7.

  • @rahulkad5392
    @rahulkad5392 2 года назад +1

    Good job sir 🥰

  • @saradasai7669
    @saradasai7669 9 лет назад

    Thank you Sir. Your explanation is very good ,clear and easily understandable.

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  9 лет назад

      Sarada Sai
      View 42 videos on Collections in Java,
      ruclips.net/video/rI3VkItC0eA/видео.html
      View 5 videos on JSF,
      ruclips.net/video/rAa1wwWLw1Y/видео.html
      View 50 videos on Oracle,
      ruclips.net/video/pX5WJctn3Kw/видео.html
      View 35 videos on Hibernate,
      ruclips.net/video/SZ8abQ3vmdc/видео.html
      View 115 videos on Data Structures,
      ruclips.net/video/oAAiBQFSEik/видео.html
      View 58 videos on Struts,
      ruclips.net/video/nYqsnrHGsQg/видео.html
      View 74 videos on Spring,
      ruclips.net/video/YpsFT50mths/видео.html
      View 41 videos on CRT,
      ruclips.net/video/fDRnKPyvR14/видео.html
      View 12 videos on JAVA Reflections API & Annotations
      ruclips.net/video/BxCw0uTxZRs/видео.html
      View 39 videos on Java Real Time Project Implementation on Spring,Struts & Hibernate,
      ruclips.net/video/iKnb0AgKZJ0/видео.html
      View other videos on ADV JAVA
      ruclips.net/video/83jLqTAbqQU/видео.html
      View 9 videos on ENUM,
      ruclips.net/video/T1fMWwI9D1E/видео.html
      View 16 videos on Serialization,
      ruclips.net/video/bIzfrvdt7qM/видео.html

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  9 лет назад

      Durga Software Solutions
      Demo Sessions on youtube,
      Language Fundamentals(16 Videos)ruclips.net/video/eTXd89t8ngI/видео.html,
      Operators & Assignments(7 Videos)ruclips.net/video/1JGOPhIyhAM/видео.html,
      Flow Control(6 Videos)ruclips.net/video/X6rS9vD9-qM/видео.html.

  • @santoshaakubathini5937
    @santoshaakubathini5937 6 лет назад +1

    Nicely explained.. Awesome..

  • @jeetendrabarnwal
    @jeetendrabarnwal 5 лет назад +6

    Thanks for these awesome videos... just wanted to add one point - null was allowed in Java 6 ..in Java 7 null is not allowed..

  • @Modern_revolution
    @Modern_revolution 5 лет назад +1

    Thank you so much for your help!

  • @nisargmunjpara5431
    @nisargmunjpara5431 9 лет назад +5

    Hello sir these are incredible stuff we are having. Thank you for it.
    I have question regarding constructors of "TreeSet(Collection c)." In treeset heterogenous objects are not allowed and what if we create/convert ArrayList collection "c"(or any collection object which consists heterogenous objects) into a TreeSet object with " new TreeSet(Collection c) " constructor???? Hows it possible?
    Thank you..!!!!

    • @jasonarora8911
      @jasonarora8911 9 лет назад +2

      +Nisarg Munjpara There is no way if there are heterogenous elements.You will get exception.You have to remove heterogenous elements and then you can take advantage of TreeSet(Having duplicate removed + Sorting elements) but no order preserved

    • @Skripturz
      @Skripturz 6 лет назад

      Thanks, I was wondering the same thing

  • @varunsen2802
    @varunsen2802 7 месяцев назад +1

    I think in Priority Queue also, heterogenous objects are not allowed, beacuse in PQ elements are inserted in a some sorted order.

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  7 месяцев назад

      Hello Varun.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @surendramishra4813
    @surendramishra4813 6 лет назад +4

    One thing I am not able to understand please correct me if I am wrong, when 4. TreeSet t = new TreeSet(Collection c); is there then why we need 3. TreeSet t = new TreeSet(SortedSet s); even though SortedSet also a Collection

    • @sangeethagopi4362
      @sangeethagopi4362 2 года назад

      I too have the same doubt... someone please clear our doubt

  • @algorithmsguide5076
    @algorithmsguide5076 5 лет назад +1

    Thankyou sir ji !!

  • @kamalapuramsivasankar7
    @kamalapuramsivasankar7 Год назад +1

    Thanks sir...

  • @rexsam3134
    @rexsam3134 4 года назад +2

    Nice one. You missed mentioning which Java version was used to run the code

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  4 года назад

      Hello Seetesh.
      Thanks for your valuable feedback. It will boost us to do new things. Please subscribe to get updates about new videos.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @senthil7818
    @senthil7818 10 лет назад +1

    Durga sir plz upload videos on map collections

  • @techgyan6233
    @techgyan6233 6 месяцев назад +1

    Might be some changes in new java version that's why Null not support
    import java.util.TreeSet;
    public class InterfacePractice {
    public static void main(String[] args) {
    TreeSet treeSet = new TreeSet();
    treeSet.add(null);
    System.out.println(treeSet);
    }
    }
    Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  6 месяцев назад

      Hello Friend.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @Rohith-ov7vr
    @Rohith-ov7vr Год назад +1

    in priorityque also the hetogeneous data not allowed

  • @javadsa367
    @javadsa367 11 месяцев назад +1

    Before Java 7, it was possible to add null elements to an empty TreeSet.

  • @rohitlovesnature
    @rohitlovesnature 7 лет назад +1

    sir, can you please provide videos on hashmap and map interface

  • @horriblecoder7280
    @horriblecoder7280 6 лет назад

    If you look at the internal implementation of the TreeSet, it uses natural ordering, that means TreeSet uses Comparable interface by default to sort its value by comparing other value.
    Just think programmatically, how will you compare the null value to other. Is it make sense to compare the value with other(NULL) which doesn't have any value in it. That's why java developer made it TreeSet such a way that, it will throw NullPointerExeption when you try to store null value.

    • @horriblecoder7280
      @horriblecoder7280 6 лет назад +1

      null as first element was considered as bug in java 1.6

    • @jvsnyc
      @jvsnyc 4 года назад

      I believe that nulls are the second worst problem in all of computer science and programming. Inconsistent handling of them, i.e. are two nulls equal or not, etc. is the first.

  • @dhrub6
    @dhrub6 8 лет назад +3

    Hi Sir,
    I am your student. I got a job after clearing my doubts in Collections and Threads.
    Thanks a lot.
    I have a Question.
    How will we break TreeSet property (it should store Objects in non sorted order)?

  • @IamJ____0001
    @IamJ____0001 Год назад +1

    sir i tried to add thiss......t.add(null) for empty set obj still getting exception..
    Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "k1" is null

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  Год назад

      Hello Jishan.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @addicted2headway
    @addicted2headway Год назад +1

    sir, i am trying to insert null value in empty treeset then also i am getting nullpointer exception

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  Год назад

      Hello Friend.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @sohrabalam9650
    @sohrabalam9650 3 года назад +2

    But in my JDK-8, I am adding null in empty TreeSet and getting NullPointerException.
    E.g.
    import java.util.TreeSet;
    public class TreeSetDemo {
    public static void main(String[] args) {
    TreeSet treeSet = new TreeSet();
    treeSet.add(null);
    }
    }
    Result : Exception in thread "main" java.lang.NullPointerException

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  3 года назад

      Hello Sohrab.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @surajmahtosbg
    @surajmahtosbg 6 лет назад +1

    watch full video

  • @Tarun1kumar1989
    @Tarun1kumar1989 9 лет назад +2

    Hi Sir,
    I have added only null value then also i am getting null pointer exception.
    import java.util.*;
    class TreeSetDemo1
    {
    public static void main(String[] args)
    {
    TreeSet t=new TreeSet();
    t.add(null);
    System.out.println(t);
    }
    }

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  9 лет назад

      +Tarun Kumar
      Hello,
      We are very Happy to say Now you can access All DURGA Sir's Core Java with
      OCJP/SCJP Video Sessions. To get Maximum Benefit from these video sessions,
      compulsory you have to write notes also. Sir will dictate each and every point
      clearly.
      Demo Sessions on RUclips:
      1. Language Fundamentals(16 Videos) ruclips.net/video/eTXd89t8ngI/видео.html
      2. Operators &Assignments(7 Videos) ruclips.net/video/1JGOPhIyhAM/видео.html
      3. Flow Control(6 Videos) ruclips.net/video/X6rS9vD9-qM/видео.html
      4. Exception Handling
      (10 Videos) ruclips.net/p/PLd3UqWTnYXOnSkaum39zWYI6bPb3i7liz
      Please give your valuable feedback and it is very important and helpful
      for us.
      Note:-If you are interested for full version of videos please revert back for
      more information.
      Fee
      : 6000/-
      Duration:
      6-months
      DURGASOFT Online Training Team,
      Mobile : +91-7207 21 24 28
      +91-8885 25 26 27
      Mail ID : durgasoftonline@gmail.com , durgasoftonlinetraining@gmail.com

    • @rickyb8989
      @rickyb8989 9 лет назад

      Same with me...getting NullPointerException...guess either Durga is wrong or it was previously allow in earlier jdk version(I am using jdk 1.8)

    • @CorporateKahani
      @CorporateKahani 9 лет назад +2

      +ricky b hi ricky, it was possible until 1.6 version only

    • @aditya12all
      @aditya12all 8 лет назад +1

      It says in the compatibility notes from Java 6 to Java 7 that the possibility of adding null to a TreeMap was a bug: oracle.com/technetwork/java/javase/…. See also: bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147

  • @VarunKumar-yy5sr
    @VarunKumar-yy5sr 6 лет назад

    As per explaination, heterogeneous objects are not allowed but also u said default sorting order for numbers is assessing and characters is alphabetical order. If it is the case y can't we insert heterogeneous elements like alphabets as some elements and numbers as some other elements. Because as per explaination numbers should sort in assending order and characters in alphabetical order.
    Please clarify.

  • @devendrasingraul8905
    @devendrasingraul8905 3 года назад +1

    TreeSet t = new TreeSet();
    // null is not allow for empty TreeSet
    t.add(null);
    for Empty TreeSet null is not allow . I am using java 1.8

  • @pal0106
    @pal0106 10 лет назад

    what happens if we try to create a tree set with the third constructor using a collection of heterogeneous elements?

  • @MayurKalambkar
    @MayurKalambkar Год назад +1

    in java 1.8 null value is not allowed in TreeSet. So you are showing program of which version.

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  Год назад

      Hello Friend.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @manassahoo2750
    @manassahoo2750 2 года назад +1

    Sit please imporve your graphics and light .

  • @pranavdeshpande9675
    @pranavdeshpande9675 9 лет назад

    sir pls upload videos for Queue nad Map too...

  • @bluesteel1
    @bluesteel1 2 года назад +1

    3:21 null insertions are not allowed this is incorrect

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  2 года назад

      Hello Friend.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @lipikabarman1012
    @lipikabarman1012 Год назад +1

    after 1.6 version it not working sir. We are not able to insert null value in TreeSet

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  Год назад

      Hello Friend.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @ashishshrivastava4532
    @ashishshrivastava4532 6 лет назад +1

    Null is not allowed in TreeSet But allowed in Hash Set So I request you to correct this one.

  • @parameshyannam8542
    @parameshyannam8542 7 лет назад +1

    Sir, why TreeSet and TreeMap Are not allowed Heterogenious Objects ??

    • @anirbanmajumdar781
      @anirbanmajumdar781 7 лет назад

      Because the underline data structure is Balanced Tree and it implements Set, so you cannot actually have sorting done on Hetrogenous objects.

  • @ajinzrathod
    @ajinzrathod 3 года назад +1

    If null value is not working even for first time.
    It was allowed till 1.6. Since 1.7, null value is not allowed even for first time.

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  3 года назад

      Hello Ajinkya.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @nitishkadam9323
    @nitishkadam9323 8 лет назад

    getting null pointer exception while adding null in empty treeset ??
    please explain.. thnks

    • @NikhilMaguluri
      @NikhilMaguluri 7 лет назад +1

      from java 7 null insertion is not allowed in a treeset. In java 6 it is a bug, that allows null insertion into a empty treeset.

    • @RoyaLockz
      @RoyaLockz 7 лет назад

      Allowing Null within a collection of unique values is generally considered bad practice and disallowed in a lot of implementations with a similar mentality. Could you describe a situation where you'd need to insert Null into a Set that it'd be beneficial and improve the overall code quality?

  • @senthil7818
    @senthil7818 10 лет назад

    This collections videos is enough for collection interview question.?Expecting a reply.Thanks in advance

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  9 лет назад

      SENTHIL KUMAR
      View 9 videos on ENUM,
      ruclips.net/video/T1fMWwI9D1E/видео.html
      View 16 videos on Serialization,
      ruclips.net/video/bIzfrvdt7qM/видео.html

  • @srikanthr5935
    @srikanthr5935 10 лет назад

    Hello sir, i am trying to insert null in an empty treeset but still i am getting null pointer exception does treeset really allows null acceptance for once

    • @ishansoni9819
      @ishansoni9819 10 лет назад

      post ur code?

    • @srikanthr5935
      @srikanthr5935 10 лет назад

      Ishan Soni
      package name;
      import java.util.Comparator;
      import java.util.HashSet;
      import java.util.TreeSet;
      public class treeset {
      public static void main(String []args) {

      TreeSet t1=new TreeSet();
      t1.add(null);
      System.out.println(t1);
      }
      }

  • @iam_hindhu
    @iam_hindhu 10 лет назад

    sir , i trying this program ( am using java 1.7)
    i have an doubt on Null Acceptance ....
    TreeSet treeSet=new TreeSet();
    treeSet.add(null);
    System.out.println("
    tree Set is : "+treeSet);
    if i want execute this program i got an Runtime Exception like :
    Exception in thread "main" java.lang.NullPointerException
    at java.util.TreeMap.compare(Unknown Source)
    at java.util.TreeMap.put(Unknown Source)
    at java.util.TreeSet.add(Unknown Source)
    at com.practice.collections.TreeSetExample.main(TreeSetExample.java:10)
    but in your sample example you didn't get any exception ...
    i got confused which one is correct
    Sir, You said TreeSet accepted null (for only one time)
    but in program i got error like what am mention above ....
    please clarify my doubt ...

  • @vishwanathtontanal2365
    @vishwanathtontanal2365 9 лет назад +2

    Null value is not allowed in TreeSet. Not even single time.

    • @ajinzrathod
      @ajinzrathod 3 года назад

      Allowed till 1.6. Since 1.7, null value is not allowed even for first time.

  • @joerokcz
    @joerokcz 10 лет назад

    if i tried to put null after an null insertion,what will happen?

  • @RAHULKUMAR-ye7cj
    @RAHULKUMAR-ye7cj 8 лет назад

    How I know about unicode of a is 97 and unicode value of A is 65. I am confused here.

  • @vineetchaurasia7460
    @vineetchaurasia7460 6 лет назад +2

    Null is not allowed

  • @WonderfulSound8
    @WonderfulSound8 5 лет назад +4

    I'm adding null as my first element, but I'm getting NPE)))
    Here's my code))
    TreeSet treeSet = new TreeSet();
    treeSet.add(null);
    System.out.println(treeSet);

    • @jvsnyc
      @jvsnyc 4 года назад +1

      Read Amiley Jones comment!

  • @tx30289
    @tx30289 9 лет назад +2

    nice English :)

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  9 лет назад +1

      *****
      Hi,
      We are very Happy to say Our Expert Faculty Durga Sir's One-to-One Video Sessions on "Core Java with SCJP/OCJP"
      are available. Shooted from direct Class Room. YOu can access these videos from anywhere anytime. Around 200+ hours
      training, 178 Videos with clear explanation on every concept. 100% Satisfaction and Perfection in Subject guaranteed.
      For Demo Sessions Please call or drop a mail to us...
      DURGASOFT Online Training
      ph: 91-8885252627
      mail :durgasoftonline@gmail.com, durgasoftonlinetraining@gmail.com
      Demo Sessions on youtube:
      1. Language Fundamentals(16 Videos)ruclips.net/video/eTXd89t8ngI/видео.html
      2. Operators & Assignments(7 Videos)ruclips.net/video/1JGOPhIyhAM/видео.html
      3. Flow Control(6 Videos)ruclips.net/video/X6rS9vD9-qM/видео.html
      durgasoft.com
      durgasoftonlinetraining.com
      facebook.com/durgasoftware
      durgajobs.com
      facebook.com/durgajobsinfo

    • @horriblecoder7280
      @horriblecoder7280 6 лет назад

      his comment was sarcastic.

  • @Mbc43m276
    @Mbc43m276 8 лет назад +1

    Why are there two separate constructors for creating an equivalent collection c and a sortedSet s?
    I.e TreeSet t =new TreeSet(Collection c) and TreeSet t =new TreeSet(SortedSet s). ? sorted set comes under the collection interface right?
    Someone please help

    • @horriblecoder7280
      @horriblecoder7280 6 лет назад

      In first type of constructor the element order will be naturally sorted.
      In second type the element order will depend on the customized sorting order that already may have been used.
      suppose:
      contents of List x: 5,3,9,7
      TreeSet t=new TreeSet(x);
      contents of TreeSet t:3,5,7,9
      suppose:
      contents of TreeSet x:5,3,9,7 (custmizing in order is done under compare method)
      TreeSet t=new TreeSet(x);
      contents of TreeSet t:5,3,9,7
      Hence order also moves.
      For more info read documentation
      docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html

  • @Nitinkandwal
    @Nitinkandwal 8 лет назад +9

    null is not allowed in TreeSet.

    • @pratikbhardwaj3804
      @pratikbhardwaj3804 6 лет назад +2

      from 1.7 version

    • @surendramishra4813
      @surendramishra4813 6 лет назад

      Nitin Kandwal
      first see full video

    • @farhadbaghirov9075
      @farhadbaghirov9075 6 лет назад +2

      +
      Kabir Kandwal null is allowed
      Let's look at the example shown below:
      class data1 {
      Integer number;
      public data1(Integer number) {
      this.number = number;
      }
      @Override
      public String toString() {
      return ""+number;
      }
      }
      TreeSet data2set = new TreeSet(new Comparator() {
      @Override
      public int compare(data1 d1,data1 d2) {
      int d1n=d1==null ? 0 : d1.number;
      int d2n=d2==null ? 0 : d2.number;
      return d1n-d2n;
      }
      });
      data2set.add(null);
      data2set.addAll(Arrays.asList(null,null));
      System.out.println(data2set);//[null]

    • @Vishwasp13
      @Vishwasp13 6 лет назад +1

      @@farhadbaghirov9075 Customising the comparator and removing null doesn't imply null is allowed. The trick is good but might confuse others.

  • @HemantShori
    @HemantShori 6 лет назад

    you had me at medile

  • @vipulgujarathi_7379
    @vipulgujarathi_7379 2 года назад +1

    what is the default size of TreeSet Constructor?

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  2 года назад

      Hello Vipul.
      Plz contact our online team durgasoftonlinetraining@gmail.com or call us on this number +918885252627, 7207212427/28

  • @shilpa3645
    @shilpa3645 10 лет назад +1

    treeset not allowed null

    • @DurgaSoftwareSolutions
      @DurgaSoftwareSolutions  10 лет назад +4

      Yah Shilpa until 1.6 version we can add null to the TreeSet. But from 1.7 version onwards we cannot add null to the TreeSet.This is a small difference wrt versions.

  • @JorgeChavez8
    @JorgeChavez8 8 лет назад

    Number 6 is wrong, TreeSet doesn't allow you to insert null objects.