Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,700 questions

55,459 answers

573 users

How to create a set of objects in Java

3 Answers

0 votes
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        
        // Adding elements to the set
        set.add("Apple");
        set.add("Banana");
        set.add("Strawberry");
        
        System.out.println(set);
    }
}


/*
run:

[Apple, Strawberry, Banana]

*/

 



answered Apr 22, 2025 by avibootz
0 votes
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<String> set = new TreeSet<>();
        
        // Adding elements to the set
        set.add("Apple");
        set.add("Banana");
        set.add("Strawberry");
        
        System.out.println(set);
    }
}


/*
run:

[Apple, Banana, Strawberry]

*/

 



answered Apr 22, 2025 by avibootz
0 votes
import java.util.LinkedHashSet;

public class Main {
    public static void main(String[] args) {
        LinkedHashSet<String> set = new LinkedHashSet<>();
        
        // Adding elements to the set
        set.add("Apple");
        set.add("Banana");
        set.add("Strawberry");
        
        System.out.println(set);
    }
}



/*
run:

[Apple, Banana, Strawberry]

*/

 



answered Apr 22, 2025 by avibootz

Related questions

1 answer 177 views
1 answer 263 views
263 views asked Jan 16, 2022 by avibootz
4 answers 419 views
419 views asked Jul 25, 2019 by avibootz
3 answers 221 views
1 answer 110 views
1 answer 207 views
...