14.9 Set Interface in Java Collection Framework
Вставка
- Опубліковано 7 вер 2024
- Check out our website: www.telusko.com
Follow Telusko on Twitter: / navinreddy20
Follow on Facebook:
Telusko : / teluskolearnings
Navin Reddy : / navintelusko
Follow Navin Reddy on Instagram: / navinreddy20
Subscribe to our other channel:
Navin Reddy : / @navinreddy
Telusko Hindi :
/ @teluskohindi
Not a bad intro. Shorter than other videos that cover more details.
3:40 non-duplicate
5:35 HashSet vs TreeSet
dude are you getting the 3090 Nvidia card?!?!
Clean and simple explanation.
I think also LinkedHashSet orders elements. great video very helpful.
Its only maitain the order of insertion..not the order
Thank you for this video. Helped me a lot!!
Thanks'
Thank you so much!
Very helpful
Nice intro
Hi Navin, What I have seen in practice is that BOTH HashSet and TreeSet do not show the elements in order. It is also shown in your video. Please correct your future videos on that.
what should we use if we want to return type in set,or arryList or in general collection
thx
Great!
And how we will get the elements in descending order
this was not the sequence, If we will use Linkedhashset at the place of treehashset then we can get the same sequence which we have entered and if we want the records in ascending order then we should use treeset. correct me of I am wrong. Thanks 🙏
intro song?
Can we add a value on index basis using hashset
Only Collections implementing the List interface allow index based adding of elements via the add(int index, E element) method.
To your question, since HashSet is an implementation of Set, it can only add on additional elements to the end of its set.
Another point to mention is the internal data storage structure behind HashSet is a Map, guaranteeing uniqueness of its elements via non repeating Keys.
Can we use basic for loop here or it always has to be enhanced one?
Please reply sir
Yes
How?
@@puttarajkoliwad3289 you can not, if you want to use regular for loop you need to create Iterator.
Iterator iterator = set.iterator();
for (int i = 0; i < set.size() ; i++) {
System.out.println(iterator.next());
}
OR
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
@@buzzikea else
values.forEach(System.out::println);
How iterator internally works for Set and list ? difference between both internal working.?
someone is asked this question into interview room can you please ans me for the same.
Anish kumar k.a. Have you found the answer?
@@haykmkrtchyan7093 not yet
@@anishkumark.a.1988 I think for both cases it works the same. Why? Let me explain. As we know Iterator purpose is to iterate over elements. And it has 3 methods as I rememeber: next(), hasNext(), remove().
Ok now the question "how it works internally"? If I'm right, it starts to iterate from the first element and every time we check in a while loop "if it has next element, than iterate". But the question "How iterator internally works for Set and list", I think for both cases iterator works same. + for List we can also use ListIterator, which is for List Interface and its childs. It provides us some new methods and as I remember we can iterate in reverse order too. But the ListIterator you can't use in anywhere else.
So my answer is: For both cases list and set, iterator works the same.
@@haykmkrtchyan7093 yaa but if you check the internal implementation of set and list for iterator both implementation is bit different.
what is that for loop called?
Enhanced For loop
or ForEach loop
package Ass5;
import java.util.HashSet;
import java.util.Set;
public class Setexample
{
public static void main(String [] args)
{
Set e1=new HashSet();
Event e2=new Event();
e2.setEventid(1);
e2.setEventname("tornedo");
e1.add(e2);
Event e3= new Event();
e3.setEventid(1);
e3.setEventname("tornedo");
e1.add(e3);
for(Event e4 : e1)
{
System.out.println(e4.getEventid());
System.out.println(e4.getEventname());
}
}
}
This is my setexample code
while running this code i am not getting proper output as set has its own functionality that it should contain unique value but while running this code i am getting duplicate value as well so what is the solution for this .
1
tornedo
1
tornedo
this is my sample output for this code.