How to return Object from a Collection?
up vote
-1
down vote
favorite
Given a Collection, how do I return a Object from that Collection knowing the index i?
I can't use the "get" method, so what should I use?
public static void remove(Collection coll, Predicate pred) {
for(int i=0; i<coll.size(); i++) {
if(pred.test(coll.get(i))) {
coll.remove(i);
}
}
}
Error: The method get(int) is undefined for the type Collection
java
|
show 1 more comment
up vote
-1
down vote
favorite
Given a Collection, how do I return a Object from that Collection knowing the index i?
I can't use the "get" method, so what should I use?
public static void remove(Collection coll, Predicate pred) {
for(int i=0; i<coll.size(); i++) {
if(pred.test(coll.get(i))) {
coll.remove(i);
}
}
}
Error: The method get(int) is undefined for the type Collection
java
1
Please show us your code and tell us why you cannot use theget()
method, please. First idea: for-each through the collection and return the object that matches your condition. By the way, what is the condition?
– deHaar
Nov 22 at 15:30
@deHaar added..
– user10691569
Nov 22 at 15:33
2
get(index)
is not defined inCollection
but only inList
. Either useList
or use an iterator to "go" to indexi
and return the element at that index. That's necessary because some collections don't support the notion of an index (e.g.Set
) but they all support iteration. Note that depending on the type of collection (e.g. when using a set) the iteration order might change so the element at "index"i
might not always be the same.
– Thomas
Nov 22 at 15:34
1
@deHaar well, you can do that but it's easier using an iterator: once the iterator "points" to the element you want to remove you just callremove()
on the iterator and then you're able to safely able to go on to the next elements.
– Thomas
Nov 22 at 15:37
@Thomas I used ListIterator<Integer> it = coll.listIterator(); but it says that the method listIterator is undefined for the type Collection
– user10691569
Nov 22 at 15:39
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Given a Collection, how do I return a Object from that Collection knowing the index i?
I can't use the "get" method, so what should I use?
public static void remove(Collection coll, Predicate pred) {
for(int i=0; i<coll.size(); i++) {
if(pred.test(coll.get(i))) {
coll.remove(i);
}
}
}
Error: The method get(int) is undefined for the type Collection
java
Given a Collection, how do I return a Object from that Collection knowing the index i?
I can't use the "get" method, so what should I use?
public static void remove(Collection coll, Predicate pred) {
for(int i=0; i<coll.size(); i++) {
if(pred.test(coll.get(i))) {
coll.remove(i);
}
}
}
Error: The method get(int) is undefined for the type Collection
java
java
edited Nov 22 at 15:32
asked Nov 22 at 15:29
user10691569
93
93
1
Please show us your code and tell us why you cannot use theget()
method, please. First idea: for-each through the collection and return the object that matches your condition. By the way, what is the condition?
– deHaar
Nov 22 at 15:30
@deHaar added..
– user10691569
Nov 22 at 15:33
2
get(index)
is not defined inCollection
but only inList
. Either useList
or use an iterator to "go" to indexi
and return the element at that index. That's necessary because some collections don't support the notion of an index (e.g.Set
) but they all support iteration. Note that depending on the type of collection (e.g. when using a set) the iteration order might change so the element at "index"i
might not always be the same.
– Thomas
Nov 22 at 15:34
1
@deHaar well, you can do that but it's easier using an iterator: once the iterator "points" to the element you want to remove you just callremove()
on the iterator and then you're able to safely able to go on to the next elements.
– Thomas
Nov 22 at 15:37
@Thomas I used ListIterator<Integer> it = coll.listIterator(); but it says that the method listIterator is undefined for the type Collection
– user10691569
Nov 22 at 15:39
|
show 1 more comment
1
Please show us your code and tell us why you cannot use theget()
method, please. First idea: for-each through the collection and return the object that matches your condition. By the way, what is the condition?
– deHaar
Nov 22 at 15:30
@deHaar added..
– user10691569
Nov 22 at 15:33
2
get(index)
is not defined inCollection
but only inList
. Either useList
or use an iterator to "go" to indexi
and return the element at that index. That's necessary because some collections don't support the notion of an index (e.g.Set
) but they all support iteration. Note that depending on the type of collection (e.g. when using a set) the iteration order might change so the element at "index"i
might not always be the same.
– Thomas
Nov 22 at 15:34
1
@deHaar well, you can do that but it's easier using an iterator: once the iterator "points" to the element you want to remove you just callremove()
on the iterator and then you're able to safely able to go on to the next elements.
– Thomas
Nov 22 at 15:37
@Thomas I used ListIterator<Integer> it = coll.listIterator(); but it says that the method listIterator is undefined for the type Collection
– user10691569
Nov 22 at 15:39
1
1
Please show us your code and tell us why you cannot use the
get()
method, please. First idea: for-each through the collection and return the object that matches your condition. By the way, what is the condition?– deHaar
Nov 22 at 15:30
Please show us your code and tell us why you cannot use the
get()
method, please. First idea: for-each through the collection and return the object that matches your condition. By the way, what is the condition?– deHaar
Nov 22 at 15:30
@deHaar added..
– user10691569
Nov 22 at 15:33
@deHaar added..
– user10691569
Nov 22 at 15:33
2
2
get(index)
is not defined in Collection
but only in List
. Either use List
or use an iterator to "go" to index i
and return the element at that index. That's necessary because some collections don't support the notion of an index (e.g. Set
) but they all support iteration. Note that depending on the type of collection (e.g. when using a set) the iteration order might change so the element at "index" i
might not always be the same.– Thomas
Nov 22 at 15:34
get(index)
is not defined in Collection
but only in List
. Either use List
or use an iterator to "go" to index i
and return the element at that index. That's necessary because some collections don't support the notion of an index (e.g. Set
) but they all support iteration. Note that depending on the type of collection (e.g. when using a set) the iteration order might change so the element at "index" i
might not always be the same.– Thomas
Nov 22 at 15:34
1
1
@deHaar well, you can do that but it's easier using an iterator: once the iterator "points" to the element you want to remove you just call
remove()
on the iterator and then you're able to safely able to go on to the next elements.– Thomas
Nov 22 at 15:37
@deHaar well, you can do that but it's easier using an iterator: once the iterator "points" to the element you want to remove you just call
remove()
on the iterator and then you're able to safely able to go on to the next elements.– Thomas
Nov 22 at 15:37
@Thomas I used ListIterator<Integer> it = coll.listIterator(); but it says that the method listIterator is undefined for the type Collection
– user10691569
Nov 22 at 15:39
@Thomas I used ListIterator<Integer> it = coll.listIterator(); but it says that the method listIterator is undefined for the type Collection
– user10691569
Nov 22 at 15:39
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
Java's Collection has no such method as get()
. Use an iterator :
Iterator<Object> iterator = coll.iterator();
while (iterator.hasNext()) {
// Your code here
}
I typed the iterator as<Object>
, and you should make your collection asCollection<Object>
unless you know you are going to get a defined type of collection.
– TheWildHealer
Nov 22 at 15:46
add a comment |
up vote
0
down vote
You can try this.
public static void remove(Collection coll, Predicate pred) {
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
if (pred.test(iterator.next())) {
iterator.remove();
}
}
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Java's Collection has no such method as get()
. Use an iterator :
Iterator<Object> iterator = coll.iterator();
while (iterator.hasNext()) {
// Your code here
}
I typed the iterator as<Object>
, and you should make your collection asCollection<Object>
unless you know you are going to get a defined type of collection.
– TheWildHealer
Nov 22 at 15:46
add a comment |
up vote
0
down vote
Java's Collection has no such method as get()
. Use an iterator :
Iterator<Object> iterator = coll.iterator();
while (iterator.hasNext()) {
// Your code here
}
I typed the iterator as<Object>
, and you should make your collection asCollection<Object>
unless you know you are going to get a defined type of collection.
– TheWildHealer
Nov 22 at 15:46
add a comment |
up vote
0
down vote
up vote
0
down vote
Java's Collection has no such method as get()
. Use an iterator :
Iterator<Object> iterator = coll.iterator();
while (iterator.hasNext()) {
// Your code here
}
Java's Collection has no such method as get()
. Use an iterator :
Iterator<Object> iterator = coll.iterator();
while (iterator.hasNext()) {
// Your code here
}
answered Nov 22 at 15:42
TheWildHealer
405216
405216
I typed the iterator as<Object>
, and you should make your collection asCollection<Object>
unless you know you are going to get a defined type of collection.
– TheWildHealer
Nov 22 at 15:46
add a comment |
I typed the iterator as<Object>
, and you should make your collection asCollection<Object>
unless you know you are going to get a defined type of collection.
– TheWildHealer
Nov 22 at 15:46
I typed the iterator as
<Object>
, and you should make your collection as Collection<Object>
unless you know you are going to get a defined type of collection.– TheWildHealer
Nov 22 at 15:46
I typed the iterator as
<Object>
, and you should make your collection as Collection<Object>
unless you know you are going to get a defined type of collection.– TheWildHealer
Nov 22 at 15:46
add a comment |
up vote
0
down vote
You can try this.
public static void remove(Collection coll, Predicate pred) {
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
if (pred.test(iterator.next())) {
iterator.remove();
}
}
}
add a comment |
up vote
0
down vote
You can try this.
public static void remove(Collection coll, Predicate pred) {
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
if (pred.test(iterator.next())) {
iterator.remove();
}
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
You can try this.
public static void remove(Collection coll, Predicate pred) {
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
if (pred.test(iterator.next())) {
iterator.remove();
}
}
}
You can try this.
public static void remove(Collection coll, Predicate pred) {
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
if (pred.test(iterator.next())) {
iterator.remove();
}
}
}
answered Nov 22 at 15:56
joemokenela
1465
1465
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%2f53434148%2fhow-to-return-object-from-a-collection%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
1
Please show us your code and tell us why you cannot use the
get()
method, please. First idea: for-each through the collection and return the object that matches your condition. By the way, what is the condition?– deHaar
Nov 22 at 15:30
@deHaar added..
– user10691569
Nov 22 at 15:33
2
get(index)
is not defined inCollection
but only inList
. Either useList
or use an iterator to "go" to indexi
and return the element at that index. That's necessary because some collections don't support the notion of an index (e.g.Set
) but they all support iteration. Note that depending on the type of collection (e.g. when using a set) the iteration order might change so the element at "index"i
might not always be the same.– Thomas
Nov 22 at 15:34
1
@deHaar well, you can do that but it's easier using an iterator: once the iterator "points" to the element you want to remove you just call
remove()
on the iterator and then you're able to safely able to go on to the next elements.– Thomas
Nov 22 at 15:37
@Thomas I used ListIterator<Integer> it = coll.listIterator(); but it says that the method listIterator is undefined for the type Collection
– user10691569
Nov 22 at 15:39