简体   繁体   English

使用collection -Stream将Java List转换为对象

[英]Convert java List to object using collection -Stream

From the method List> method() i get the output like this with 3 elements 从方法List> method()中,我得到带有3个元素的输出

[123456, 10, 03-JAN-16]
[956233, 20, 03-JAN-16]
[254656, 30, 03-JAN-16]
[455556, 40, 04-JAN-16]
[548566, 50, 03-JAN-16]
[215663, 60, 03-JAN-16]

I need to store the above result in a pojo class name 'ClassName' which has the following columns col1, col2 and col3, So I try to run the following code as 我需要将以上结果存储在pojo类名称“ ClassName”中,该类具有以下列col1,col2和col3,因此我尝试将以下代码运行为

public void method() {
    try {
        List<List<String>> list = testDAO.methodName();
        List<ClassName> className= new ArrayList<ClassName>();
        for (Iterator<List<String>> iterator = list.iterator(); iterator.hasNext();) {
            List<String> list2 = (List<String>) iterator.next();
            int i = 0;
            ClassName className= new ClassName ();
            for (Iterator<String> iterator2 = list2.iterator(); iterator2.hasNext();) {
                String string = (String) iterator2.next();
                /* System.out.println(string); */
                if (i == 0)
                    className.setCol1(string);
                else if (i == 1)
                    className.setCol2(Long.parseLong(string));
                else if (i == 2)
                    className.setCol3(string);
                i++;

            }
            odhs.add(className);

            System.out.println(className);
            // System.out.println(className.col2());
            // System.out.println(className.col3());
        }
        // System.out.println("Total size: "+ odhs.size());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But i got the output as 但是我得到的输出为

com.project.model.ClassName@61af1510
com.project.model.ClassName@37af1f93
com.project.model.ClassName@778d82e9
com.project.model.ClassName@408e96d9
com.project.model.ClassName@59901c4d
com.project.model.ClassName@168cd36b
com.project.model.ClassName@d8d9199
com.project.model.ClassName@3901f6af

Please provide a solution to save the datas in the POJO class 'ClassName' 请提供一种将数据保存在POJO类'ClassName'中的解决方案

您的类Odh必须重写toString方法。

Override Odh's toString method. 重写Odh的toString方法。

Assuming the names of your parameters are dist_id , pv and post_date and all are of String types 假设参数的名称为dist_idpvpost_date并且所有参数均为String类型

@Override
public String toString(){
   return getClass().getSimpleName() + "[dist_id=" + dist_id + ", pv=" + pv + ", post_date=" + post_date + "]";
}

That would allow you to print 那可以让你打印

Odh[dist_id=123456, pv=10, post_date=03-JAN-16] Odh [dist_id = 123456,pv = 10,post_date = 03-JAN-16]

On your Odh class you have to override toString method. Odh类上,您必须重写toString方法。

@Override
    public String toString ( )
    {
        return "Odh [firstAttribute=" + firstAttribute + ", secondAttribute=" + secondAttribute + "]";
    }

如何转换列表<object>至 Map<k, v> 使用 java stream<div id="text_translate"><p> 我想将List&lt;ObjectInList&gt;转换为Map&lt;K, V&gt;</p><pre> class ObjectInList { List&lt;Long&gt; listWithLong; Map&lt;String, Object&gt; dataMap; // there is 'id' key, and i want to use this id as key in map to be converted }</pre><p> 新的 map 格式如下</p><pre>String type; // this type is value of dataMap. List&lt;Long&gt; contents</pre><p> List&lt;Object&gt;中的每个 Object 都可以有重复的类型</p><p>例如</p><pre>///////// before converted //////////// [ { list: [1,2,3], dataMap: { type: "a", } }, { list: [4,5,6], dataMap: { type: "b", } }, { list: [7,8], dataMap: { type: "a", } }, ] ///////////// after converted ////////// { "a": [1,2,3,7,8], "b": [4,5,6] }</pre></div></k,></object> - How to convert LIst<Object> to Map<K, V> with using java stream

暂无
暂无

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

相关问题 转换流 <Object[]> 列出 <Object> 使用java流 - Convert Stream<Object[]> to List<Object> using java stream 如何使用Java流API将Collection <Pair <K,Collection <V >>转换为List <MyObject <K,V >>? - How to convert a Collection<Pair<K, Collection<V>> to a List<MyObject<K,V>> using Java stream API? 如何转换列表<object>至 Map<k, v> 使用 java stream<div id="text_translate"><p> 我想将List&lt;ObjectInList&gt;转换为Map&lt;K, V&gt;</p><pre> class ObjectInList { List&lt;Long&gt; listWithLong; Map&lt;String, Object&gt; dataMap; // there is 'id' key, and i want to use this id as key in map to be converted }</pre><p> 新的 map 格式如下</p><pre>String type; // this type is value of dataMap. List&lt;Long&gt; contents</pre><p> List&lt;Object&gt;中的每个 Object 都可以有重复的类型</p><p>例如</p><pre>///////// before converted //////////// [ { list: [1,2,3], dataMap: { type: "a", } }, { list: [4,5,6], dataMap: { type: "b", } }, { list: [7,8], dataMap: { type: "a", } }, ] ///////////// after converted ////////// { "a": [1,2,3,7,8], "b": [4,5,6] }</pre></div></k,></object> - How to convert LIst<Object> to Map<K, V> with using java stream 将 object 列表转换为 map java 8 ZF7B44CFAFD5C52223D7BZBZ - convert list of object to map java 8 stream 使用带索引的流将Java列表转换为映射 - Convert java list to map using stream with indexes 如何筛选清单 <String,Object> 用Java流收集吗? - How to Filter List<String,Object> Collection with java stream? 如何使用 java 8 中的流将集合/数组转换为 JSONArray - How to convert a collection/ array into JSONArray using stream in java 8 用Java 8流转换对象 - Convert object with java 8 stream JAVA 8:需要在流中执行列表迭代并转换另一个对象的列表 - JAVA 8 : need to perform List iteration in stream and convert list of another object 转换地图 <Integer, List<Object> &gt;到地图 <Integer, Map<String, Map<LocalDate, Integer> &gt;&gt;使用Java流API - Convert Map<Integer, List<Object>> to Map<Integer, Map<String, Map<LocalDate, Integer>>> using Java stream API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM