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

后端 未结 12 1055
长发绾君心
长发绾君心 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 09:03

    No, you can't. You have to traverse the whole list and get each "field1" value from each object.

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

    Without Iterating this is not possible .You can reduce iteration somehow but it is must.

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

    Depends...

    ... whether your questions refers to avoiding iterating over the collection either:

    • in terms of ease of implementation at call points
    • or in terms of the algorithmic complexity.

    Concretely, do you mean:

    • you don't want to type in an iterating construct yourself (simply use a convenience library),
    • or you actually want something that would return elements auto-magically in O(1) without needing to process them (and have perfect access)?

    See below for solutions and options.


    Using Convenience Libraries

    If it's the first one, then look at Google Guava, LambdaJ, FunctionalJava or other libraries that implement basic functional constructs and will allow you to do what you want in a few expressive calls. But keep in mind these do what is says on the tin: they will filter, collect or transform a collection, and will iterate through its elements to do this.

    For instance:

    • Google Guava:

      Set<String> strings = buildSetStrings();  
      Collection<String> filteredStrings =
          Collections2.filter(strings, Predicates.containsPattern("^J"));  
      
    • Functional Java:

      Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
      Array<Integer> b = a.filter(even);
      
    • LambdaJ:

      List<Integer> biggerThan3 = filter(greaterThan(3), asList(1, 2, 3, 4, 5));
      

    Perfect Access

    If it's the second one, this is not possible as-is, except if you architectured everything from the start so that your objects should be managed by a custom collection class that would indexing your objects based on their field values on insertion.

    It would keep them in buckets indexed by said value to be readily available for you to retrieve them as a list or set on demand.

    As mentioned in the comments below dounyy's answer, designing such a custom collection would probably have an impact on the API of the elements it would accept (most likely by defining a super interface to use for element types), or would require a fairly intricate implementation to resolve members dynamically (most likely by using reflection), if you ever wanted this collection to be generic.

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

    An object is a reference towards a memory address. Then, the fields of this objects are other references towards other memory addresses. Hence, a list of objects is a list of references. So, it's impossible for the list to direclty access the object fields (references given by the references). The short answer is no.

    Note: anyway you'll find an API that does what you want, it still loops in the inside.

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

    Yes!! Its possible. But you have to do some crooked work.

    1. Create a inner class with required variables. Eg: here am going to use 2 variables.( TNAME and TABTYPE).

       public class storeTables {
              private String tablename = "";
              private String tabletype = "";
              public storeTables(String a, String b)
              {
                  this.setTablename(a);
                  this.setTabletype(b);
              }
              public void setTablename(String tablename) {
                  this.tablename = tablename;
              }
              public String getTablename() {
                  return tablename;
              }
              public void setTabletype(String tabletype) {
                  this.tabletype = tabletype;
              }
              public String getTabletype() {
                  return tabletype;
              }}
      
    2. Create a List of the inner class created above and dont forget to encapsulate it.

      private List<storeTables> objlist= new ArrayList<storeTables>();
      
    3. Get the value stored to the list as a inner class object.

      String Query="SELECT * FROM TAB";
      while(rs.next())
      {
      String tname=rs.getString("TNAME");
      String tabtype=rs.getString("TABTYPE");
      getObjlist().add(new storeTables(tname,tabtype));
              }
      
    4. create a DATAMODEL and push the list to the datamodel

      private DataModel selectableItems= new ListDataModel(objlist);
      
    5. get the warpped data to another list.

      List<storeTables> items= (List<storeTables>)selectableItems.getWrappedData();
      
    6. Finally!!! printing the data.

      for(storeTables item:items){
      System.out.println(item.getTablename());
          }
      

    TADA !!!! it will print only the tablename and not the tabtype ;) Believe that there is nothing impossible in java Any doubts!!

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

    Maybe you could create an hashmap in this way:

    HashMap<String, ArrayList<Object>> map;
    

    The key would be the field. Doing this way, when you ask the HashMap to retrieve you the object corresponding to the field that you want, the map would return you an ArrayList cointaing all of the elements which has that field.

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