
- 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 save the elements of a TreeSet to a file in Java?
A TreeSet is a subclass of AbstractSet class and it does not allow duplicate elements. By default, TreeSet stores the elements in an ascending order and retrieval speed of an element out of a TreeSet is faster. The TreeSet class internally uses a TreeMap to store elements. The elements in a TreeSet are sorted according to their natural ordering.
We can also save the elements stored in a TreeSet to a file by using the Arrays.asList() method and pass this set as an argument to the writeObject() method of ObjectOutputStream class.
Syntax
public class TreeSet extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable
Example
import java.util.*; import java.io.*; public class TreeSetTest { public static void main(String args[]) { try { String elements[] = {"Raja", "Jai", "Adithya", "Chaitanya"}; Set<String> set = new TreeSet<String>(Arrays.asList(elements)); FileOutputStream fos = new FileOutputStream("set.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(set); oos.close(); System.out.println("The elements of a Set saved to a File Sucessfully"); } catch(Exception e) { System.out.println("Error Occurred : " + e.getMessage()); } } }
Output
The elements of a Set saved to a File Sucessfully
- Related Articles
- Java Program to Save a String to a File
- How to Save Command Output to a File in Linux?
- Can we save content from JTextField to a file in Java?
- How to save a Python Dictionary to CSV file?
- Iterate through elements of TreeSet in Java
- How many ways to iterate a TreeSet in Java?
- How to read certain number of elements from a file in Java?
- How to save files using a File Chooser in JavaFX?
- How to save a vector in R as CSV file?
- Copy all elements in Java TreeSet to an Object Array
- Fetch elements of Java TreeSet using Iteration
- How to save a matrix as CSV file using R?
- How to convert Stream to TreeSet in Java?
- Remove all elements from TreeSet in Java
- How to save canvas data to file in HTML5?

Advertisements