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?










share|improve this question






















  • Edit title to specifically describe your particular question
    – Basil Bourque
    Nov 11 at 16:19















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?










share|improve this question






















  • Edit title to specifically describe your particular question
    – Basil Bourque
    Nov 11 at 16:19













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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.






share|improve this answer






























    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.






    share|improve this answer




























      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?






      share|improve this answer




























        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));





        share|improve this answer




























          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));





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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.






            share|improve this answer



























              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.






              share|improve this answer

























                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 28 at 5:24

























                answered Oct 27 at 9:32









                developer

                14.7k31940




                14.7k31940
























                    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.






                    share|improve this answer

























                      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.






                      share|improve this answer























                        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.






                        share|improve this answer












                        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.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Oct 27 at 8:02









                        Khalid Shah

                        1,127620




                        1,127620






















                            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?






                            share|improve this answer

























                              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?






                              share|improve this answer























                                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?






                                share|improve this answer












                                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?







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 27 at 8:04









                                Daniel Larsson

                                551419




                                551419






















                                    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));





                                    share|improve this answer

























                                      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));





                                      share|improve this answer























                                        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));





                                        share|improve this answer












                                        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));






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Oct 27 at 9:25









                                        Ayush Soni

                                        264




                                        264






















                                            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));





                                            share|improve this answer

























                                              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));





                                              share|improve this answer























                                                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));





                                                share|improve this answer












                                                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));






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 11 at 16:03









                                                Dragos Razvan

                                                33




                                                33






























                                                    draft saved

                                                    draft discarded




















































                                                    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.




                                                    draft saved


                                                    draft discarded














                                                    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





















































                                                    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







                                                    Popular posts from this blog

                                                    Coverage of Google Street View

                                                    Full-time equivalent

                                                    Surfing