title: Why does my main sorted set treeset get modified when i modify my tailset in java?
body: <p>Please refer to the doIt function.</p>
  <p>Whenever I store my tailset in a variable and perform an action on it, it also changes my ss variable's value. How do I remove the first element of my tailset without removing it from my main sortedset, ss?</p>
  <p>input file:</p>
  <p>apple</p>
  <p>cheese</p>
  <p>aperitif</p>
  <p>apartheid</p>
  <p>danube</p>
  <p>ape</p>
  <p>cheap</p>
  <p>cheapen</p>
  <p>app</p>
  <p>chea</p>
  <p>dan</p>
  <p>output file:
  apple</p>
  <p>ss: [apple, cheese]
  ss after remove[apple]</p>
  <p>ss: [aperitif, apple]
  ss after remove[apple]</p>
  <p>ss: [apartheid, apple]
  ss after remove[apple]</p>
  <p>ss: [apple, danube]
  ss after remove[apple]</p>
  <p>ss: [ape, apple]
  ss after remove[apple]</p>
  <p>ss: [apple, cheap]
  ss after remove[apple]</p>
  <p>ss: [apple, cheapen]
  ss after remove[apple]</p>
  <p>ss: [app, apple]
  ss after remove[apple]</p>
  <p>ss: [apple, chea]
  ss after remove[apple]</p>
  <p>ss: [apple, dan]
  ss after remove[apple]</p>
  <pre><code>import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.util.*;
  
  public class Part4 {
  
      /**
       * 
       * @param r the reader to read from
       * @param w the writer to write to
       * @throws IOException
       */
  
      public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
          SortedSet&lt;String&gt; ss = new TreeSet&lt;String&gt;();
          for (String line = r.readLine(); line != null; line = r.readLine()) {
              if (ss.isEmpty()) {
                  ss.add(line);
                  w.println(line);
              } else {
                  w.println(&quot;&quot;);
                  ss.add(line);
                  w.println(&quot;ss: &quot; + ss);
                  SortedSet&lt;String&gt; tailset = new TreeSet&lt;&gt;();
                  tailset = ss.tailSet(line);
                  tailset.remove(line);
                  w.println(&quot;ss after remove&quot;+ss);
  
              }
  
          }
      }
  
      /**
       * The driver. Open a BufferedReader and a PrintWriter, either from System.in
       * and System.out or from filenames specified on the command line, then call
       * doIt.
       * 
       * @param args
       */
      public static void main(String[] args) {
          try {
              BufferedReader r;
              PrintWriter w;
              if (args.length == 0) {
                  r = new BufferedReader(new InputStreamReader(System.in));
                  w = new PrintWriter(System.out);
              } else if (args.length == 1) {
                  r = new BufferedReader(new FileReader(args[0]));
                  w = new PrintWriter(System.out);
              } else {
                  r = new BufferedReader(new FileReader(args[0]));
                  w = new PrintWriter(new FileWriter(args[1]));
              }
              long arrlirt = System.nanoTime();
              doIt(r, w);
              w.flush();
              long stop = System.nanoTime();
              System.out.println(&quot;Execution time: &quot; + 1e-9 * (stop - arrlirt));
          } catch (IOException e) {
              System.err.println(e);
              System.exit(-1);
          }
      }
  }
  </code></pre>