If i don't use generics then why do i need to convert it into generics ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

If i don't use generics then why do i need to convert it into generics ?

Set set=map.entrySet();

16th May 2017, 6:15 PM
Somnath Ghosh
Somnath Ghosh - avatar
10 Answers
+ 11
Sorry, I don't understand your question. What do you want to do?
16th May 2017, 6:36 PM
Tashi N
Tashi N - avatar
+ 3
Set is a generic interface so if you use Set you use generic.
16th May 2017, 7:20 PM
Tamás Barta
Tamás Barta - avatar
+ 2
Yes, a generic interface so every implementation is a generic class. Sorry for the mistake, I fixed it ;)
16th May 2017, 7:37 PM
Tamás Barta
Tamás Barta - avatar
+ 2
You can: https://code.sololearn.com/cpY85ql77YLX/?ref=app But as Set is Iterable, it is better to use in enhanced for than use Iterator
16th May 2017, 8:51 PM
Tamás Barta
Tamás Barta - avatar
+ 1
/Non-generic import java.util.*; public class MapExample1 { public static void main(String[] args) { Map map=new HashMap(); //Adding elements to map map.put(1,"Amit"); map.put(5,"Rahul"); map.put(2,"Jai"); map.put(6,"Amit"); //Traversing Map Set set=map.entrySet();//Converting to Set so that we can traverse Iterator itr=set.iterator(); while(itr.hasNext()){ //Converting to Map.Entry so that we can get key and value separately Map.Entry entry=(Map.Entry)itr.next(); System.out.println(entry.getKey()+" "+entry.getValue()); } } }
16th May 2017, 7:18 PM
Somnath Ghosh
Somnath Ghosh - avatar
+ 1
set is a interface
16th May 2017, 7:35 PM
Somnath Ghosh
Somnath Ghosh - avatar
+ 1
public interface Set<E> extends Collection<E>
16th May 2017, 8:33 PM
Tamás Barta
Tamás Barta - avatar
0
if we use generics then we don't need this line
16th May 2017, 7:17 PM
Somnath Ghosh
Somnath Ghosh - avatar
0
what is generic interface ?
16th May 2017, 8:19 PM
Somnath Ghosh
Somnath Ghosh - avatar
0
why we can't write ..Iterator i=map.entryset().iterator()
16th May 2017, 8:43 PM
Somnath Ghosh
Somnath Ghosh - avatar