Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:29:20+00:00 2026-05-25T01:29:20+00:00

I have a set of 100 object. How can i get a subset of

  • 0

I have a set of 100 object.
How can i get a subset of 5 objects from this set ?

I’m doing this for now but it only returns me one object

          int size = memberSet.size();
      Set<Member> randomSet = new HashSet<Member>();
            int item = new Random().nextInt(size);
            int i = 0;
            for(Member mbr : memberSet)
            {
                if (i == item){
                    randomSet.add(mbr);
            }
                i = i + 1;
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:29:21+00:00Added an answer on May 25, 2026 at 1:29 am
    List<Member> list = new LinkedList<Member>(memberSet);
    Collections.shuffle(list);
    Set<Member> randomSet = new HashSet<Member>(list.subList(0, 5));
    

    Full example:

    public static void main(String... args) {
    
        Set<Member> memberSet = new HashSet<Member>();
        for (int i = 0; i < 100; i++)
            memberSet.add(new Member(i));
    
        List<Member> list = new LinkedList<Member>(memberSet);
        Collections.shuffle(list);
        Set<Member> randomSet = new HashSet<Member>(list.subList(0, 5));
    
        System.out.println(randomSet);
    }
    
    static class Member {
        final int value;
        public Member(int value) {
            this.value = value; 
        }
        @Override
        public String toString() {
            return "" + value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a collection of objects in one list, but each object in
I have a main wrapper div that is set 100% width. Inside that i
Using ASP MVC, I have set up a webpage for localhost/Dinner/100 to show the
i want to have an element inside a div set to height 100%. The
I have a draggable event set up like such: $(ids.label).draggable({ containment: ids.wrapper, revertDuration: 100,
I have an array set up Dim managerList(1 To 50, 1 To 100) As
Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
I have a binding for ListBox but how I can add an event to
Let me try to explain this the best I can. I have a component
I have this WPF slider: <Slider Height=22 HorizontalAlignment=Left Width=100 TickPlacement=BottomRight AutoToolTipPlacement=BottomRight TickFrequency=1 Minimum=10 Maximum=110

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.