Menu

Post image 1
Post image 2
1 / 2
0

Set in Java

DEV Community·Vidya·about 1 month ago
#gaHy3Okh
Reading 0:00
15s threshold

What is a Set in Java Collections? A Set is a collection in Java that: --> Does not allow duplicate elements --> Does not guarantee index (no ordering like List) --> Stores only unique values It is part of java.util.Set interface. Main Types of Set 1. HashSet --> No order maintained --> Fast performance --> Allows one null Example import java.util.* ; public class HashSetExample { public static void main ( String [] args ) { Set < String > set = new HashSet <>(); set . add ( "Apple" ); set . add ( "Banana" ); set . add ( "Apple" ); // duplicate ignored set . add ( "Mango" ); System . out . println ( set ); } } output: [ Apple , Banana , Mango ] Enter fullscreen mode Exit fullscreen mode 2. LinkedHashSet --> Maintains insertion order --> No duplicates allowed Example import java.util.* ; public class LinkedHashSetExample { public static void main ( String [] args ) { Set < Integer > set = new LinkedHashSet <>(); set . add ( 10 ); set . add ( 30 ); set .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More