Convert String Object to Comparable Object

Getting all types of warnings in Eclipse when I try to cast. I have a method that wants to accept a Comparable<Object> and I would like to pass in a string, float, or integer, and make sure of the Comparable interface.

EJP wrote:
Try Comparable<?>. If that doesn't work please post some code.I will continue this thread then.
inserInSubTree() is to take an attribute and compare it to another using Comparable.
Here is my code:
private PrimaryIndexNode insertInSubtree(Comparable<?> newAttribute,
                                        int newByteIndex, PrimaryIndexNode subTreeRoot)
          if (subTreeRoot == null)
               return new PrimaryIndexNode(newAttribute, newByteIndex, null, null);
          else if(newAttribute.compareTo(subTreeRoot.attribute) < 0)
               subTreeRoot.leftLink = insertInSubtree(newAttribute,
                                                  newByteIndex, subTreeRoot.leftLink);
               return subTreeRoot;
          else
               subTreeRoot.rightLink = insertInSubtree(newAttribute,
                                                  newByteIndex, subTreeRoot.rightLink);
               return subTreeRoot;
     }I am unfamiliar with generics or the proper use of Comparable. what I desire to do is compare any object for ordering it and subsequently inserting it into a binary tree. I figured Comparable could do this and with auto up and down boxing I can do this for primitives and objects.
However, unfamiliarity with what I need to do and having no clear example in my resources has left me without remedy.

Similar Messages

  • How do i convert an object into a string?

    has said above, im trying to convert a object to a string.
    here is what i ahve so far:
    Object nodeInfo = node.getUserObject()

    RTFM
    Object o =...
    String str = o.toString();

  • Convert String variable value  to an Object referece type

    Hi all,
    I want to know that if there any possible way to convert String variable value to a Object reference type in java. I'll explain by example if this is not clear.
    Let's think that there is a string variable name like,
    String name="HelloWorld";
    and there is a class name same to the variable value.
    class HelloWorld
    I am passing this string variable value to a method which is going to make a object of helloworld;
    new convertobj().convert("HelloWorld");
    class convertobj{
    public void convert(String name){
    // in hert it is going to create the object of HelloWorld;
    // HelloWorld hello=new HelloWorld(); just like this.
    now i want to do this from the name variable value.( name na=new name()) like wise.
    please let me know if there any possible way to do that.
    I am just passing the name of class by string variable then i wanted to make a instance of the class which come through the variable value.
    thanx.

    Either cast the object to a HelloWorld or use the reflection API (Google it) - with the reflection API you can discover what methods are available.
    Note: if you are planning to pass in various string arguments and instantiate a class which you expect to have a certain method... you should define an interface and have all of your classes that you will call in this way implement the interface (that way you're sure the class has a method of that name). You can then just cast the object to the interface type and call the method.
    John

  • Convert Map String, Object to Map String,String ?

    How can I convert a map say;
    Map<String, Object> map = new Map<Striing, Object>();
    map.put("value", "hello");
    to a Map<String,String>.
    I want to pass the map to another method which is expecting Map<String,String>.
    Thanks

    JoachimSauer wrote:
    shezam wrote:
    Because im actaully calling an external method to populate map which returns <String, Object>.Now we're getting somewhere.
    Oh wait, no, we're not! We're back to my original reply:
    What do you want and/or expect to happen if one of the values isn't actually a String object but something else?Nothing like a bit of confusion :). They are and always will be String objects.
    So this external method, call it external1 for now returns a Map<String, Object>, I then want to pass this map to another external method external2 which takes Map <String,String> as a parameter.

  • Convert Map String, Object   to    Map Object, Object

    is there any way to convert a map of type Map<String, Object> to a map of type Map<Object, Object>? I tried casting it did not work

    You can also take the long way around:
        Map<Object, Object> map = (Map<Object, Object>)buildMapObject(
          new String[] { "1", "2", "3" },
          new Object[] { new Integer(1), new Integer(100), new Integer(1000) });
      public Object buildMapObject(String[] s, Object[] o) {
        Map<String, Object> map = new HashMap<String, Object>();
    // Populate map from arguments
        return map;
      }Though why you'd want to do that is beyond me.

  • Converting an array of char to a String object

    let's say i have an extremely long character array of 1 million chars and store a text file to it. is it safe to cast the character array into a String object? if not, what is a safe way to convert it to a String? I need to search through the String and create substrings and stuff. Thanks you!

    HOMEWORK ALERT HOMEWORK ALERT
    You can't cast from a character to a String. That doesn't work.
    However, in a fit of being helpful.
    Look at the constructors for the String object in the API
    Link ===> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#String(char[])
    char[] charA = {'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D' } ;
    String a = new String(charA) ;
    System.out.println(a) ;There's a price for my help, and it's this. Now that you understand what you can do, is it a good idea or a bad idea and why ???
    Explain.

  • Converting Base64 encoded String to String object

    Hi,
    Description:
    I have a Base64 encoded string and I am using this API for it,
    [ http://ws.apache.org/axis/java/apiDocs/org/apache/axis/encoding/Base64.html]
    I am simply trying to convert it to a String object.
    Problem:
    When I try and write the String, ( which is xml ) as a XMLType to a oracle 9i database I am getting a "Cannot map Unicode to Oracle character." The root problem is because of the conversion of the base64 encoded string to a String object. I noticed that some weird square characters show up at the start of the string after i convert it. It seems like I am not properly converting to a String object. If i change the value of the variable on the fly and delete these little characters at the start, I don't get the uni code error. Just looking for a second thought on this.
    Code: Converting Base64 Encoded String to String object
    public String decodeToString( String base64String )
        byte[] decodedByteArray = Base64.decode( base64String );
        String decodedString = new String( decodedByteArray, "UTF-8");
    }Any suggestions?

    To answer bigdaddy's question and clairfy a bit more:
    Constraints:
    1. Using a integrated 3rd party software that expects a Base64 encoded String and sends back a encoded base64 String.
    2. Using JSF
    3. Oracle 10g database and storing in a XMLType column.
    Steps in process.
    1. I submit my base64 encoded String to this 3rd party software.
    2. The tool takes the encoded string and renders a output that works correctly. The XML can be modified dynamically using this tool.
    3. I have a button that is binded to my jsf backing bean. When that button is clicked, the 3rd party tool sets a backing bean string value with the Base64 String representing the updated XML.
    4. On the backend in my jsf backing bean, i attempt to decode it to string value to store in the oracle database as a XML type. Upon converting the byte[] array to a String, i get this conversion issue.
    Possibly what is happen is that the tool is sending me a different encoding that is not UTF-8. I thought maybe there was a better way of doing the decoding that i wasn't looking at. I will proceed down that path and look at the possibility that the tool is sending back a different encoding then what it should. I was just looking for input on doing the byte[] decoding.
    Thanks for the input though.
    Edited by: haju on Apr 9, 2009 8:41 AM

  • Object convert String (=' '=)""-- URGENT

    I need to convert few object to string, how i use toString() to convert it ???

    Actually i writing JFormattedTextField, my JFormattedTextField.getValue() which return object, it cause me problem to write a condition checking.e.g checking for null.....
    I had try with JFormattedTextField.toString(), error.....

  • Convert strings to publicKey/privateKey objects

    Hi,
    I have the following problem : I'm generating RSA key pairs in string formats. I want to sign an XML document so I have to specifiy KeyPair object in order to do that.
    How can I convert string to publicKey and privateKey objects ?
    i.e How can I correct this code :
    String pubKey = generatePublicKey();
    String priKey = generatePrivateKey();
    KeyPair kp = new KeyPair(pubKey,priKey);
    // sign the XML documents
    Thanks a lot for your precious help.
    Cheers,
    Othman.

    http://forum.java.sun.com/thread.jspa?threadID=577716&tstart=0

  • How to create a xml file from String object in CS4

    Hi All,
    I want to convert a string object into an XML file using Javascript in Indesign CS4.
    I have done the following script. But it does not convert the namespaces for the xml elements with no value in it.
    var xml = new XML(string);
    The value present in string is "<level_1 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>"
    When it is converted to xml, the value becomes "<level_1 xsi:nil="true"/>"
    On processing the above xml object, am getting an error like this "Uncaught JavaScript exception: Unbound namespace prefix."
    Kindly help.
    Thanks,
    Anitha

    Can you post more of the script?
    Are you getting the XML file from disk, or a string?

  • String object to Date object

    Hi,
    I have date as string object "22/04/2008". I want convert this string object to Date object as specified format (dd/MM/yyyy) only.
    Please do the need full.
    Thanks in Advance

    kishoreyakkali wrote:
    Hi,
    I tried as follows
    SimpleDateFormat strOutDt = new SimpleDateFormat("dd/MM/yyyy");
    Date dateObj=strOutDt.parse(strTmp);
    System.out.println("dateObj::::" + dateObj);
    Output : Mon Apr 21 00:00:00 IST 2008 A date object is always a wrapper around a millisecond value. It does not contain any formatting information, so you should never print a Date object. You should always convert it to a String using SimpleDateFormat before you print.
    Kaj

  • Why create a String object no need to use constructor?

    If we create a Java String object, we will do:
    String s = "Hello";
    And we won't do:
    String s = new String("Hello");
    In API doc, String() constructor description says "Initializes a newly created String object so that it represents an empty character sequence. Note that use of this constructor is unnecessary since Strings are immutable."
    I am not sure how immutable is related to this??
    Also, I wonder if the compiler will convert
    String s = "Hello" to
    String s = new String("Hello");
    Thanks!

    String s = new String("Hello");
    This is a valid statement too..... But the compiler will not convert String s = "Hello" to String s = new String("Hello") as you suggested. The reason is that java has a sort of a String Bag. Everytime you do a String s = "Hello" it will check the bag and see if such a string already exists. If it does, it merely creates a pointer to it, because strings are immutable, it doesn't need to worry about others modifying that string. If the string doesn't exist then it creates the necessary memory for the string object and adds the reference to the bag.
    However, once you do a String s = new String("Hello") what you are saying to the compiler is, "Hey, don't check the bag just create the String." This is all fine and dandy, except that it increases the memory size of your program unnecessarily.
    V

  • Why jvm maintains string pool only for string objects why not for other objects?

    why jvm maintains string pool only for string objects why not for other objects? why there is no pool for other objects? what is the specialty of string?

    rp0428 wrote:
    You might be aware of the fact that String is an immutable object, which means string object once created cannot be manipulated or modified. If we are going for such operation then we will be creating a new string out of that operation.
    It's a JVM design-time decision or rather better memory management. In programming it's quite a common case that we will define string with same values multiple times and having a pool to hold these data will be much efficient. Multiple references from program point/ refer to same object/ value.
    Please refer these links
    What is Java String Pool? | JournalDev
    Why String is Immutable in Java ? | Javalobby
    Sorry but you are spreading FALSE information. Also, that first article is WRONG - just as OP was wrong.
    This is NO SUCH THING as a 'string pool' in Java. There is a CONSTANT pool and that pool can include STRING CONSTANTS.
    It has NOTHING to do with immutability - it has to do with CONSTANTS.
    Just because a string is immutable does NOT mean it is a CONSTANT. And just because two strings have the exact same sequence of characters does NOT mean they use values from the constant pool.
    On the other hand class String offers the .intern() method to ensure that there is only one instance of class String for a certain sequence of characters, and the JVM calls it implicitly for literal strings and compile time string concatination results.
    Chapter 3. Lexical Structure
    In that sense the OPs question is valid, although the OP uses wrong wording.
    And the question is: what makes class Strings special so that it offers interning while other basic types don't.
    I don't know the answer.
    But in my opinion this is because of the hybrid nature of strings.
    In Java we have primitive types (int, float, double...) and Object types (Integer, Float, Double).
    The primitive types are consessons to C developers. Without primitive types you could not write simple equiations or comparisons (a = 2+3; if (a==5) ...). [autoboxing has not been there from the beginning...]
    The String class is different, almost something of both. You create String literals as you do with primitives (String a = "aString") and you can concatinate strings with the '+' operator. Nevertheless each string is an object.
    It should be common knowledge that strings should not be compared with '==' but because of the interning functionality this works surprisingly often.
    Since strings are so easy to create and each string is an object the lack ot the interning functionality would cause heavy memory consumption. Just look at your code how often you use the same string literal within your program.
    The memory problem is less important for other object types. Either because you create less equal objects of them or the benefit of pointing to the same object is less (eg. because the memory foot print of the individual objects is almost the same as the memory footpint of the references to it needed anyway).
    These are my personal thoughts.
    Hope this helps.
    bye
    TPD

  • Unable to read big files into string object  and java.lang.OutOfMemory Prob

    Hi All,
    I have an application that uses applet and servlet communication. On the client side I am reading an large xml file of 12MB size (using JFileChooser) and converting the file to an string object using below code. But I am getting java.lang.OutOfMemory on the client side . But the same below code works fine for small xml files which are less than 4MB sizes:
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF8"), 1024*12);
    String s, s2 = new String();
    while((s = in.readLine())!= null)
         s2 += s + "\n";
    I even tried below code but still java.lang.OutOfMemory is coming:
    while (true)
         int i = in.read();
         if (i == -1)
              break;
         sb.append(i);
    Please let me know what am I doing wrong here ...
    Thanks & Regards,
    Sony.

    Using a String is bad for the following reason:
    When you initially create the String, it has a certain memory size (allocated length if you will). As you keep appending to this String, then memory reallocation will occur over and over, slowing your program down dramatically (ive seen with a 16k x 8 Char file taking 30 secs to read into memory using Strings in this way)
    A Better way would be if you knew the number of characters in the XML file (Using some File size method for example) Then you can use a StringBuffer, which will pre allocate enough space (or try to, it may just be that you cannot create a string as large as you need). You can use toString() method to get the resultant in a String Object (the extra allocated space at the end of the Buffer will be removed)
    StringBuffer strBuf = new StringBuffer(xxxx);
    Where xx is the length (int). Assuming that you are only allowed to enter an int to the constructor then (platform depedant) an int is 2^31 at maximum (or whatever) which allows 2.14e9 characters, therefore an xml file being totally filed would allow a size of ~2048 MB to be read in.
    Try it and see.

  • Unable to read big files into string object & java.lang.OutOfMemory Problem

    Hi All,
    I have an application that uses applet and servlet communication. On the client side I am reading an large xml file of 12MB size (using JFileChooser) and converting the file to an string object using below code. But I am getting java.lang.OutOfMemory on the client side . But the same below code works fine for small xml files which are less than 4MB sizes:
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF8"), 1024*12);
    String s, s2 = new String();
    while((s = in.readLine())!= null)
    s2 += s + "\n";
    I even tried the below code but still java.lang.OutOfMemory is coming:
    while (true)
    int i = in.read();
    if (i == -1)
    break;
    sb.append(i);
    Please let me know what am I doing wrong here ...

    Hi,
    I could avoid the java.lang.OutOfMemory error using below code. But using below code I could read small files of sizes less than 4MB
    but with large files of 12 MB the below code just simply hangs and I am unable to print the string object namely 's'.
    My purpose is to construct an String or StringBuffer object out the user uploaded xml file at the client side and pass that object to server for processing. So how can I construct such object avoid memory problem and increasing the performance of such operations.
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    byte[] b = new byte[in.available()];
    in.read(b, 0, b.length);
    String s = new String(b, 0, b.length);
    in.close();
    Thanks & Regards,
    Sony.

Maybe you are looking for

  • How to get your $200 back from the price drop.

    Many of you likely paid with a credit card and many of the credit card companies have a price protection policy. I know american express and some mastercards do. Basically if you buy a product and the price goes down within 60 or 90 days depending on

  • WiFi NOT working. Please Help

    I bought my ipod touch days ago. Since then i´ve been trying to use the wifi at home and at public places. It connects to the network but when using safari it says: cannot open .... because the server could not be found. Now, a lot of people have bee

  • User Exit for the PO Create in 4.6c version

    Hi Experts, We have a requirement where we want to note the PO number generated in ME21n, schedule a job to run for upadating the same PO( putting Vendor Confirmation ), after the PO is successfully created. Can someone help me the with the exits ava

  • Help In PreparedStatement

    I need some help with SQL and PreparedStatement I have add the PreparedStatement and I'm not to sure if I'm doing it correctly. When I do a test run on Choose 1 ,2 or 3 I now I get this error. Error - com.mysql.jdbc.Statement here is my code below im

  • Having trouble opening and installing Flash CS3

    Hey there, I recently downloaded the Flash CS3 trial version for my mac, and after downloading it seems that i can't complete installation, because once its installed about 2 out of 3 parts it comes up with an installer alert, saying the following "P