Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Interface Set's get() method>>>>

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
574
Chapter 7: Generics and Collections
.
.
.
.
.
.
.
Dog d = getDogList().get(0); // we KNOW a Dog is coming out
With pre-Java 5, non-generic code, the getDogList() method would be
public Set getDogList() {
Set dogs = new HashSet();
// code to add only Dogs... fingers crossed...
return dogs; // a Set of ANYTHING will work here
}
and the caller would need a cast:
Dog d = (Dog) getDogList().get(0);

************************************************

the above lines i have extracted from K n B's page 574.Here it is mentioned Set has get(int index) method.

based on this i made a code sample as--

import java.util.*;
class set1
{
public static void main(String[] arg)
{
Set s=new HashSet();
s.add("a");
s.add("b");
s.add("c");
System.out.println("***"+s.get(0));
}
}


and here compiler is not getting get() method..

Is there any thing wrong???

Thanks
Deepak
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,
You need a iterator to work upon a collection .I am sending you the code .please refer to that

import java.util.*;
class Test
{
public static void main(String[] arg)
{
Set s=new HashSet();
s.add("a");
s.add("b");
s.add("c");
Iterator itr = s.iterator();
while(itr.hasNext()) {
System.out.println("***"+itr.next();
}

}
}

I hope it answers your question

Thanks
Neha
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Neha

But that i know.this way all the objects will get printed.
If you go to page no. as i suggested in my post you will find get(int index) method is being used saying that it's Set's method.

While compiling code ,it's giving error.............
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Deepak Bahubal:

the above lines i have extracted from K n B's page 574.Here it is mentioned Set has get(int index) method.



I'm looking at that page now and they're using java.util.List, not java.util.Set.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public Set getDogList() {
Set dogs = new HashSet();
// code to add only Dogs... fingers crossed...
return dogs; // a Set of ANYTHING will work here
}
and the caller would need a cast:
Dog d = (Dog) getDogList().get(0); //line a


My question is related to 'line a' i mentioned above.getDogList() is returning a Set and we are calling get(int index) on Set.

As in book on post mid of page 574 within parenthesis it's mentioned "casting is done for what comes from >>>>>Set's get() method.<<<<<<<

****************************************

This i want to understand.

Thanks
Deepak
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic