1

Is there a way to make the TreeSet sorted by the time of entering the keys not by the value itself
i mean if

TreeSet a= new TreeSet() ;   
a.add("Zenda") ;   
a.add("Apple") ;  

then i printed the treeSet
i want to have "Zenda" then "Apple"
not sorted lexicographically .

  • You should [not use raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – rgettman Jun 27 '19 at 17:36

2 Answers2

1

You could use LinkedHashSet. That is a HashSet that keeps insertion order.

k5_
  • 5,450
  • 2
  • 19
  • 27
0

You can use LinkedList or ArrayList in java that will maintain the insertion order. You can also print reverse order in LinkedList using ListIterator interface.

HashSet or LinkedHashSet does not allow duplicate values.

Maxx Selva K
  • 454
  • 4
  • 9