Sorting and eliminating duplicates From Hacker Earth:

Problem Statement:
Chotu's father is the owner of a Vada Pav shop. One Sunday, his father takes him to the shop. Father tells him that at the end of the day, Chotu has to give him a list consisting of the names of all the customers on that day who bought Vada Pav(s) from the shop. The list should not have the names of any of the customers being repeated and it should be such that the lexicographically smallest name comes first, ie., the names should be sorted in dictionary order.
As and when a particular customer buys a Vada Pav, Chotu writes down the name of that particular customer. Chotu's initial list is ready, but, he is confused as to how to make the list Father expects from him. Chotu comes to you for help. Your job now is to create the final list, as Father expects from Chotu.
Input :
First line consists of N, the number of names of customers in Chotu's initial list. The next N lines are such that each line consists of a customer's name.
Output :
On the first line, print the total number of names appearing in Chotu's final list. Then print the list such that every customer's name is printed on a new line.
Example
Sample Input:
11        
babu
anand
rani 
aarti
nandu
rani
rani
ap
anand 
babu 
nandu

Sample Output:
6
aarti
anand
ap
babu
nandu
rani


For above problem I've submitted the following code:
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
  Scanner in = new Scanner(System.in);
    Set&lt;String&gt; set = new TreeSet&lt;&gt;();
    int n = in.nextInt();
    for(int i=0;i&lt;n;i++){
        set.add(in.next());
    }
    System.out.println(set.size());
    Iterator i = set.iterator();
    while(i.hasNext()){
        System.out.println(i.next());
    }
}
}

But from 7 test cases, it passed 4 test cases but in rest of the test cases, the time limit exceeds. So my questions is how can I reduce time for this? I was thinking to use BufferedReader to read the whole input as a single String and than splitting it into a String array but the input is new-line separated not space separated. So I can't understand how to use fast I/O method here.
Why make it so complicated? Just use basic java 8 streaming operations and the new-ish BufferedReader.lines() method:

public static void main (String[] args) {
    Set&lt;String&gt; uniqueOrderedNames = new TreeSet&lt;&gt;();
    try(BufferedReader br = new BufferedReader(new InputStreamReader (System.in))){
        br.lines().forEach(uniqueOrderedNames::add);
    } catch (Exception e) {
        // don't do this, use the most specific exception type and handle it
    }
    System.out.println(uniqueOrderedNames.size());
    uniqueOrderedNames.forEach(System.out::println);
}


That much for your approach. Before you now copy paste that, I have some more things to say:


Indent correctly: Methods are indented by one level, blocks are indented 1 level. Levels add up. Your method is missing one level of indentation and your created Scanner should be indented 2 more spaces
Add spaces around binary operators: for(int i=0;i&lt;n;i++){ becomes for (int i = 0; i &lt; n; i++) {, which is significantly easier on the eyes.
Use clear names Set set is nonsensical, it doesn't shed any light whatsoever on what the thing does. Give it a proper name so you can understand what happens.