
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement a Set interface in JShell in Java 9?
JShell is a command-line tool in Java 9 that has been used to execute simple statements like expressions, classes, interfaces, methods, and etc.
A Set is an interface in Java that specifies a contract for collections having unique elements. If object1.equals(object2) returns true, then only one of object1 and object2 have a place in Set implementation.
In the below code snippet, we have to use the Set.of() method. The collection returned by the Set.of() method is immutable, so it doesn't support the add() method. If we trying to add an element, throws UnsupportedOperationException. If we want to create a HashSet collection instead, which supports the add() method to test a unique property of a Set. It returns false indicates that inserting a duplicate "Adithya" entry has failed.
Snippet-1
jshell> Set<String> set = Set.of("Adithya", "Chaitanya", "Jai"); set ==> [Jai, Adithya, Chaitanya] jshell> set.add("Adithya"); | java.lang.UnsupportedOperationException thrown: jshell> Set<String> hashSet = new HashSet<>(set); hashSet ==> [Chaitanya, Jai, Adithya] jshell> hashSet.add("Adithya"); $8 ==> false jshell> hashSet hashSet ==> [Chaitanya, Jai, Adithya]
In the below code snippet, we have to implement HashSet in which elements neither stored in the order of insertion nor in sorted order.
Snippet-2
jshell> Set<Integer> numbers = new HashSet<>(); numbers ==> [] jshell> numbers.add(12345); $11 ==> true jshell> numbers.add(1234); $12 ==> true jshell> numbers.add(123); $13 ==> true jshell> numbers.add(12); $14 ==> true jshell> numbers numbers ==> [1234, 12345, 123, 12]
In the below code snippet, we have to implement LinkedHashSet in which elements are stored in the order of insertion.
Snippet-3
jshell> Set<Integer> numbers1 = new LinkedHashSet<>(); numbers1 ==> [] jshell> numbers1.add(12345); $17 ==> true jshell> numbers1.add(1234); $18 ==> true jshell> numbers1.add(123); $19 ==> true jshell> numbers1.add(12); $20 ==> true jshell> numbers1 numbers1 ==> [12345, 1234, 123, 12] jshell> numbers1.add(123456); $22 ==> true jshell> numbers1 numbers1 ==> [12345, 1234, 123, 12, 123456]
In the below code snippet, we have to implement TreeSet in which elements are stored in sorted order.
Snippet-4
jshell> Set<Integer> numbers2 = new TreeSet<>(); numbers2 ==> [] jshell> numbers2.add(12345); $25 ==> true jshell> numbers2.add(1234); $26 ==> true jshell> numbers2.add(123); $27 ==> true jshell> numbers2.add(12); $28 ==> true jshell> numbers2 numbers2 ==> [12, 123, 1234, 12345] jshell> numbers2.add(123456); $30 ==> true jshell> numbers2 numbers2 ==> [12, 123, 1234, 12345, 123456]
- Related Articles
- How to implement a String in JShell in Java 9?
- How to implement a lambda expression in JShell in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement Flow.Publisher interface in Java 9?
- How to declare a class and an interface in JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?\n
- How can we implement a map in JShell in Java 9?
- How to implement the encapsulation concept in JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement integer type conversion in JShell in Java 9?
- How to set a verbose mode in JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement String utility and immutability in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
