Java7中的java流

Java7中的java流,java,java-stream,Java,Java Stream,我的问题可能过于宽泛,答案可能是简单的“不”,但我不得不问 在Java 7中是否有(Java 8)*的等效实现 我熟悉(Java8)流,但我的项目要求是使用Java7 *不要与和混淆。谷歌的Guava库包含Java版本5到7的一些功能性习惯用法: 此外,您可能想查看这个库(直到几分钟前我执行Google搜索时才听说它:-)) 在官方API中,第 Java 7不再有公开更新。如果您是一个客户,您可能仍然会得到一些小的更新,但是这对于流API的后移植来说不是(或者不太可能) 稍微挖掘一下,你就可以

我的问题可能过于宽泛,答案可能是简单的“不”,但我不得不问

在Java 7中是否有(Java 8)*的等效实现

我熟悉(Java8)流,但我的项目要求是使用Java7


*不要与和混淆。

谷歌的Guava库包含Java版本5到7的一些功能性习惯用法:

此外,您可能想查看这个库(直到几分钟前我执行Google搜索时才听说它:-))


在官方API中,第

Java 7不再有公开更新。如果您是一个客户,您可能仍然会得到一些小的更新,但是这对于流API的后移植来说不是(或者不太可能)

稍微挖掘一下,你就可以看到。我从未测试过它,但很明显,它的目标是将流API向后移植到Java6/7,如果您想将其与lambda表达式相结合,还可以使用

可能很有趣。这与流API的目的并不完全相同,但是如果您的目标是筛选/映射/等等,那么列表/数组可能适合您的需要。用于:

final List b=List(1,2,3).map(add.f(-1));
listShow(intShow).println(b);//[0, 1, 2]

最后,您可以查看Scala的流API。由于Scala也在JVM上运行,您可以混合代码。也许这并不完全是您想要的,但如果需要,值得一试。

Java 6还有另一种选择+

接口:

interface TransformRule<In, Out> {
    Out extract(In obj);
}

interface FilterRule<T> {
    boolean apply(T obj);
}
接口转换规则{
外萃取物(在obj中);
}
接口过滤规则{
布尔应用(T-obj);
}
和用于集合/映射的类似Java8Stream的容器类:

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

class FPMapContainer<KeyType, ValueType> extends FPContainer<Map<KeyType, ValueType>, Map.Entry<KeyType, ValueType>, ValueType> {
    FPMapContainer(Map<KeyType, ValueType> container) {
        super(container);
    }

    @Override
    public <Out> FPMapContainer<KeyType, Out> map(TransformRule<Map.Entry<KeyType, ValueType>, Out> rule) {
        return new FPMapContainer<>(handleContainer(getMapMapRule(rule)));
    }

    @Override
    public FPMapContainer<KeyType, ValueType> filter(FilterRule<Map.Entry<KeyType, ValueType>> rule) {
        return new FPMapContainer<>(handleContainer(getMapFilterRule(rule)));
    }

    @Override
    public FPMapContainer<KeyType, ValueType> concat(Map<KeyType, ValueType> another) {
        HashMap newOne = new HashMap(container);
        newOne.putAll(another);
        return new FPMapContainer<>(newOne);
    }

    @Override
    public FPMapContainer<KeyType, ValueType> concat(FPContainer<Map<KeyType, ValueType>, Map.Entry<KeyType, ValueType>, ValueType> another) {
        return concat(another.get());
    }

    protected <Out> TransformRule<Map<KeyType, ValueType>, Map<KeyType, Out>> getMapMapRule(final TransformRule<Map.Entry<KeyType, ValueType>, Out> rule) {
        return new TransformRule<Map<KeyType, ValueType>, Map<KeyType, Out>>() {
            @Override
            public Map<KeyType, Out> extract(Map<KeyType, ValueType> obj) {
                Map<KeyType, Out> newOne = new HashMap<>();
                for (Map.Entry<KeyType, ValueType> entry : obj.entrySet()) {
                    newOne.put(entry.getKey(), rule.extract(entry));
                }
                return newOne;
            }
        };
    }

    protected TransformRule<Map<KeyType, ValueType>, Map<KeyType, ValueType>> getMapFilterRule(final FilterRule<Map.Entry<KeyType, ValueType>> rule) {
        return new TransformRule<Map<KeyType, ValueType>, Map<KeyType, ValueType>>() {
            @Override
            public Map<KeyType, ValueType> extract(Map<KeyType, ValueType> obj) {
                Map<KeyType, ValueType> newOne = new HashMap<>();
                for (Map.Entry<KeyType, ValueType> entry : obj.entrySet()) {
                    KeyType key = entry.getKey();
                    ValueType value = entry.getValue();
                    boolean isValid = rule.apply(entry);

                    if (isValid) {
                        newOne.put(key, value);
                    }
                }
                return newOne;
            }
        };
    }
}

class FPCollectionContainer<ValueType> extends FPContainer<Collection<ValueType>, ValueType, ValueType> {
    FPCollectionContainer(Collection<ValueType> container) {
        super(container);
    }

    @Override
    public <Out> FPCollectionContainer<Out> map(TransformRule<ValueType, Out> rule) {
        return new FPCollectionContainer<>(handleContainer(getCollectionMapRule(rule)));
    }

    @Override
    public FPCollectionContainer<ValueType> filter(FilterRule<ValueType> rule) {
        return new FPCollectionContainer<>(handleContainer(getCollectionFilterRule(rule)));
    }

    @Override
    public FPCollectionContainer<ValueType> concat(Collection<ValueType> another) {
        ArrayList<ValueType> newOne = new ArrayList<>(container);
        newOne.addAll(another);
        return new FPCollectionContainer<>(newOne);
    }

    @Override
    public FPCollectionContainer<ValueType> concat(FPContainer<Collection<ValueType>, ValueType, ValueType> another) {
        return concat(another.get());
    }

    protected <Out> TransformRule<Collection<ValueType>, Collection<Out>> getCollectionMapRule(final TransformRule<ValueType, Out> rule) {
        return new TransformRule<Collection<ValueType>, Collection<Out>>() {
            @Override
            public Collection<Out> extract(Collection<ValueType> obj) {
                Collection<Out> newOne = new ArrayList<>();
                for (ValueType entry : obj) {
                    newOne.add(rule.extract(entry));
                }
                return newOne;
            }
        };
    }

    protected TransformRule<Collection<ValueType>, Collection<ValueType>> getCollectionFilterRule(final FilterRule<ValueType> rule) {
        return new TransformRule<Collection<ValueType>, Collection<ValueType>>() {
            @Override
            public Collection<ValueType> extract(Collection<ValueType> obj) {
                Collection<ValueType> newOne = new ArrayList<>();
                for (ValueType entry : obj) {
                    if (rule.apply(entry)) {
                        newOne.add(entry);
                    }
                }
                return newOne;
            }
        };
    }
}

abstract class FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> {

    protected ContainerTypeWithValueType container;

    protected FPContainer(ContainerTypeWithValueType container) {
        this.container = container;
    }

    public static <KeyType, ValueType> FPMapContainer<KeyType, ValueType> from(Map<KeyType, ValueType> container) {
        return new FPMapContainer<>(container);
    }

    public static <ValueType> FPCollectionContainer<ValueType> from(Collection<ValueType> container) {
        return new FPCollectionContainer<>(container);
    }

    public abstract <Out> Object map(TransformRule<ContainerIterableItemType, Out> rule);

    public abstract FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> filter(FilterRule<ContainerIterableItemType> rule);

    public abstract FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> concat(FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> another);

    public abstract FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> concat(ContainerTypeWithValueType another);

    public <Out> Out reduce(TransformRule<ContainerTypeWithValueType, Out> rule) {
        return rule.extract(container);
    }

    public ContainerTypeWithValueType get() {
        return container;
    }

    protected <ContainerTargetType> ContainerTargetType handleContainer(TransformRule<ContainerTypeWithValueType, ContainerTargetType> collectionMapRule) {
        if (collectionMapRule != null) {
            return collectionMapRule.extract(container);
        }

        return (ContainerTargetType) container;
    }
}
import java.util.ArrayList;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.Map;
类FPMapContainer扩展了FPContainer{
FPMapContainer(映射容器){
超级集装箱;
}
@凌驾
公共FPMapContainer映射(TransformRule){
返回新的FPMapContainer(handleContainer(getmapprule(rule));
}
@凌驾
公共FPMapContainer筛选器(FilterRule规则){
返回新的FPMapContainer(handleContainer(getMapFilterRule(rule));
}
@凌驾
公共FPMapContainer concat(映射另一个){
HashMap newOne=新的HashMap(容器);
newOne.putAll(另一个);
返回新的FPMapContainer(newOne);
}
@凌驾
公共FPMapContainer concat(FPContainer另一个){
返回concat(另一个.get());
}
受保护的TransformRule GetMapRule(最终TransformRule规则){
返回新的TransformRule(){
@凌驾
公共地图摘录(地图obj){
Map newOne=newhashmap();
for(Map.Entry:obj.entrySet()){
put(entry.getKey(),rule.extract(entry));
}
返回newOne;
}
};
}
受保护的TransformRule getMapFilterRule(最终FilterRule规则){
返回新的TransformRule(){
@凌驾
公共地图摘录(地图obj){
Map newOne=newhashmap();
for(Map.Entry:obj.entrySet()){
KeyType key=entry.getKey();
ValueType value=entry.getValue();
布尔值isValid=rule.apply(条目);
如果(有效){
newOne.put(键、值);
}
}
返回newOne;
}
};
}
}
类FPCollectionContainer扩展了FPContainer{
FPCollectionContainer(收集容器){
超级集装箱;
}
@凌驾
公共FPCollectionContainer映射(TransformRule){
返回新的FPCollectionContainer(handleContainer(getCollectionMapRule(rule));
}
@凌驾
公共FPCollectionContainer筛选器(筛选器规则){
返回新的FPCollectionContainer(handleContainer(getCollectionFilterRule(规则));
}
@凌驾
公共FPCollectionContainer concat(另一个集合){
ArrayList newOne=新的ArrayList(容器);
newOne.addAll(另一个);
返回新的FPCollectionContainer(新的);
}
@凌驾
公共FPCollectionContainer concat(FPContainer另一个){
返回concat(另一个.get());
}
受保护的TransformRule getCollectionMapRule(最终TransformRule规则){
返回新的TransformRule(){
@凌驾
公共收藏摘录(收藏obj){
Collection newOne=newarraylist();
对于(ValueType条目:obj){
newOne.add(rule.extract(entry));
}
返回newOne;
}
};
}
受保护的TransformRule getCollectionFilterRule(最终FilterRule规则){
返回新的TransformRule(){
@凌驾
公共收藏摘录(收藏obj){
Collection newOne=newarraylist();
对于(ValueType条目:obj){
如果(规则适用(条目)){
newOne.add(条目);
}
}
返回newOne;
}
};
}
}
抽象类容器{
受保护的容器类型WithValueType容器;
受保护的FPContainer(ContainerTypeWithValueType容器){
this.container=容器;
}
公共静态FPMapContainer from(映射容器){
返回新的FPMapContainer(container);
}
公共静态FPCollectionContainer from(收集容器){
退回新的FPCollectionContainer(容器);
}
公共抽象对象映射(TransformRule);
公共容器过滤器(过滤器规则);
公共摘要FPContainer concat(FPContainer-other);
公共摘要FPContainer concat(ContainerTypeWithValueType另一个);
公共输出减少(转换规则){
复述
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

class FPMapContainer<KeyType, ValueType> extends FPContainer<Map<KeyType, ValueType>, Map.Entry<KeyType, ValueType>, ValueType> {
    FPMapContainer(Map<KeyType, ValueType> container) {
        super(container);
    }

    @Override
    public <Out> FPMapContainer<KeyType, Out> map(TransformRule<Map.Entry<KeyType, ValueType>, Out> rule) {
        return new FPMapContainer<>(handleContainer(getMapMapRule(rule)));
    }

    @Override
    public FPMapContainer<KeyType, ValueType> filter(FilterRule<Map.Entry<KeyType, ValueType>> rule) {
        return new FPMapContainer<>(handleContainer(getMapFilterRule(rule)));
    }

    @Override
    public FPMapContainer<KeyType, ValueType> concat(Map<KeyType, ValueType> another) {
        HashMap newOne = new HashMap(container);
        newOne.putAll(another);
        return new FPMapContainer<>(newOne);
    }

    @Override
    public FPMapContainer<KeyType, ValueType> concat(FPContainer<Map<KeyType, ValueType>, Map.Entry<KeyType, ValueType>, ValueType> another) {
        return concat(another.get());
    }

    protected <Out> TransformRule<Map<KeyType, ValueType>, Map<KeyType, Out>> getMapMapRule(final TransformRule<Map.Entry<KeyType, ValueType>, Out> rule) {
        return new TransformRule<Map<KeyType, ValueType>, Map<KeyType, Out>>() {
            @Override
            public Map<KeyType, Out> extract(Map<KeyType, ValueType> obj) {
                Map<KeyType, Out> newOne = new HashMap<>();
                for (Map.Entry<KeyType, ValueType> entry : obj.entrySet()) {
                    newOne.put(entry.getKey(), rule.extract(entry));
                }
                return newOne;
            }
        };
    }

    protected TransformRule<Map<KeyType, ValueType>, Map<KeyType, ValueType>> getMapFilterRule(final FilterRule<Map.Entry<KeyType, ValueType>> rule) {
        return new TransformRule<Map<KeyType, ValueType>, Map<KeyType, ValueType>>() {
            @Override
            public Map<KeyType, ValueType> extract(Map<KeyType, ValueType> obj) {
                Map<KeyType, ValueType> newOne = new HashMap<>();
                for (Map.Entry<KeyType, ValueType> entry : obj.entrySet()) {
                    KeyType key = entry.getKey();
                    ValueType value = entry.getValue();
                    boolean isValid = rule.apply(entry);

                    if (isValid) {
                        newOne.put(key, value);
                    }
                }
                return newOne;
            }
        };
    }
}

class FPCollectionContainer<ValueType> extends FPContainer<Collection<ValueType>, ValueType, ValueType> {
    FPCollectionContainer(Collection<ValueType> container) {
        super(container);
    }

    @Override
    public <Out> FPCollectionContainer<Out> map(TransformRule<ValueType, Out> rule) {
        return new FPCollectionContainer<>(handleContainer(getCollectionMapRule(rule)));
    }

    @Override
    public FPCollectionContainer<ValueType> filter(FilterRule<ValueType> rule) {
        return new FPCollectionContainer<>(handleContainer(getCollectionFilterRule(rule)));
    }

    @Override
    public FPCollectionContainer<ValueType> concat(Collection<ValueType> another) {
        ArrayList<ValueType> newOne = new ArrayList<>(container);
        newOne.addAll(another);
        return new FPCollectionContainer<>(newOne);
    }

    @Override
    public FPCollectionContainer<ValueType> concat(FPContainer<Collection<ValueType>, ValueType, ValueType> another) {
        return concat(another.get());
    }

    protected <Out> TransformRule<Collection<ValueType>, Collection<Out>> getCollectionMapRule(final TransformRule<ValueType, Out> rule) {
        return new TransformRule<Collection<ValueType>, Collection<Out>>() {
            @Override
            public Collection<Out> extract(Collection<ValueType> obj) {
                Collection<Out> newOne = new ArrayList<>();
                for (ValueType entry : obj) {
                    newOne.add(rule.extract(entry));
                }
                return newOne;
            }
        };
    }

    protected TransformRule<Collection<ValueType>, Collection<ValueType>> getCollectionFilterRule(final FilterRule<ValueType> rule) {
        return new TransformRule<Collection<ValueType>, Collection<ValueType>>() {
            @Override
            public Collection<ValueType> extract(Collection<ValueType> obj) {
                Collection<ValueType> newOne = new ArrayList<>();
                for (ValueType entry : obj) {
                    if (rule.apply(entry)) {
                        newOne.add(entry);
                    }
                }
                return newOne;
            }
        };
    }
}

abstract class FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> {

    protected ContainerTypeWithValueType container;

    protected FPContainer(ContainerTypeWithValueType container) {
        this.container = container;
    }

    public static <KeyType, ValueType> FPMapContainer<KeyType, ValueType> from(Map<KeyType, ValueType> container) {
        return new FPMapContainer<>(container);
    }

    public static <ValueType> FPCollectionContainer<ValueType> from(Collection<ValueType> container) {
        return new FPCollectionContainer<>(container);
    }

    public abstract <Out> Object map(TransformRule<ContainerIterableItemType, Out> rule);

    public abstract FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> filter(FilterRule<ContainerIterableItemType> rule);

    public abstract FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> concat(FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> another);

    public abstract FPContainer<ContainerTypeWithValueType, ContainerIterableItemType, ValueType> concat(ContainerTypeWithValueType another);

    public <Out> Out reduce(TransformRule<ContainerTypeWithValueType, Out> rule) {
        return rule.extract(container);
    }

    public ContainerTypeWithValueType get() {
        return container;
    }

    protected <ContainerTargetType> ContainerTargetType handleContainer(TransformRule<ContainerTypeWithValueType, ContainerTargetType> collectionMapRule) {
        if (collectionMapRule != null) {
            return collectionMapRule.extract(container);
        }

        return (ContainerTargetType) container;
    }
}
TransformRule<Integer, String> integerStringTransform = new TransformRule<Integer, String>() {
    @Override
    public String extract(Integer obj) {
        return "" + obj;
    }
};

TransformRule<Collection<String>, String> collectionStringTransform = new TransformRule<Collection<String>, String>() {
    @Override
    public String extract(Collection<String> obj) {
        String result = "";
        for (String item : obj) {
            result += item;
        }

        return result;
    }
};

FilterRule<Integer> ltFourFilter = new FilterRule<Integer>() {
    @Override
    public boolean apply(Integer obj) {
        return obj != null && obj < 4;
    }
};

// ==============================================

String reduced;

// Collection case:
// `reduced` would be "123"
reduced = FPContainer.from(Arrays.asList(1, 4))
        .concat(FPContainer.from(Arrays.asList(2)))
        .concat(Arrays.asList(3))
        .filter(ltFourFilter)
        .map(integerStringTransform).reduce(collectionStringTransform);

// Map case:
reduced = FPContainer.from(stringIntegerHashMap)
        .filter(new FilterRule<Map.Entry<String, Integer>>() {
            @Override
            public boolean apply(Map.Entry<String, Integer> obj) {
                return obj.getKey().charAt(0) < 'c' && obj.getValue() < 4;
            }
        })
        .map(new TransformRule<Map.Entry<String,Integer>, String>() {
            @Override
            public String extract(Map.Entry<String, Integer> obj) {
                return ""+obj.getValue();
            }
        }).reduce(new TransformRule<Map<String, String>, String>() {
            @Override
            public String extract(Map<String, String> obj) {
                String result = "";

                Map<String, String> objectStringMap = sortByValue(obj);
                for (Map.Entry<String, String> entry : objectStringMap.entrySet()) {
                    result += entry.getKey().toString() + entry.getValue();
                }

                return result;
            }
        });
public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
    Collections.sort(list, new Comparator<Object>() {
        @SuppressWarnings("unchecked")
        public int compare(Object o1, Object o2) {
            return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue());
        }
    });

    Map<K, V> result = new LinkedHashMap<>();
    for (Iterator<Map.Entry<K, V>> it = list.iterator(); it.hasNext();) {
        Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
        result.put(entry.getKey(), entry.getValue());
    }

    return result;
}