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 7591945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:46:46+00:00 2026-05-30T20:46:46+00:00

java.util.Set specifies only methods that return all records (via Iterator or array). Why is

  • 0

java.util.Set specifies only methods that return all records (via Iterator or array).

Why is there no option to return any value from Set?

It has a lot of sense in the real life. For example, I have a bowl of strawberries and I want to take just one of them. I totally don’t care which one.

Why I can’t do the same in java?

  • 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-30T20:46:48+00:00Added an answer on May 30, 2026 at 8:46 pm

    This is not answerable. You’d have to ask the original designers of the Java collections framework.

    One plausible reason is that methods with non-deterministic behavior tend to be problematic:

    • They make unit testing harder.
    • They make bugs harder to track down.
    • They are more easily misunderstood and misused by programmers who haven’t bothered to read the API documentation.

    For hashtable-based set organizations, the behavior a “get some element” method is going to be non-deterministic, or at least difficult to determine / predict.

    By the way, you can trivially get some element of a non-empty set as follows:

    Object someObject = someSet.iterator().next();
    

    Getting a truly (pseudo-)random element is a bit more tricky / expensive because you can’t index the elements of a set. (You need to extract all of the set elements into an array …)


    On revisiting this, I realized that there is another reason. It is simply that Set is based on the mathematical notion of a set, and the elements of a set in mathematics have no order. It is simply meaningless to talk about the first element of a mathematical set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any built-in functionality for classical set operations on the java.util.Collection class? My
import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer;
I have a java.util.Set<City> cities and I need to add cities to this set
I have a field aliases of type java.util.Set in one Entity . This Set
In java.util.Calendar , January is defined as month 0, not month 1. Is there
I'm using a java.util.concurrent.ExecutorService that I obtained by calling Executors.newSingleThreadExecutor() . This ExecutorService can
package edu.uci.ics.jung.algorithms.cluster; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.collections15.Transformer;
Why does the java.util.Set interface lack get(int Index) , or any similar get() method?
Spring: In my context.xml, I have: <util:set id=someIDs set-class=java.util.HashSet value-type=java.lang.String> <value>W000000001</value> <value>W000000003</value> <value>W000000009</value> </util:set>
I have a method fetchObjects(String) that is expected to return an array of Contract

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.