Java List parameterized?
I am quite new to Java . I wrote a class called DLPFile which is basically a container of other objects like Strings, ints, floats, etc. When putting my files into a List and then saving it in my session (which is from Map class) variable is easy;
DLPFile file = new DLPFile(); List fileList = new ArrayList (); fileList.add(file); session.put("filesList", fileList);
"List is a raw type.References to generic type List should be parameterized."
List ) session.get("fileslist"); List ; and List session.get("fileslist");
5 Answers 5
This is because of the Generics Type erasure. The compiler has no way to determine the actual generic type argument when you get it from your session (except if you session.get takes a Class argument to cast it accordingly), because the session probably only returns the type of object. You can make sure that your object is an instance of List, but the generic type information is lost (basically the compiler converts it to a List internally). That is why you get the warning, because only you as the programmer can know if the generic type parameter you want to cast it to is the right one.
If you don’t like the warning you may add an
@SuppressWarnings("unchecked")
Annotation at the beginning of your method.
No there isn’t a way because of how Generics are implemented in Java. The only way is for the session object to support a generic parameter in the get method (but it woon’t do anything different than an unchecked cast). It is just a warning, not an error. List is the equivalent of List. Best way would be to derive your own class from List cause then you are checking against an actual class and not a generic interface.?>
Java parameter list string
This class holds MIME parameters (attribute-value pairs). The mail.mime.encodeparameters and mail.mime.decodeparameters System properties control whether encoded parameters, as specified by RFC 2231, are supported. By default, such encoded parameters are not supported.
Also, in the current implementation, setting the System property mail.mime.decodeparameters.strict to «true» will cause a ParseException to be thrown for errors detected while decoding encoded parameters. By default, if any decoding errors occur, the original (undecoded) string is used.
The current implementation supports the System property mail.mime.parameters.strict , which if set to false when parsing a parameter list allows parameter values to contain whitespace and other special characters without being quoted; the parameter value ends at the next semicolon. If set to true (the default), parameter values are required to conform to the MIME specification and must be quoted if they contain whitespace or special characters.
Author: John Mani, Bill Shannon
Constructor Summary | |
---|---|
ParameterList () No-arg Constructor. | |
ParameterList (java.lang.String s) Constructor that takes a parameter-list string. |
Method Summary | |
---|---|
java.lang.String | get (java.lang.String name) Returns the value of the specified parameter. |
java.util.Enumeration | getNames () Return an enumeration of the names of all parameters in this list. |
void | remove (java.lang.String name) Removes the specified parameter from this ParameterList. |
void | set (java.lang.String name, java.lang.String value) Set a parameter. |
void | set (java.lang.String name, java.lang.String value, java.lang.String charset) Set a parameter. |
int | size () Return the number of parameters in this list. |
java.lang.String | toString () Convert this ParameterList into a MIME String. |
java.lang.String | toString (int used) Convert this ParameterList into a MIME String. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
ParameterList
ParameterList
Constructor that takes a parameter-list string. The String is parsed and the parameters are collected and stored internally. A ParseException is thrown if the parse fails. Note that an empty parameter-list string is valid and will be parsed into an empty ParameterList.
Parameters: s — the parameter-list string. Throws: ParseException — if the parse fails.
Method Detail |
---|
size
Returns: number of parameters.
get
public java.lang.String get(java.lang.String name)
Parameters: name — parameter name. Returns: Value of the parameter. Returns null if the parameter is not present.
set
public void set(java.lang.String name, java.lang.String value)
Parameters: name — name of the parameter. value — value of the parameter.
set
public void set(java.lang.String name, java.lang.String value, java.lang.String charset)
Set a parameter. If this parameter already exists, it is replaced by this new value. If the mail.mime.encodeparameters System property is true, and the parameter value is non-ASCII, it will be encoded with the specified charset, as specified by RFC 2231.
Parameters: name — name of the parameter. value — value of the parameter. charset — charset of the parameter value. Since: JavaMail 1.4
remove
public void remove(java.lang.String name)
Removes the specified parameter from this ParameterList. This method does nothing if the parameter is not present.
Parameters: name — name of the parameter.
getNames
public java.util.Enumeration getNames()
Returns: Enumeration of all parameter names in this list.
toString
public java.lang.String toString()
Convert this ParameterList into a MIME String. If this is an empty list, an empty string is returned.
Overrides: toString in class java.lang.Object Returns: String
toString
public java.lang.String toString(int used)
Convert this ParameterList into a MIME String. If this is an empty list, an empty string is returned. The ‘used’ parameter specifies the number of character positions already taken up in the field into which the resulting parameter list is to be inserted. It’s used to determine where to fold the resulting parameter list.
Parameters: used — number of character positions already used, in the field into which the parameter list is to be inserted. Returns: String
| ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2009-2011, Oracle Corporation and/or its affiliates. All Rights Reserved. Use is subject to license terms.
Generated on 10-February-2011 12:41
How to initialize List object in Java?
If you check the API for List you’ll notice it says:
Being an interface means it cannot be instantiated (no new List() is possible).
If you check that link, you’ll find some class es that implement List :
All Known Implementing Classes:
AbstractList , AbstractSequentialList , ArrayList , AttributeList , CopyOnWriteArrayList , LinkedList , RoleList , RoleUnresolvedList , Stack , Vector
Some of those can be instantiated (the ones that are not defined as abstract class ). Use their links to know more about them, I.E: to know which fits better your needs.
The 3 most commonly used ones probably are:
List supplierNames1 = new ArrayList(); List supplierNames2 = new LinkedList(); List supplierNames3 = new Vector();
Bonus:
You can also instantiate it with values, in an easier way, using the Arrays class , as follows:
List supplierNames = Arrays.asList("sup1", "sup2", "sup3"); System.out.println(supplierNames.get(1));
But note you are not allowed to add more elements to that list, as it’s fixed-size .
Arrays.asList is great but as you should have noticed by now fails to do what one expects in cases like int[] a = <1,2,3>; System.out.println(Arrays.asList(a)); // [[I@70cdd2]1,2,3>
@J.A.I.L. That’s not what’s happening. That’s a list of one element, where the one element is an array of ints. The wanted behaviour was probably a list of three elements 1, 2, and 3.
@Christoffer Hammarström to print a list or an array, you could use java.util.Arrays.toString(array);, so this feature is already given..
If you need the resulting list to be an ArrayList , use new ArrayList<>(Arrays.asList(«sup1», «sup2», «sup3»))
Can’t instantiate an interface but there are few implementations:
List list = Arrays.asList("one", "two", "three");
//diamond operator List list = new ArrayList<>(); list.add("one"); list.add("two"); list.add("three");
List list = Stream.of("one", "two", "three").collect(Collectors.toList());
// creates immutable lists, so you can't modify such list List immutableList = List.of("one", "two", "three"); // if we want mutable list we can copy content of immutable list // to mutable one for instance via copy-constructor (which creates shallow copy) List mutableList = new ArrayList<>(List.of("one", "two", "three"));
Plus there are lots of other ways supplied by other libraries like Guava.
List list = Lists.newArrayList("one", "two", "three");
List is an Interface, you cannot instantiate an Interface, because interface is a convention, what methods should have your classes. In order to instantiate, you need some realizations(implementations) of that interface. Try the below code with very popular implementations of List interface:
List supplierNames = new ArrayList();
List supplierNames = new LinkedList();
You will need to use ArrayList or such.
import java.util.ArrayList; . List supplierNames = new ArrayList();
List is an interface, and you can not initialize an interface. Instantiate an implementing class instead.
List abc = new ArrayList(); List xyz = new LinkedList();
In most cases you want simple ArrayList — an implementation of List
JDK 7 and later you can use the diamond operator
Further informations are written here Oracle documentation — Collections
List is just an interface, a definition of some generic list. You need to provide an implementation of this list interface. Two most common are:
ArrayList — a list implemented over an array
List supplierNames = new ArrayList();
LinkedList — a list implemented like an interconnected chain of elements
List supplierNames = new LinkedList();
Depending on what kind of List you want to use, something like
List supplierNames = new ArrayList();
List is the interface, ArrayList is one implementation of the List interface. More implementations that may better suit your needs can be found by reading the JavaDocs of the List interface.
If you just want to create an immutable List with only one object in it, you can use this API:
List oneObjectList = Collections.singletonList("theOnlyObject”);
List is an Interface . You cant use List to initialize it.
List supplierNames = new ArrayList();
These are the some of List impelemented classes,
ArrayList, LinkedList, Vector
You could use any of this as per your requirement. These each classes have its own features.
Just in case, any one still lingering around this question. Because, i see one or two new users again asking the same question and everyone telling then , No you can’t do that, Dear Prudence, Apart from all the answers given here, I would like to provide additional Information — Yes you can actually do, List list = new List(); But at the cost of writing implementations of all the methods of Interfaces. The notion is not simply List list = new List(); but
List list = new List() < @Override public int size() < // TODO Auto-generated method stub return 0; >@Override public boolean isEmpty() < // TODO Auto-generated method stub return false; >@Override public boolean contains(Object o) < // TODO Auto-generated method stub return false; >
. and So on (Cant write all methods.)
This is an example of Anonymous class. Its correct when someone states , No you cant instantiate an interface, and that’s right. But you can never say , You CANT write List list = new List(); but, evidently you can do that and that’s a hard statement to make that You can’t do.