Collection and ArrayList in java
up vote
-2
down vote
favorite
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Can anyone explain why i can't use get() method which pre-defined in ArrayList class?
java
add a comment |
up vote
-2
down vote
favorite
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Can anyone explain why i can't use get() method which pre-defined in ArrayList class?
java
Edit title to specifically describe your particular question
– Basil Bourque
Nov 11 at 16:19
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Can anyone explain why i can't use get() method which pre-defined in ArrayList class?
java
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Can anyone explain why i can't use get() method which pre-defined in ArrayList class?
java
java
asked Oct 27 at 7:59
lim matthew
133
133
Edit title to specifically describe your particular question
– Basil Bourque
Nov 11 at 16:19
add a comment |
Edit title to specifically describe your particular question
– Basil Bourque
Nov 11 at 16:19
Edit title to specifically describe your particular question
– Basil Bourque
Nov 11 at 16:19
Edit title to specifically describe your particular question
– Basil Bourque
Nov 11 at 16:19
add a comment |
5 Answers
5
active
oldest
votes
up vote
3
down vote
accepted
Why I can't use get() method which pre-defined in ArrayList class?
In Java, reference variable type decides WHAT methods you can invoke on the object.
As your reference variable type is Collection to which you are assigning the ArrayList object, you can invoke only the methods declared by the Collection type which are specified (look here for API).
In simple words, get() is defined by ArrayList, but not by the Collection interface.
Now, if you want to invoke the get() method, you need to change the reference type from Collection to List as shown below:
List<String> a = new ArrayList<>();//No need to specify type for ArrayList
You can look at all of the methods declared by the List interface here and get(int i) is one of the methods.
add a comment |
up vote
1
down vote
Collection Interface don't have a get() method. List interface has get() method.
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Now it will works fine.
add a comment |
up vote
0
down vote
Try this,
Print first item in the collection :
System.out.printf(a.iterator().next());
For java8 :
System.out.println(a.stream().findFirst().orElse("not found"));
Java — How to get first item from Collection?
add a comment |
up vote
0
down vote
If you still need a Collection Interface you can type caste and use it
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(((ArrayList<String>)a).get(0));
add a comment |
up vote
0
down vote
The Collection Interface does not have a 'get(int index)' method.
You can either declare your a as a List, or just cast your Collection to a list:
List<String> a = new ArrayList<String>();
Also, < String > is redundant, so you can get rid of it.
List<String> a = new ArrayList<>();
If you want to cast your Collection to an ArrayList:
System.out.println(((ArrayList<String>)a).get(0));
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Why I can't use get() method which pre-defined in ArrayList class?
In Java, reference variable type decides WHAT methods you can invoke on the object.
As your reference variable type is Collection to which you are assigning the ArrayList object, you can invoke only the methods declared by the Collection type which are specified (look here for API).
In simple words, get() is defined by ArrayList, but not by the Collection interface.
Now, if you want to invoke the get() method, you need to change the reference type from Collection to List as shown below:
List<String> a = new ArrayList<>();//No need to specify type for ArrayList
You can look at all of the methods declared by the List interface here and get(int i) is one of the methods.
add a comment |
up vote
3
down vote
accepted
Why I can't use get() method which pre-defined in ArrayList class?
In Java, reference variable type decides WHAT methods you can invoke on the object.
As your reference variable type is Collection to which you are assigning the ArrayList object, you can invoke only the methods declared by the Collection type which are specified (look here for API).
In simple words, get() is defined by ArrayList, but not by the Collection interface.
Now, if you want to invoke the get() method, you need to change the reference type from Collection to List as shown below:
List<String> a = new ArrayList<>();//No need to specify type for ArrayList
You can look at all of the methods declared by the List interface here and get(int i) is one of the methods.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Why I can't use get() method which pre-defined in ArrayList class?
In Java, reference variable type decides WHAT methods you can invoke on the object.
As your reference variable type is Collection to which you are assigning the ArrayList object, you can invoke only the methods declared by the Collection type which are specified (look here for API).
In simple words, get() is defined by ArrayList, but not by the Collection interface.
Now, if you want to invoke the get() method, you need to change the reference type from Collection to List as shown below:
List<String> a = new ArrayList<>();//No need to specify type for ArrayList
You can look at all of the methods declared by the List interface here and get(int i) is one of the methods.
Why I can't use get() method which pre-defined in ArrayList class?
In Java, reference variable type decides WHAT methods you can invoke on the object.
As your reference variable type is Collection to which you are assigning the ArrayList object, you can invoke only the methods declared by the Collection type which are specified (look here for API).
In simple words, get() is defined by ArrayList, but not by the Collection interface.
Now, if you want to invoke the get() method, you need to change the reference type from Collection to List as shown below:
List<String> a = new ArrayList<>();//No need to specify type for ArrayList
You can look at all of the methods declared by the List interface here and get(int i) is one of the methods.
edited Oct 28 at 5:24
answered Oct 27 at 9:32
developer
14.7k31940
14.7k31940
add a comment |
add a comment |
up vote
1
down vote
Collection Interface don't have a get() method. List interface has get() method.
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Now it will works fine.
add a comment |
up vote
1
down vote
Collection Interface don't have a get() method. List interface has get() method.
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Now it will works fine.
add a comment |
up vote
1
down vote
up vote
1
down vote
Collection Interface don't have a get() method. List interface has get() method.
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Now it will works fine.
Collection Interface don't have a get() method. List interface has get() method.
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(a.get(0));
Now it will works fine.
answered Oct 27 at 8:02
Khalid Shah
1,127620
1,127620
add a comment |
add a comment |
up vote
0
down vote
Try this,
Print first item in the collection :
System.out.printf(a.iterator().next());
For java8 :
System.out.println(a.stream().findFirst().orElse("not found"));
Java — How to get first item from Collection?
add a comment |
up vote
0
down vote
Try this,
Print first item in the collection :
System.out.printf(a.iterator().next());
For java8 :
System.out.println(a.stream().findFirst().orElse("not found"));
Java — How to get first item from Collection?
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this,
Print first item in the collection :
System.out.printf(a.iterator().next());
For java8 :
System.out.println(a.stream().findFirst().orElse("not found"));
Java — How to get first item from Collection?
Try this,
Print first item in the collection :
System.out.printf(a.iterator().next());
For java8 :
System.out.println(a.stream().findFirst().orElse("not found"));
Java — How to get first item from Collection?
answered Oct 27 at 8:04
Daniel Larsson
551419
551419
add a comment |
add a comment |
up vote
0
down vote
If you still need a Collection Interface you can type caste and use it
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(((ArrayList<String>)a).get(0));
add a comment |
up vote
0
down vote
If you still need a Collection Interface you can type caste and use it
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(((ArrayList<String>)a).get(0));
add a comment |
up vote
0
down vote
up vote
0
down vote
If you still need a Collection Interface you can type caste and use it
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(((ArrayList<String>)a).get(0));
If you still need a Collection Interface you can type caste and use it
Collection<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
System.out.println(((ArrayList<String>)a).get(0));
answered Oct 27 at 9:25
Ayush Soni
264
264
add a comment |
add a comment |
up vote
0
down vote
The Collection Interface does not have a 'get(int index)' method.
You can either declare your a as a List, or just cast your Collection to a list:
List<String> a = new ArrayList<String>();
Also, < String > is redundant, so you can get rid of it.
List<String> a = new ArrayList<>();
If you want to cast your Collection to an ArrayList:
System.out.println(((ArrayList<String>)a).get(0));
add a comment |
up vote
0
down vote
The Collection Interface does not have a 'get(int index)' method.
You can either declare your a as a List, or just cast your Collection to a list:
List<String> a = new ArrayList<String>();
Also, < String > is redundant, so you can get rid of it.
List<String> a = new ArrayList<>();
If you want to cast your Collection to an ArrayList:
System.out.println(((ArrayList<String>)a).get(0));
add a comment |
up vote
0
down vote
up vote
0
down vote
The Collection Interface does not have a 'get(int index)' method.
You can either declare your a as a List, or just cast your Collection to a list:
List<String> a = new ArrayList<String>();
Also, < String > is redundant, so you can get rid of it.
List<String> a = new ArrayList<>();
If you want to cast your Collection to an ArrayList:
System.out.println(((ArrayList<String>)a).get(0));
The Collection Interface does not have a 'get(int index)' method.
You can either declare your a as a List, or just cast your Collection to a list:
List<String> a = new ArrayList<String>();
Also, < String > is redundant, so you can get rid of it.
List<String> a = new ArrayList<>();
If you want to cast your Collection to an ArrayList:
System.out.println(((ArrayList<String>)a).get(0));
answered Nov 11 at 16:03
Dragos Razvan
33
33
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53019881%2fcollection-and-arraylist-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Edit title to specifically describe your particular question
– Basil Bourque
Nov 11 at 16:19