简体   繁体   中英

Java Stream HOW TO .stream()

I am trying and googling and can not get it right. How to write this with stream?

private final ConcurrentMap<UUID, Expression> alleMap = new ConcurrentHashMap<>(1000);

 private String[] getAllDatabases()
  {
     Set<String> allDatabases = new HashSet<>();
     for (Entry<UUID, Expression> entry : alleMap.entrySet())
     {
        allDatabases.add(entry.getValue().getChapter().getDatabaseName());
     }
     List<String> allDatabasesList = new ArrayList<>(allDatabases);
     String[] result = new String[allDatabases.size()];
     for (int i=0; i < result.length; i++)
     {
        result[i] = allDatabasesList.get(i);
     }  
     return result;
  }


alleMap.values().stream().???

The reason I need an Array is that I am writing a Swing Application and need the String[] for a JOptionPane question.

Breaking this down for you:

Set<String> allDatabases = new HashSet<>();
for (Entry<UUID, Expression> entry : alleMap.entrySet()) {
    allDatabases.add(entry.getValue().getChapter().getDatabaseName());
}

This is getting all unique database names for all chapters for expressions that are values in your map.

 List<String> allDatabasesList = new ArrayList<>(allDatabases);
 String[] result = new String[allDatabases.size()];
 for (int i=0; i < result.length; i++) {
    result[i] = allDatabasesList.get(i);
 }  

As far as I can tell this is all just to convert the set of database names to an array. If so, you could have just used allDatabases.toArray(new String[allDatabases.size()] . Streams have a similar method (though they take an IntFunction to create an array rather than the new array):

This translates to:

alleMap.values().stream()
    .map(Expression::getChapter)
    .map(Chapter::getDatabaseName)
    .distinct()
    .toArray(String[]::new);

Note the distinct to replicate the uniqueness of sets.

Starts with not wanting arrays. They are low-level, inflexible constructs that don't play nice with generics and in general shouldn't be used unless you really know what you are doing and are willing to accept a little pain to accomodate it.

Streams are a tool. Not a universal improvement. If there's a pressing need to go with streams, by all means. But don't replace everything with streams because you think that's somehow 'better'. Just do what you're comfy with:)

If you must:

You aren't using the keys at all, just the values. So why bother with entrySet? values() will get what you want with far less effort.

List<String> alleMap.values().stream()
  .map(e -> e.getChapter().getDataBaseName())
  .collect(Collectors.toList());

This is pretty basic; maybe drop the specific case you're working on, and follow some tutorials first, then pick this back up, you'll get the hang of it in no time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM