java-stream

Converting loop to Java 8 streams

两盒软妹~` 提交于 2019-12-08 14:06:28
问题 How can I convert the following looping code to simple Java 8 streams? List<String> headers = new ArrayList<>(); ... int column = 0; for(String text:headers){ Cell cell = header.createCell(column++); cell.setCellValue(text); } 回答1: Streams won't be needed. Use an AtomicInteger and Iterable#forEach: AtomicInteger column = new AtomicInteger(0); headers.forEach(text -> header.createCell(column.getAndIncrement()).setCellValue(text)); Whether that's more readable is up to you. 来源: https:/

Java-8: Stream How to convert on Map<K, List<D>> to Map<D, List<K>>

大兔子大兔子 提交于 2019-12-08 13:07:07
问题 I've just started looking at Java 8 and to try out lambdas, I have an use case for the above problem, which I am solving using the usual for loop which is very lengthy and hard to read My Existing code private static Map<DataType, List<OperatorType>> buildmap() { Map<DataType, List<OperatorType>> map = Maps.newHashMap(); for (OperatorType type : OperatorType.values()) { List<DataType> supportedTypes = type.getSupportedtypes(); supportedTypes.forEach(datatype -> { if (map.containsKey(datatype)

How do I convert this program into java 8 functional style using streams?

半城伤御伤魂 提交于 2019-12-08 12:23:15
问题 Problem I have written a program to find out every possibility of Uppercase and Lowercase of a character for a given string. An example would be, Input - "ab"/"Ab" etc. -- any one of those Output - ["ab","Ab","aB","AB"] Code Incorrect algorithm - please check below. public static ArrayList<String> permuteUCLC(String a) { String s=new String(a.toLowerCase()); ArrayList<String> arr = new ArrayList<>(); arr.add(a); int l = a.length(); for(int i=0;i<=l;i++) { for(int j=i+1;j<=l;j++) { arr.add(s

Effective way to get hex string from a byte array using lambdas and streams

拈花ヽ惹草 提交于 2019-12-08 08:42:43
问题 This is a follow-up question of How can I make an IntStream from a byte array? I created a method converting given byte array to a joined hex string. static String bytesToHex(final byte[] bytes) { return IntStream.rang(0, bytes.length * 2) .map(i -> (bytes[i / 2] >> ((i & 0x01) == 0 ? 4 : 0)) & 0x0F) .mapToObj(Integer::toHexString) .collect(joining()); } My question is that, not using any 3rd party libraries, is above code effective enough? Did I do anything wrong or unnecessary? 回答1: static

how to use java parallel stream instead of executorThreadsPool?

让人想犯罪 __ 提交于 2019-12-08 08:30:14
问题 I want to write a test that execute many parallel calls to my API. ExecutorService executor = Executors.newCachedThreadPool(); final int numOfUsers = 10; for (int i = 0; i < numOfUsers; i++) { executor.execute(() -> { final Device device1 = getFirstDevice(); final ResponseDto responseDto = devicesServiceLocal.acquireDevice(device1.uuid, 4738); if (responseDto.status == Status.SUCCESS) { successCount.incrementAndGet(); } }); } I know I can do it using executorThreadsPool, like this:

Filter a list of strings which contains one or more strings from another list with Java 8 streams

丶灬走出姿态 提交于 2019-12-08 07:14:36
问题 I want to use the strings imputed in a TextField to filter a list. I am using a KeyReleased Event on the TextField to filter the list on every key. The piece of code below filters the list when I type in a word, but when I press space and start typing another word the list gets empty. I am a bit new to streams. I don't know what I am doing wrong. private ObservableList<Products_Data> productList; @FXML private JFXTextField searchField; @FXML private TableView<Products_Data> productTable;

How to get all values from the inner maps of a map using a common key?

 ̄綄美尐妖づ 提交于 2019-12-08 06:52:37
问题 I have a map of maps: HashMap<String, Map<DistinctCode, String>> . I need to extract the String value from the inner maps just by using a DistinctCode . How can I do that in one line or statement? In other words, I need a method something like this: mapOfMap.find(distinctcode) Is it doable in one line or statement? 回答1: In Java 8 List<String> list = map.values().stream().map(m -> m.get(distinctcode)).filter(Objects::nonNull).collect(Collectors.toList()); 回答2: With Java 8 you can do Set<String

Java 8 stream group by min and max

好久不见. 提交于 2019-12-08 06:39:53
问题 Suppose you run an SQL query against an employees table: SELECT department, team, MIN(salary), MAX(salary) FROM employees GROUP BY department, team And in the java client you map the result set to a list of Aggregate instances by making a DAO call like below: List<Aggregate> deptTeamAggregates = employeeDao.getMinMaxSalariesByDeptAndTeam() And 'Aggregate' has getter methods for department, team, minSalary, maxSalary and there is a Pair<T, T> tuple What would be the clearest and possible the

How to convert type of stream?

橙三吉。 提交于 2019-12-08 06:19:55
问题 In addition to my question asked previously, that can be found here, How to combine list elements and find the price of largest combination Instead of using Integer price , I am using String price , List<Long> highest = details .stream() .map(d -> Stream.concat(Stream.of(d.getDetailId()), d.getStackableDetails().stream()).collect(Collectors.toList())) .collect(Collectors.toMap(s -> s.stream().map(Double.class::cast).reduce(0D, (left, right) -> left + Double.parseDouble(map.get(right).getPrice

Mapping Over an Ordered List in java 8

老子叫甜甜 提交于 2019-12-08 06:02:12
问题 In the streaming library included with Java 8, we're provided with the forEachOrdered API whose documentation is reproduced here void forEachOrdered(Consumer action) Performs an action for each element of this stream. This is a terminal operation. This operation processes the elements one at a time, in encounter order if one exists . Performing the action for one element happens-before performing the action for subsequent elements , but for any given element, the action may be performed in