How to get a list of specific fields values from objects stored in a list?

后端 未结 12 1054
长发绾君心
长发绾君心 2021-01-31 08:23

Say I have an list of objects with two fields field1 and field2, both of String type.

How do I get a list of all field1 values wit

相关标签:
12条回答
  • 2021-01-31 08:46

    Add the values of records in different lists and by using the one or two iterator simply print the seperate values.like follows:

    rs=st.executeQuery("select * from stu");
    List data=new ArrayList();
    List data1=new ArrayList();
    while(rs.next())
    {
       data.add(rs.getString(1));
       data1.add(rs.getString(2));
    }
    Iterator it=data.iterator();
    Iterator it1=data1.iterator();
    while(it1.hasNext())
    {
       System.out.println(" "+it.next()+"  "+it1.next());
    }
    
    0 讨论(0)
  • 2021-01-31 08:52

    Ancient question, but I came upon it while looking to see if I could improve on a similar solution.

    You can implement the List<String> interface without creating a fleshed-out ArrayList<String>, and thus not iterating over the parent object.

    final List<Entity> entities = getEntities()
    final List<String> field1 = new AbstractList() {
        public String get(int index) {
            return entities.get(index).getField1();
        }
        public int size() {
            return entities.size();
        }
    }
    

    That gives you a List without iterating over the parent object.

    Random access to the derived List<String> will be as expensive as random access to the underlying List<Entity>; if you're using an implementation of List<Entity> that does not provide quick random access, you might have to jump through a couple of hoops (i.e. implementing more methods of List<String>. But this should work for 99% of the cases where you need a light-weight adapter.

    0 讨论(0)
  • 2021-01-31 08:53

    Let the object be of the following class.

    public class Bike {
    
        String bikeModel;
        Int price;
    }
    

    And now there is list of bikes called bikeList of type List

    So now we want a list of bikemodels of all the bikes in the above list.

    bikeList.map{ Bike b -> b.bikeModel }.toCollection(arrayListOf())
    

    returns an array list of first field of all the bike objects in the bikeList

    0 讨论(0)
  • 2021-01-31 08:54

    try this:

    List<Entity> entities = getEntities();
    List<Integer> listIntegerEntities = Lambda.extract(entities, Lambda.on(Entity.class).getFielf1());
    

    the LambdaJ allows to access collections without explicit loops, so instead of have more lines of code to iterate the list yourself, you let LambdaJ do it.

    0 讨论(0)
  • 2021-01-31 09:01

    Neither java as a language nor JDK libraries do not do what you want yet. You can either use LambdaJ or wait for Java 8 that is expected to include lambda expressions.

    0 讨论(0)
  • 2021-01-31 09:03

    Fortunately, you can do this using Java 8 - Streams

    Assume you have an entity named YourEntity

    public class YourEntity {
    
        private String field1;
        private String field2;
    
        public YourEntity(String field1, String field2) {
            this.field1 = field1;
            this.field2 = field2;
        }
    
        public void setField1(String field1) {
            this.field1 = field1;
        }
    
        public void setField2(String field2) {
            this.field2 = field2;
        }
    
        public String getField1() {
            return field1;
        }
    
        public String getField2() {
            return field2;
        }
    }
    

    Declare you list of YourEntity using:

    List<YourEntity> entities = Arrays.asList(new YourEntity("text1", "text2"), new YourEntity("text3", "text4"));
    

    You can extract the list of field1 in one shot in this way:

    import java.util.stream.Collectors;
    
    List<String> field1List = entities.stream().map(YourEntity::getField1).collect(Collectors.toList());
    

    Or in this way

    import java.util.stream.Collectors;
    
    List<String> field1List = entities.stream().map(urEntity -> urEntity.getField1()).collect(Collectors.toList());
    

    You can print all the items also using java 8 :)

    field1List.forEach(System.out::println);
    

    Output

    text1
    text3
    
    0 讨论(0)
提交回复
热议问题