Using sets with Strings

Hello, I'm in a dilemma here
I'm using a Set to store Strings:
private Set<String> X;
I have a class that receives a String and first check if the string is allready on the set and if not it adds it:
public boolean method (V e) {
if(X.contains(e.getString()) )
return false;
Y.add(e.getString());
return true;
So I got confussed when looking at the properties of a Set, (i.e. do I need to check if the string is allready there, or it'll do it for me?)
and if I have to check it myself can I use .contains() to compare the given string to all strings in the set?
Thanks
Edited by: edsc86 on Nov 18, 2008 7:03 PM
Edited by: edsc86 on Nov 18, 2008 7:04 PM
Edited by: edsc86 on Nov 18, 2008 7:05 PM

Dude! Do NOT screw around with the colors like that. It makes you post way hard to read. Use the CODE button or [[code] and [[/code] tags for sections of code, and an occasional italic or bold for emphasis, and the quote button or a > for quoting.
You don't need to check if it's there. Read the docs for Set. They tell you that it will add it if it's not there, and return true if the set was modified (that is, if it wasn't already there).
Set<String> set = new HashSet<String>();
boolean b1 = set.add("x"); // adds x, returns true
boolean b2 = set.add("x"); // does nothing to the set, returns false

Similar Messages

  • Can we use Iteartor with String[ ] a

    Hii javaites
    Can somebody tell me wht is the differnce between
    String[ ] a & String a[ ],
    and can we use Iterator with String[ ]a .
    Plz tell me some code to transeverse through a ;
    Thanx

    import java.util.Arrays;
    import java.util.Iterator;
    public class StringIt {
        public static void main(String[] args) {
                // two ways of declaring string arrays
            String strArrA[], strB;
            String[] strArrC, strArrD;
                // the first would declare an array and a string
            strArrA = new String[] {"a", "string", "array"};
            strB = "Just a string";
                // the second would declare two arrays
            strArrD = new String[] {"another", "string", "array"};
            strArrC = strArrD;
                // "old fashioned" iteration through an array
            for(int ndx = 0; ndx < strArrC.length; ndx++) {
                System.out.println(strArrC[ndx]);
                // you can use Iterator, but why?
            Iterator<String> it = Arrays.asList(strArrC).iterator();
            while(it.hasNext()) {
                System.out.println(it.next());
                // array iteration the new way!  The Iterator is
                // still being used, but is "hidden"
            for(String str :strArrC) {
                System.out.println(str);
    }

  • Can we use sequences with strings?

    In Sql can we use sequence with strings? ......please tel me

    Hi,
    Sorry, sequences only generate numbers.
    You could write a function in PL/SQL which generates a string, based on the value it gets from a sequence.
    Edited by: Frank Kulash on Jan 22, 2009 2:31 PM
    Of course, you can convert the number to a string, like Salim demonstrated. I assume you're talking about something that generates, for example, 'A', 'B', ..., 'Z', 'AA', 'BB', ..., 'ZZ', 'AAA', ...

  • Performance issue in linux while using set with URL object

    Hi,
    I am facing performance issue while using Set(HashSet) with URL object on linux. But it is running perfectly on windows.
    I am using
    set.contains(urlObject)
    Above particular statement is taking 40 sec on Linux, and only a fraction of ms on windows.
    I have checked the jre version on both OS. It is the same version (jre6)
    on both the OS.
    Could anyone please tell me what is the exact reason, why the same statement is taking more time on linux than windows.
    Thanks & Regards
    Naveen

    jtahlborn wrote:
    I believe the URL hashCode/equals implementations have some /tricky behavior which involves network access in order to run (doing hostname lookups and the like). you may want to either use simple Strings, or possibly the URI class (i think it fixed some of this behavior, although i could be wrong).The second new thing I have learned today. I was wrong in reply # 1 because looking at the URL code for 1.6 I see that the hash code is generated from the IP address and this has a lazy evaluation. Each URL placed in a HashMap (or other hash based collection) requires a DNS lookup the first time the hash code is used.
    P.S. 40 seconds does seem a long time for a DNS lookup!
    Edited by: sabre150 on Feb 13, 2008 3:40 PM

  • Using intern() with String objects

    Hi, I was wondering if anybody could explain to me more about using the method intern() with String objects.
    Suppose I have a String, say testString1.
    I want to make it uppercase and I would like to refer to it/use it in many places in the code.
    Does using the statement
    testString1.toUppercase().intern()
    put the uppercase testString1 in some kind of a pool? so that everytime I call testString1.toUppercase(), a new testString1 in uppercase is not created each time? Instead it is taken from the pool? Is that how this works?
    I would appreciate a response from anybody who knows about this stuff.
    Thanks.

    No !!!... It doesn't work that way.
    I would recommand you (and anyone alse) not to use String.intern() unless you have a COMPLETE understanding of how it works.

  • Setting VarChar Variable with String Parameter in Crystal Report Add Command

    Setting a VarChar Variable with a String Parameter in a Crystal Report 2008 SP6.3 Add Command is causing an error with a SQL State of 42000.  The Error Code depends on which value I select for the parameter.  I created the @DateOpt string parameter in the Modify Command window, not in the report. If I comment out line 14 of the SQL the error does not occur.
    I have attached the SQL Code and screen shots of the errors.   Please tell me what I am doing wrong.

    Since your parameter is of type STRING, you need to enclose references to it within the Command inside single quotes.
    For example, instead of
    SET @BeginDate ={?BeginDate}
    use
    SET @BeginDate ='{?BeginDate}'
    hth,
    - ido

  • If user disable cookie how to set and use session with URL Rewritting

    if user disable cookie how to set and use session with URL Rewritting by append session ID in url

    If cookies are disabled, then app server will automatically try to use URL rewriting for session control. Programmer's responsibility is to encode any links or redirects using
    response.encodeURL("/yourPage.jsp")
    and
    response.encodeRedirectURL("/yourPage.jsp")
    See API for details
    http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletResponse.html#encodeURL(java.lang.String))

  • I am using Mail with OS 10.9.1...I used to be able to drag and drop contact addresses from my address book to an e-mail header.  Now I have to type all the names and can't drag and drop.  Is there a setting for this?  Thanks!

    I am using Mail with OS 10.9.1.  I used to be able drag and drop names from my address book into the address header, but can no longer do that.  I now have to type each name in.  Does anyone know if there is a setting for this or is it a feature of the updated software?  Thanks for any info!

    Yes, that appears how Mavericks works right now. However, you can open the Address Panel, select all your contacts, then click the To (or CC, or Bcc) buttons to move the selection to the address field.
    If it is not already in the Toolbar, right-click on the Toolbar and select Customize Toolbar…
    Then, drag the address panel up to wherever you want it.
    You may also want to add it to the New Message window in the same way.

  • How do i set the proxy user in FF 3.6.13, this entry was existing earlier its gone now. using IE with entries in user account pwds works while FF doesn't.

    How do i set the proxy user in FF 3.6.13.
    previous versions had an entry for proxy user.
    its gone now.
    using IE with entries in user account pwds works while FF doesn't.
    too bad have to change back to IE :-(

    You can find the connection settings in Tools > Options > Advanced : Network : Connection
    See "Firefox connection settings":
    *[[Firefox cannot load websites but other programs can]]

  • If i set up my iphone on a pc, can i still use it with my mac?

    i just got a hand-me-down iphone 3gs that's been restored and updated, etc. however, i am currently using a pc until i get my new macbook (which will probably not be for a few months). if i use itunes with my phone on my pc now, can i still set it up to sync later when i get my new laptop? or is it the same as with ipods, like once you set up on a pc, you can't add anything to it from a mac?

    Depends on what you want to sync.
    Contacts and calendars - yes with icloud.
    photos yes with computer and future pictures with icloud.
    Apps - does not make since, since apps are different for ipad and iphone.

  • There is a feature on the Ad on using iPad with apple tv as a dual screen such as the iPad as agame controller, how is this being set up or done?

    There is a feature on the Ad on using iPad with apple tv as a dual screen such as the iPad as agame controller, how is this being set up or done?

    This is something the app developer enables within airplay mirroring.
    There is nothing you can do to enable that.
    If you are unsure how to enable mirroring
    http://support.apple.com/kb/HT5209?viewlocale=en_US&locale=en_US

  • Using OR operator with string

    How we can use OR operator with string in java??

    Gaurav1 wrote:
    its logical OR operator;how can we use it with matcher classLike its been said already. The "logical OR" is used the same everywhere.
    Why don't you post the code you're having problems with. (Only the relevant areas, please. And use code tags.)

  • How do I change SSID setting? Getting message "using network with default ssid'

    Getting message "using network with default ssid'

    First, let's address the biggest issue, the version of Firefox you are using is extremely old, out of date and insecure. Please update to Firefox 33 ASAP ([[Update Firefox to the latest version]])
    After that, please give a bit more context on what exactly is going on. Keep in mind this is a Firefox support forum, we can't help you with Windows settings or setting up wireless networks.

  • How to use SELECTION-SET with FREE-SELECTIONS?

    Hi,
    how to use SELECTION-SET with FREE-SELECTIONS?
    Regards,
    Vinay.

    Check http://help.sap.com/abapdocu_70/en/ABAPSUBMIT_SELSCREEN_PARAMETERS.htm

  • How to use Special value set with Column clause to display addtional column

    hi
    can anyone tell me
    How to use Special value set with Column clause to display addtional column
    how can i use FND POPID and with COLUMN clause ?

    Dear Yaseen,
                To get the value in formula cumulative,after you have created the formula for that particular key figure,go to edit and click on tab "calculations".
    In that select overall result as 'count all values not equal to zero' and single result as 'count all values not equal to zero' .
    And mark the cumulative check box underneath.
    I hope you get the desired result and please do me favour .
    Sumit goomber

Maybe you are looking for