如何在java中将csv转换为multiMap?

时间:2016-07-07 18:59:23

标签: java csv multimap

我正在处理如下的一些数据,第一列是交易ID,第二列是模拟ID(重复很多),第三列是一些愚蠢的日期也很复制,第四列一个是交易的现值,大多只是0,但任何其他值都应该是非常独特的。

41120634|1554|20150203|-509057.56
40998001|1554|20150203|0
40960705|1554|20150203|0
40998049|1554|20150203|0
41038826|1554|20150203|0
41081136|1554|20150203|-7198152.23
41120653|1554|20150203|-319.436349
41081091|1554|20150203|-4.28520907E+009

我决定使用如下数据结构:

Map<Integer,Map<Integer,List<Map<Integer,Float>>>

然后我的csv将被保存为:

 {20150203:{1554:[{41120634:-509057.56,41120634:0,...}]}}

我的问题是如何有效地将这样的csv文件转换为我想要的数据结构?

2 个答案:

答案 0 :(得分:3)

易于实施将是Map<K1, Map<K2, Map<K3, V>>>之类的结构。此格式称为NestedMap,在本例中为NestedMap3,三个键和一个值。

使用继承实现起来非常容易,因为NestedMap3内部使用Map<K1, NestedMap2<K2, K3, V>>NestedMap2使用Map<K1, Map<K2, V>>

接下来,您应该考虑使用Map的位置以及使用多个容器的位置,例如Pair<A, B>Triple<A, B, C>等。
如果您的值经常更改,请使用像Pair这样的容器。如果您的值通常相同,请使用Map。基于此,您可以混合使用某些值,例如,如果后来的值经常更改,则Map<K, Triple<A, B, C>>可能会很好。

在您提供的方案中,第二个和第三个值通常是相同的。因此,我建议您使用NestedMap3<Integer, Integer, Integer, Float>

首先是设置数据结构的代码,我假设您的输入是String中存储的inputLines行:

NestedMap3<Integer, Integer, Integer, Float> map = new NestedMap3<>();
for (String line : inputLines) {
    String[] values = inputLines.split("|");
    map.put(toInt(values[0]), toInt(values[1]), toInt(values[2]), toFloat(values[3]));
}

当然,我们还需要实施toInttoFloat

public Integer toInt(final String value) {
    return Integer.parseInt(value);
}

public Float toFloat(final String value) {
    return Float.parseFloat(value);
}

最后实施NestedMap3NestedMap2

public class NestedMap3<K1, K2, K3, V> {

    private final Map<K1, NestedMap2<K2, K3, V>> mK1ToK2ToK3V = 
        new HashMap<K1, NestedMap2<K2, K3, V>>();

    public V put(K1 key1, K2 key2, K3 key3, V value) {
                NestedMap2<K2, K3, V> k2tok3toV = mK1ToK2ToK3V.get(key1);
            if (k2tok3toV == null) {
                k2tok3toV = new NestedMap2<>();
                mK1ToK2ToK3V.put(key1, k2tok3toV);
            }
        return k2tok3toV.put(key2, key3, value);
        }

        public V get(K1 key1, K2 key2, K3 key3) {
                final NestedMap2<K2, K3, V> k2tok3toV = mK1ToK2ToK3V.get(key1);
            if (k2tok3toV == null) {
                return null;
            } else {
                return k2tok3toV.get(key2, key3);
            }
    }

    public Map<K3, V> get(K1 key1, K2 key2) {
        final NestedMap2<K2, K3, V> k2toV = mK1ToK2ToK3V.get(key1);
            if (k2toV == null) {
                return null;
            } else {
                return k2toV.get(key2);
            }
    }

    public NestedMap2<K2, K3, V> get(K1 key1) {
        return mK1ToK2ToK3V.get(key1);
    }

    public Set<K1> keySet() {
        return mK1ToK2ToK3V.keySet();
    }

    public void clear() {
        mK1ToK2ToK3V.clear();
    }
}

public class NestedMap2<K1, K2, V> {

    private final Map<K1, Map<K2, V>> mK1ToK2ToV = new HashMap<K1, Map<K2, V>>();

    public V put(K1 key1, K2 key2, V value) {
    Map<K2, V> k2toV = mK1ToK2ToV.get(key1);
    if (k2toV == null) {
        k2toV = new HashMap<>();
        mK1ToK2ToV.put(key1, k2toV);
    }
    return k2toV.put(key2, value);
    }

    public V get(K1 key1, K2 key2) {
    final Map<K2, V> k2toV = mK1ToK2ToV.get(key1);
    if (k2toV == null) {
        return null;
    } else {
        return k2toV.get(key2);
    }
    }

    public Map<K2,V> get(K1 key1) {
    return mK1ToK2ToV.get(key1);
    }

    public Set<K1> keySet() {
    return mK1ToK2ToV.keySet();
    }

    public Iterable<Pair<K1,K2>> keys2() {
    return new Iterable<Pair<K1,K2>>() {

        @Override
        public Iterator<Pair<K1, K2>> iterator() {
            return new Iterator<Pair<K1,K2>>() {
                private Iterator<Entry<K1, Map<K2, V>>> mIterator1;
                private Entry<K1, Map<K2, V>> mIterator1Object;
                private Iterator<K2> mIterator2;

                {
                    mIterator1 = mK1ToK2ToV.entrySet().iterator();
                    if (mIterator1.hasNext()) {
                        mIterator1Object = mIterator1.next();
                        mIterator2 = mIterator1Object.getValue().keySet().iterator();
                    }
                }

                @Override
                public boolean hasNext() {
                    if (mIterator1Object == null) {
                        return false;
                    } else {
                        return mIterator2.hasNext();
                    }
                }

                @Override
                public Pair<K1, K2> next() {
                    if (mIterator1Object == null) {
                        throw new NoSuchElementException();
                    } else {
                        if (!mIterator2.hasNext()) {
                            if (!mIterator1.hasNext()) {
                                throw new NoSuchElementException();
                            } else {
                                mIterator1Object = mIterator1.next();
                                assert mIterator1Object.getValue().size() > 0 : "must contain at least one value";
                                mIterator2 = mIterator1Object.getValue().keySet().iterator();
                            }
                        }
                        return new Pair<K1, K2>(mIterator1Object.getKey(), mIterator2.next());
                    }
                }
            };
        }
    };

    }

    public Iterable<Triple<K1,K2,V>> entrySet() {
    final ArrayList<Triple<K1,K2,V>> result = new ArrayList<Triple<K1,K2,V>>();
    for (final Entry<K1, Map<K2, V>> entryOuter  : mK1ToK2ToV.entrySet()) {
                for (final Entry<K2, V> entryInner : entryOuter.getValue().entrySet()) {
                    result.add(new Triple<>(entryOuter.getKey(), entryInner.getKey(), entryInner.getValue()));
                }
    }
    return result;
    }

    public void addAll(NestedMap2<K1, K2, V> nestedMap) {
    for (final Triple<K1, K2, V> triple : nestedMap.entrySet()) {
        this.put(triple.getFirst(), triple.getSecond(), triple.getThird());
    }
    }

    public Map<K2, V> remove(K1 k1) {
    return mK1ToK2ToV.remove(k1);
    }

    public V remove(K1 k1, K2 k2) {
        final Map<K2, V> k2ToV = mK1ToK2ToV.get(k1);
    if (k2ToV == null) {
                return null;
    } else {
                return k2ToV.remove(k2);
    }
    }

    @Override
    public String toString() {
            return mK1ToK2ToV.toString();
    }

    public void clear() {
        mK1ToK2ToV.clear();
    }
}

答案 1 :(得分:1)

首先,在使用CommonApache的CSV方法后,创建一个适合您数据的对象。 https://commons.apache.org/proper/commons-csv/

下面我发现了我所做的代码的摘录,我认为它对你有用。

    $http({
            method:'POST',
            url:"http://www.example-server.com/{{param-1}}/{{param-2}}/example",
            data : {
              phone: $scope.phone,
              company: $scope.company,
              contract: '1',
              privacy: '1',
              email: $scope.email
            },
            headers: {
                'Content-Type': 'application/json'
            }
        })