Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing Number, String to more general type Object

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

changed to



because i have a common 1 method, which update's these Set's. it is basically iterator. part of code shown below.



here i can specify only 1 type as Object.



Any idea's ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ideas for what? Do you have a question?
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My real question is, would it be fine to represent either String or Number as 1 common type as Object as shown in my changed code.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:My real question is, would it be fine to represent either String or Number as 1 common type as Object as shown in my changed code.


You will be better off referring to the Sets using their proper types. In the method that needs the Iterator, pass them in as Set<? extends Object>:


So you can maintain type safety elsewhere.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is really great. thank you.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would prefer to use the enhanced for loop.




now my question is, how to ensure the null check before using the enhanced for loop?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean this:
Note that I removed "extends Object". This is because "?" and "? extends Object" are basically the same since Object is the root of all class hierarchies. Similarly, if you ever see "? super Object" you can replace it with "Object".
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to check for null, traditional method. thanks

this is informative.

Note that I removed "extends Object". This is because "?" and "? extends Object" are basically the same since Object is the root of all class hierarchies. Similarly, if you ever see "? super Object" you can replace it with "Object".



you mean i should use objects only or ? .



or

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i tried the code. the first option works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic