-3

This is what i tried.

import java.util.*;
public class SetDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet<String> s1=new HashSet<String>();
        s1.add("Java");
        s1.add("C++");
        s1.add("C");
        s1.add("dotNet");
        s1.add("JavaScript");
        s1.add("Script");
        s1.add("Python");
        s1.add("JavaScript");
    }
}

Output [Java, Script, C++, dotNet, C, JavaScript, Python]

Edit:Just read this , i think it might be answer to what i was asking

  • 6
    Does this answer your question? [Java Set retain order?](https://stackoverflow.com/questions/10752753/java-set-retain-order) – dbl Aug 19 '20 at 13:04
  • 1
    Because it uses hashing, which by definition assigns a random position to elements added. The point of this is to get O(1) time complexity. – user13784117 Aug 19 '20 at 13:09
  • @user13784117 this is not right. Sets have no order by nature. All members of the sets are equal in matter. [wikked it](https://en.wikipedia.org/wiki/Set_(abstract_data_type))... – dbl Aug 19 '20 at 13:11
  • (1) This is Java programming, not mathematics, See TreeSet. It's a Set with order. (2) But for mathematics - consider the set of natural numbers. Do you contend there is no ordering? – user13784117 Aug 19 '20 at 13:16
  • Who said mathematics? I see you don't follow random links so I will quote: **In computer science, a set is an abstract data type that can store unique values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.** The most intuitive implementation would use hashing and then what you said will be true. – dbl Aug 19 '20 at 14:14
  • ok, thanks got it. Actually i wanted to see the how that set would have been implemented in java. – Pranav Mittal Aug 19 '20 at 14:19
  • @PranavMittal then maybe you would like to read [about buckets](https://stackoverflow.com/questions/37959941/what-exactly-is-bucket-in-hashmap) – dbl Aug 19 '20 at 14:23
  • @dbl - ok, I'll rephrase - it's Java programming, not theoretical computer science. Java has something it calls a Set which is ordered. But in any case, the OP is talking specifically about a HashSet, to *only* which my original comment applied. – user13784117 Aug 19 '20 at 15:36
  • @user13784117 [TreeSet-Set](https://en.wikipedia.org/wiki/Circle%E2%80%93ellipse_problem#:~:text=The%20circle%E2%80%93ellipse%20problem%20in,subtype%20polymorphism%20in%20object%20modelling.&text=The%20existence%20of%20the%20circle,to%20criticize%20object%2Doriented%20programming.)? – dbl Aug 20 '20 at 09:10

2 Answers2

6

From the relevant JavaDoc:

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

Basically, a HashSet:

  • stores unique elements and permits nulls
  • is backed by a HashMap
  • doesn't maintain insertion order
  • is not thread-safe
JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
2

You need you to use LinkedHashSet<>() for the same.

import java.util.*;
public class SetDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashSet<String> s1=new LinkedHashSet<String>();
        s1.add("Java");
        s1.add("C++");
        s1.add("C");
        s1.add("dotNet");
        s1.add("JavaScript");
        s1.add("Script");
        s1.add("Python");
        s1.add("JavaScript");
    }
}

You can also use the SortedSet if you want natural order.

SSK
  • 3,444
  • 6
  • 32
  • 59
  • 2
    Yeah, this is a good answer. Might also want to update to include `SortedSet` if OP wanted to use natural order and not insertion order. – Jason Aug 19 '20 at 13:04
  • It sounds to me more like the OP would like to use a list instead... – dbl Aug 19 '20 at 13:07
  • @Jason completely agree with you if we need natural order then we can use `SortedSet` – SSK Aug 19 '20 at 13:07
  • However, one ought to ask **why** the OP thinks maintaining insertion order is desirable for his use. – user13784117 Aug 19 '20 at 13:10