473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What will be the output of following java code snippet?

public class Test {
public static void main(String[] args) {
HashSet s = new HashSet();
for(short i = 0; i<100;i++){
s.add(i);
s.remove(i-1);
}
System.out.println(s.size());
}
}

< spoiler removed >
Oct 30 '08 #1
13 4682
JosAH
11,448 Expert 8TB
If you really can't find the answer by thinking about it simpy cook up a compilable
and runnable piece of code, run it and see for yourself.

kind regards,

Jos

ps. answer: 1
Oct 30 '08 #2
karthickkuchanur
156 100+
If you really can't find the answer by thinking about it simpy cook up a compilable
and runnable piece of code, run it and see for yourself.

kind regards,

Jos

ps. answer: 1
Whether it is possible to add the integer to hashset
Oct 31 '08 #3
JosAH
11,448 Expert 8TB
Whether it is possible to add the integer to hashset
Since Java 1.5 it is: autoboxing (but then you need a Set<Short>)

kind regards,

Jos
Oct 31 '08 #4
karthickkuchanur
156 100+
Since Java 1.5 it is: autoboxing (but then you need a Set<Short>)

kind regards,

Jos
Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2. public static void main(String[] args) {
  3. HashSet s = new HashSet();
  4. for(short i = 0; i<100;i++){
  5. s.add(String.valueOf(i));
  6. s.remove(String.valueOf(i-1));
  7. }
  8. System.out.println(s.size());
  9. }
  10. }
i just run above code like this so wat iam asking
Oct 31 '08 #5
JosAH
11,448 Expert 8TB
i just run above code like this so wat iam asking
I'm sorry I don't understand what you're saying; the original example uses a raw
Set; since Java 1.5 you shouldn't do that anymore; use a Set<Short> or Set<Integer>
instead. Is that what you're talking about?

kind regards,

Jos
Oct 31 '08 #6
karthickkuchanur
156 100+
OK thanks ,nothing i dont know what is mean <set>Short.
Oct 31 '08 #7
JosAH
11,448 Expert 8TB
OK thanks ,nothing i dont know what is mean <set>Short.
Read this article for a nice tutorial on generics.

kind regards,

Jos
Oct 31 '08 #8
chaarmann
785 Expert 512MB
I don't think you run your code successfully.
You should get an error for calling String.valueOf(-1), (Index out of bounds exception) because the first time inside the loop i=0 !

Expand|Select|Wrap|Line Numbers
  1. ...
  2. for(short i = 0; i<100;i++){
  3. ...
  4. s.remove(String.valueOf(i-1));
  5. }
  6. ...
  7.  
i just run above code like this so wat iam asking
Oct 31 '08 #9
JosAH
11,448 Expert 8TB
I don't think you run your code successfully.
You should get an error for calling String.valueOf(-1), (Index out of bounds exception) because the first time inside the loop i=0 !
The value -1 is a value to be deleted from a Set, not its index.

kind regards,

Jos
Oct 31 '08 #10
karthickkuchanur
156 100+
Thanks Josh may know wre r u from
Oct 31 '08 #11
I have found the answer to this question at
http://java4all.info/corejava/interview-questions/java-interview-questions.html#q4

Thanks
Nov 4 '08 #12
JosAH
11,448 Expert 8TB
I have found the answer to this question at
http://java4all.info/corejava/interview-questions/java-interview-questions.html#q4

Thanks
Bloody autoboxing ... again it got me ...

kind regards,

Jos
Nov 4 '08 #13
out put : 100.
-----------------------------------------------------------------
remove() in HashSet

public boolean remove(Object o)

Removes the specified element from this set if it is present.

Specified by:
remove in interface Set
Overrides:
remove in class AbstractCollection

Parameters:
o - object to be removed from this set, if present.
Returns:
true if the set contained the specified element.

------------------------------------------------------------------------------------
Since you are requesting size ,you will get 100.
---------------------------------------------------------------------------
JDK1.5 uses concept called autoboxing.
You face no problem in executing the program.
--------------------------------------------------------------------

regards,
shailesh
Nov 4 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Zulik | last post by:
Hello, I have a problem with MessageDigest MD5 in Java. I want to calculate the digest from a file, encode it in base64 and display it. Now, according to MD5 spec, digest shall be 128 bits so,...
56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
4
by: jay | last post by:
I was told that an ostringstream should be terminated by ends. However, the following code snippet does not do what I would expect if that were the case: #include <iostream> #include...
7
by: Neil Zanella | last post by:
OK, this time the compiler's got me a little bit puzzled, simply because it is doing something I am not expecting. My understanding, according to the documentation of std::vector<>::resize(), is...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
2
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
5
by: Claire | last post by:
I've explained the problem in the following code snippet. I want to share single functions for use by several data types. In this function I call different functions to fill out the original object...
5
by: Carlitos | last post by:
Hi there, What would be the counterpart for Java's StringTokenizer class in .NET? In fact, does anybody know a good web site that gives this kind of information, like a Java-.NET/.NET-Java...
2
by: crabsdf | last post by:
My project is a single method class which takes a xml object and, using Apache FOP, transforms it into a PDF which is returned as an output stream. Here is full code package...
12
by: Marc | last post by:
Comming from JSP-HTML when I hear 'script' I think of JavaScript or perhaps VBScript. But I think in ASP.NET they mean something else? I am bit confused now. Reading a book I see the following:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.