The Artima Developer Community
Sponsored Link

Java Answers Forum
List And Set

5 replies on 1 page. Most recent reply: Sep 14, 2003 8:07 PM by wdzwdz

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
guru

Posts: 3
Nickname: codecguru
Registered: Sep, 2003

List And Set Posted: Sep 11, 2003 3:06 AM
Reply to this message Reply
Advertisement
how to convert string argument to List type as well as Set type


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: List And Set Posted: Sep 11, 2003 3:13 AM
Reply to this message Reply
List??
java.awt.List or what??

And set?? u can add strings to a class that implements Set.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: List And Set Posted: Sep 11, 2003 6:29 PM
Reply to this message Reply
I think the person is reffering to java.util.List interface.

import java.util.*;
public class Test{
  public static void main(String args[]){
     List myList = new ArrayList();
     Set mySet = new HashSet();
     for (int i=0; i<args.length; i++){
	myList.add(args[i]);
	mySet.add(args[i]);
     }
     System.out.println(myList.toString());
     System.out.println(mySet.toString());
   }
}

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: List And Set Posted: Sep 12, 2003 5:59 AM
Reply to this message Reply
Actually, there are some convenience methods in the Collections API for this:

import java.util.*;
public class Test{
  public static void main(String args[]){
     List myList = Arrays.toList(args);
     Set mySet = new HashSet(myList);
     // I think the toString() call might be redundant
     // println internally invokes the object's toString()
     System.out.println(myList);
     System.out.println(mySet);
   }
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: List And Set Posted: Sep 12, 2003 7:39 PM
Reply to this message Reply
I checked the API of ArrayList and HashSet to see whether it takes an Array as an Argumnet in one of its consructor. But it didn't click to me to check Array. Thanks man

wdzwdz

Posts: 3
Nickname: wdzwdz
Registered: Dec, 2002

Re: List And Set Posted: Sep 14, 2003 8:07 PM
Reply to this message Reply
//List myList = Arrays.toList(args);
//should is
List myList = Arrays.asList(args);

Flat View: This topic has 5 replies on 1 page
Topic: How do I save my changes to XML file? Previous Topic   Next Topic Topic: deploying ordinary beans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use