简体   繁体   English

投射Iterator的最佳方法 <Object> 到一套 <String> , 例如

[英]Best way to cast Iterator<Object> to a Set<String>, for instance

Casting an Iterator<Object> to a Set<String> Iterator<Object>转换为Set<String>

What would be the cleanest/best practice way? 什么是最干净/最佳实践方式?

public Set<B> getBs(){
    Iterator<A> iterator = myFunc.iterator();
    Set<B> result = new HashSet<B>();
    while (iterator.hasNext()) {
        result.add((B) iterator.next();
    }
    return result;
}

But of course, it will fail if all the A s returned by the iterator are not B s. 但是,当然,如果迭代器返回的所有A不是B ,它将失败。

If you want to filter the iterator, then use instanceof: 如果要过滤迭代器,请使用instanceof:

public Set<B> getBs(){
    Iterator<A> iterator = myFunc.iterator();
    Set<B> result = new HashSet<B>();
    while (iterator.hasNext()) {
        A a = iterator.next();
        if (a instanceof B) {
            result.add((B) iterator.next();
        }
    }
    return result;
}

Using Guava, the above can be reduced to 使用番石榴,上述可以简化为

return Sets.newHashSet(Iterators.filter(myFunc.iterator(), B.class));

If we are talking about iterators and collections that need to use them, and you need the iterater to be generic enough so that it can be used by different collections . 如果我们谈论的是iteratorscollections需要使用它们,你需要的iterater是通用足够,以便它可以通过不同的使用collections
Just use if/else with instanceof keyword as follows: 只需将if/elseinstanceof关键字一起使用,如下所示:

while(iterator.hasNext()) {
  Object obj = iterator.next();
  if (obj instanceof A) {
    collection.add((A) o);  
  } else if (obj instanceof B) {
    collection.add((B) o);  
  } else if ...etc
}

I'm still not 100% sure what you want, but check this out and see: 我仍然不是100%地确定您想要什么,但请查看此内容并查看:

public static void main(String[] args) {
  final Iterator<?> it = Arrays.asList(new Object[] {"a", "b", "c"}).iterator();
  System.out.println(setFromIterator(it));
}

public static Set<String> setFromIterator(Iterator<?> it) {
  final Set<String> s = new HashSet<String>();
  while (it.hasNext()) s.add(it.next().toString());
  return s;
}

That's the only way, probably. 那可能是唯一的方法。

while(iterator.hasNext()) {
  Object o = iterator.next();
  if (o instanceof B) {
    collection.add((B) o);  
  }
}

org.apache.commons.collections.IteratorUtils can be used to do this. org.apache.commons.collections.IteratorUtils可用于执行此操作。

Here is an example to convert iterator to set; 这是将迭代器转换为set的示例;

Set<String> mySet = new HashSet<String>(IteratorUtils.toList(myIterator))

You can not cast the Iterator to Set directly. 不能将 Iterator直接转换为Set。 Iterator pattern provides the way to access the elements of an aggregate object sequentially without exposing its underlying presentation. 迭代器模式提供了一种顺序访问聚合对象元素的方法,而无需暴露其基础表示。 The possible solution is to travel each elements sequentially and add each element to the set 可能的解决方案是依次移动每个元素并将每个元素添加到集合中

while (iterator.hasNext()) {
    Object obj = iterator.next();
    set.add(obj.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM