How to group array elements and get many occurring elements? [duplicate] - java

This question already has answers here:
How can I count occurrences with groupBy?
(6 answers)
Closed 2 years ago.
How to get an element of an array that occur multiple times?
ArrayList<String> arrBarCode = new ArrayList<String>();
arrBarCode.add(BarCode);

Use a Map<String, Integer> to remember the number of occurrences while iterating over the List:
ArrayList<String> arrBarCode = new ArrayList<String>();
arrBarCode.add("a");
arrBarCode.add("a");
arrBarCode.add("a");
arrBarCode.add("c");
Map<String, Integer> a = new HashMap<>();
arrBarCode.forEach(s -> a.put(s, a.computeIfAbsent(s, foo -> 0)+1));
System.out.println(a);
prints
{a=3, c=1}

Related

Create a Map of String and List from List of objects in java 8 [duplicate]

This question already has answers here:
Group a list of objects by an attribute
(13 answers)
Closed 2 years ago.
I have a List<Emp> where Emp is name, id, address.
value saved is like
("Name1, 11, Delhi"),
("Name1, 12, Chennai"),
("Name2, 13, Delhi"),
("Name3, 14, Delhi"),
("Name4, 15, Delhi")
I am trying to create a Map with Key as Name and value as list of Emp which has those name.
This is what I tried, and it's working fine, but I am looking for a better way.
Map<String, List<Emp>> nameMap = new HashMap<>();
empList.forEach(
empDto -> {
List<EmpDto> empDtoName = new ArrayList<>();
String name = empDto.getName();
if (nameMap .containsKey(name))
{
empDtoName = nameMap.get(name);
}
empDtoName.add(userRoleDto);
userRoleMap.put(usrNwId, empDtoName);
}
);
Use a Stream and collect its elements with a groupingBy collector:
Map<String, List<Emp>> nameMap =
empList.stream()
.collect(Collectors.groupingBy(Emp::getName));

How to typecast map.getValue() to ArrayList<Integer>()? [duplicate]

This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 3 years ago.
What I want do is, store the indexes in the arraylist of the current element in which it repeats, I have done that and now I want to check.
HashMap<Integer,ArrayList<Integer>> hmap = new HashMap<>();
for(int i=0;i<arr.length;i++)
{
if(!hmap.containsKey(arr[i]))
{
ArrayList<Integer> a1 = new ArrayList<>();
hmap.put(arr[i],a1);
}
else
hmap.get(arr[i]).add(i);
}
for(Map.Entry m:hmap.entrySet())
{
ArrayList<Integer> alist = {Here it is showing erorr.}m.getValue();
System.out.println(m.getKey()+" ");
for(int i=0;i<alist.size();i++)
System.out.print(alist.get(i)+" ");
}
You have to specify your generic types (same as in Map) in foreach loop.
for (Map.Entry<Integer, ArrayList<Integer>> m : hmap.entrySet())

List<String> to Map using Lambda expressions [duplicate]

This question already has answers here:
How can I initialize an ArrayList with all zeroes in Java?
(5 answers)
Closed 4 years ago.
I want to transform a list of String to a map, where the key of map is a simple increment.
For example:
List<String> result = new ArrayList<String>();
result.add("hello");
result.add("Java");
Pretend result:
Map<Integer, String> mapOfList;
map(1, "Hello");
map(2, "Java");
Try:
AtomicInteger atomic=new AtomicInteger(0);
mapOfList=result.stream().collect(atomic.incrementAndGet(), s -> s);
You need to iterate. Here's one-line using an int stream:
IntStream.range(0, fillMyList.size()).forEach(i -> fillMyList.set(i, ""));

How to add values from Map<T,List<L>> map to List<L>? [duplicate]

This question already has answers here:
Convert List of List into list in java
(5 answers)
Closed 5 years ago.
I have a multimap Map<T,List<L>> map and I need a list with all the values of the values from the map, namely List<L>. With map.values() I get a List<List<L>>, but thats not what I want.
Does someone know a clean solution without looping?
If you are using Java 8, you could collect all L values from all List<L>s in a single List<L> by Stream#flatMap:
final List<L> list = map
// get a Collection<List<L>>
.values()
// make a stream from the collection
.stream()
// turn each List<L> into a Stream<L> and merge these streams
.flatMap(List::stream)
// accumulate the result into a List
.collect(Collectors.toList());
Otherwise, a for-each approach with Collection#addAll can be applied:
final List<L> list = new ArrayList<>();
for (final List<L> values : map.values()) {
list.addAll(values);
}

how to add 2 list items to hashmap in java [duplicate]

This question already has answers here:
What is the best way to combine two lists into a map (Java)?
(18 answers)
Closed 6 years ago.
Hi I have 2 arraylist one list of userNames and another List of city. I want to add it to HashMap so that I can match user to city exactly.
List user : a1,b1,c1
List City : abc,null,def
I want to add it to HashMap map = new HashMap();
then if I read a1 key it should give value abc
a1=abc
b1=null
c1=def
.....
Map<String, String> map= new HashMap<String, String>();
Iterator<String> i1 = user.iterator();
Iterator<String> i2 = city.iterator();
while (i1.hasNext() && i2.hasNext()) {
map.put(i1.next(), i2.next());
}
For Java 8
IntStream.range(0, users.size())
.boxed()
.collect(toMap(users::get, cities::get)));
Something like this ?
Map<String, String> hash= new HashMap<String, String>();
for (int i=0;i<user.size();i++){
hash.put(user.get(i),city.get(i));
}
You can add it in for loop example
for(int i = 0; i< user.size(); i++){
map.put(user.get(i), city.get(i));
}

Categories