I believe instead of set.length, you'll want to call set.size()
GeeksforGeeks
geeksforgeeks.org › java › set-size-method-in-java-with-example
Set size() method in Java with Example - GeeksforGeeks
July 26, 2024 - // Java code to illustrate Set.size() method import java.util.*; public class SetDemo { public static void main(String args[]) { // Creating an empty Set Set<String> set = new HashSet<String>(); // Use add() method to add elements into the Set set.add("Welcome"); set.add("To"); set.add("Geeks"); set.add("4"); set.add("Geeks"); // Displaying the Set System.out.println("Set: " + set); // Displaying the size of the Set System.out.println("The size of the set is: " + set.size()); } } Output ·
Top answer 1 of 2
33
I believe instead of set.length, you'll want to call set.size()
2 of 2
7
Your set is a Set<String>, not a String[]. So you have to call:
int x = set.size();
How does the size() of the Set interface work in Java? Instead of the expected number of items in my set it evaluates to 1. What's wrong with my code?
Especially if you're a relative beginner, I would recommend explicitly declaring your types instead of using the var keyword. If you had declared uniqueNumbers with the type you expected it to have (presumably Set) then the compiler would have given you an error, telling you that the actual type was different. More on reddit.com
Preferred Size, Minimum Size, Size
This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at... More on thecodingforums.com
How to find the size of HashSet in Java (Core Java Interview ...
06:33
What is the difference between length, length() and size() (Core ...
17:58
🔔 Top 5 mistakes with Java Sets! - YouTube
Set and HashSet in Java - Full Tutorial
00:52
How to check the size of the HashSet? | HashSet (Size) | Java ...
07:42
Java Using setPreferredSize with BoarderLayout - YouTube
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Set.html
Set (Java Platform SE 8 )
3 weeks ago - Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set.
Reddit
reddit.com › r/learnprogramming › how does the size() of the set interface work in java? instead of the expected number of items in my set it evaluates to 1. what's wrong with my code?
r/learnprogramming on Reddit: How does the size() of the Set interface work in Java? Instead of the expected number of items in my set it evaluates to 1. What's wrong with my code?
November 18, 2022 -
I'm trying to do a little exercise: for a given array and a given difference k, return the unique pairs of the array which difference is k.
The logic is not too complicated: Get a set from the array to make sure items are unique, get unique pairs with two loops, if difference == k, increment the counter.
public static int countPairsWithDiff(int[] numbers, int difference) {
var uniqueNumbers = Set.of(numbers);
var count = 0;
var ln = uniqueNumbers.size();
for (int i = 0; i < ln ; i++) {
for (int j = i; j < ln; j++) {
if (Math.abs(i-j) == difference) {
count++;
}
}
}
return count;
}The .size() method evaluates to 0 (edit: 1) however. Why is this?
Top answer 1 of 2
3
Especially if you're a relative beginner, I would recommend explicitly declaring your types instead of using the var keyword. If you had declared uniqueNumbers with the type you expected it to have (presumably Set) then the compiler would have given you an error, telling you that the actual type was different.
2 of 2
2
Set.of doesn't work like that, now you get a set containing one array of ints. Set.of is used like Set.of(1, 2, 3, 4) If you want to convert your array to set, you can do something like new HashSet(Arrays.asList(numbers)); I don't understand how you'd get size of 0 though, should be 1, and even is when i tested your code.
Demmel
demmel.com › joc › help › JavaDocumentation › java › util › Set › size.htm
Method java.util.Set.size
Package java.util Interface Set · abstract int size() _INSERT_METHOD_SIGNATURE_HERE_ Description: Retunrs the number of elements in this set (its cardinality).
W3Schools
w3schools.com › java › java_set.asp
Java Set
The Set interface is part of the Java Collections Framework and is used to store a collection of unique elements. Unlike a List, a Set does not allow duplicates, and it does not preserve the order of elements (unless you're using TreeSet or LinkedHashSet). ... Tip: Use a Set when you need to store unique values only. Next, you'll explore the HashSet class in detail. ... Coding fundamentals as bite-sized ...
GeeksforGeeks
geeksforgeeks.org › java › set-in-java
Set in Java - GeeksforGeeks
Set interface is a part of the Java Collection Framework, located in the java.util package.
Published: 2 weeks ago
Oracle
docs.oracle.com › javase › 8 › docs › api › java › awt › Dimension.html
Dimension (Java Platform SE 8 )
July 21, 2026 - Sets the size of this Dimension object to the specified width and height in double precision.
CodeSignal
codesignal.com › learn › courses › mastering-complex-data-structures-in-java › lessons › understanding-sets-in-java
Understanding Sets in Java
In this lesson, you'll learn how to create and operate on sets using HashSet<E>. You'll explore the advantages of using sets and how they can optimize performance. Let's get started! In Java, you can create a set using the HashSet<E> class.
CodeGym
codegym.cc › java blog › java collections › java set
Java Set
February 13, 2025 - Set is an interface from the Java Collection Framework, but Set is not an ordered collection, unlike List. That means that Java Set elements are kept without a particular order. So there is no control over the position where you can insert an element. Also you can't access elements by their index.
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
abs() acos() addExact() asin() atan() atan2() cbrt() ceil() copySign() cos() cosh() decrementExact() exp() expm1() floor() floorDiv() floorMod() getExponent() hypot() IEEEremainder() incrementExact() log() log10() log1p() max() min() multiplyExact() negateExact() nextAfter() nextDown() nextUp() pow() random() rint() round() scalb() signum() sin() sinh() sqrt() subtractExact() tan() tanh() toDegrees() toIntExact() toRadians() ulp() Java Output Methods ... add() addAll() clear() clone() contains ensureCapacity() forEach() get() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() removeIf() replaceAll() retainAll() set() size() sort() spliterator() subList() toArray() trimToSize() Java LinkedList Methods
The Coding Forums
thecodingforums.com › archive › archive › java
Preferred Size, Minimum Size, Size | Java | Coding Forums
May 25, 2008 - From what I can tell: setPreferredSize(Dimension) - This is (obviously) the preferred size of the component. As far as I can tell, this is used to determine what size the component should be when pack is used. setMinimumSize - The component ...
Visual Studio Code
code.visualstudio.com › docs › editing › codebasics
Basic editing
November 3, 2021 - If your file uses a different number of spaces, the indentation may not be correctly detected. For example, if your convention is to indent with 3 spaces, you may want to turn off editor.detectIndentation Open in VS Code Open in VS Code Insiders and explicitly set the tab size to 3.
AlphaCodingSkills
alphacodingskills.com › java › notes › java-stack-setsize.php
Java Stack setSize() Method - AlphaCodingSkills
The java.util.Stack.setSize() method is used to set the size of the stack. If the new size is greater than the current size, new null items are added to the top of the stack. If the new size is less than the current size, all components at index newSize and greater are discarded.
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Set.html
Set (Java SE 17 & JDK 17)
April 21, 2026 - This method also accepts a single array as an argument. The element type of the resulting set will be the component type of the array, and the size of the set will be equal to the length of the array.
GeeksforGeeks
geeksforgeeks.org › java › stack-setsize-method-in-java-with-example
Stack setSize() method in Java with Example - GeeksforGeeks
December 24, 2018 - // Java program to demonstrate // setSize() method for String value import java.util.*; public class GFG1 { public static void main(String[] argv) { try { // Creating object of Stack<Integer> Stack<String> stack = new Stack<String>(); // adding ...
Facebook
facebook.com › groups › 379120905458325 › posts › 26307978538812540
Easier cleaning for 20 gallon aquarium?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment