How to convert Map<String, String> to Map<MyClass, MyOtherClass>?

I have a simple Map<String, String> scanMap and I want to collect and covert it to a map of type of Map<CustomTag, CustomTagType>. CustomTag has 2 fields properties – key & value and I want to inject the original map key & value into it. the 2nd CustomTagType is an enum. I want to do… Read More How to convert Map<String, String> to Map<MyClass, MyOtherClass>?

How to Parse Strings from an ArrayList<>() into a Map<String, String>

I have a method that takes a parameter of filters that is an ArrayList<>() of strings that I need to split on ":" and put in a map. List<String> filtersQuery = new ArrayList<>(); filtersQuery.add("brand:nike,adidas"); filtersQuery.add("catagory:running"); And would like the output of the map to be: brand=nike,adidas catagory=running I am currently trying to use stream(). Im… Read More How to Parse Strings from an ArrayList<>() into a Map<String, String>

Streams and Collectors.toMap() – two approaches?

Among the below two code snippets – which one is preferable. Approach -1 public Map<String, List<MyObj>> approach1( Iterable<? extends String> keys) { return StreamSupport.stream(keys.spliterator(), false) .map(dt -> new AbstractMap.SimpleEntry<>(dt, load(dt))) .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, HashMap::new)); } Approach – 2 public Map<String, List<MyObj>> approach2( Iterable<? extends String> keys) { return StreamSupport.stream(keys.spliterator(), false) .collect(toMap(dt ->… Read More Streams and Collectors.toMap() – two approaches?

How to use Switch-statement inside a Stream

I need to get a balance sum from a list of type List<TransactionSumView>, which I’m receiving from the database. My TransactionSumView interface: //projection interface public interface TransactionSumView { String getType(); BigDecimal getAmount(); } Please see my implementation below: List<TransactionSumView> listSum = transactionsRepository.findAllSumByAcc1IdGroupByType(id); BigDecimal sum = BigDecimal.ZERO; // forEach loop for (TransactionSumView list : listSum) {… Read More How to use Switch-statement inside a Stream

How to create a `HashSet<Character>` contains '0'-'9' using stream api?

I’m using jdk 17. I want to use a concise syntax like var totalSet = IntStream.range(‘1’, ‘9’).map(i -> (char) i).boxed().collect(Collectors.toSet()); // type of totalSet is `Set<Integer>` to create a Set<Character> contains value from ‘0’ to ‘9’, (the type of totalSet is Set<Integer> with above) Other concise syntaxes to do this are appreciated. >Solution : You… Read More How to create a `HashSet<Character>` contains '0'-'9' using stream api?

stream.count() resulting in NPE

I am seeing some weird behaviour in Java stream API. Here’s my code: public static void main( String[] args ) { final List<String> list = Arrays.asList( "string1", "string2" ); final Map<String, List<String>> map = new HashMap<>(); map.put( "string1", Arrays.asList( "1" ) ); //map.put( "string2", Arrays.asList( "1" ) ); Stream<String> stream = list.stream().map( map::get ).flatMap( List::stream… Read More stream.count() resulting in NPE

How to get rid of NullPointerException when removing elements from array?

Given the following task. We have an Employee and a Company classes. Each instance of Employee class is stored in array Employee[] employees in the Company class. I need a method which removes an instance of Employee in the array Employee[] employees by id. I managed to write the following code: public class Employee {… Read More How to get rid of NullPointerException when removing elements from array?

Java group a map by value where value is a List

I have a Map<String,List<User>>map = new HashMap<>(); map.put("projectA",Arrays.asList(new User(1,"Bob"),new User(2,"John"),new User(3,"Mo"))); map.put("projectB",Arrays.asList(new User(2,"John"),new User(3,"Mo"))); map.put("projectC",Arrays.asList(new User(3,"Mo"))); Can use String instead of User. String is a project Name but the same users can relate to different projects. I would like to get sth like Map<User, List> where key will represent distinct user and a value as… Read More Java group a map by value where value is a List