Split a map into submaps Java - java

I have a map with 10 000 keys and it looks like this:
Map<String, List<Integer>>. I want to make a list of Map<String, List<Integer>> which will contains 50 submaps X 200 elements.
This is the code:
Map<String, List<Integer>> map = new HashMap<>();
for (int i = 0; i < 10000; i++) {
map.put(String.valueOf(i),new ArrayList<>(Arrays.asList(new Random().nextInt(100),new Random().nextInt(200),new Random().nextInt(300)
,new Random().nextInt(400))));
}
List<Map<String, List<Integer>>> list = new ArrayList<>();
Map<String,List<Integer>> submap = new TreeMap<>();
List<Map<String, List<Integer>>> sublist = new ArrayList<>();
for (Map.Entry<String, List<Integer>> stringListEntry : map.entrySet()) {
submap.put(stringListEntry.getKey(),stringListEntry.getValue());
if (submap.size() == 200){
sublist.add(submap);
list.addAll(sublist);
sublist.clear();
submap.clear();
}
}
System.out.println(list);
but the list by the end is empty.
Why does this happen?

You need to generate a new sub-Map when the desired size is reached, instead of cleaning the same sub-Map and placing it into the resulting list multiple times.
List<NavigableMap<String, List<Integer>>> list = new ArrayList<>();
NavigableMap<String, List<Integer>> submap = new TreeMap<>();
final int limit = 200;
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
submap.put(entry.getKey(), entry.getValue());
if (submap.size() == limit) {
list.add(submap);
submap = new TreeMap<>();
}
}
Sidenote: it's more beneficial to use NavigableMap interface while working with a TreeMap because it offers many methods firtKey(), higherKey(), etc. that are not defined in the Map interface.

Related

How to filter Array of Map with multiple value using Guava

I have an Array containing Map. And I want to filter my array using some (multiple) key and value inside of the map object. For example, WHERE ID > 1 AND Name <> "cc" (key > 1, Name<>"cc").
How can i do that in Java?
I have imported the Guava libraries that has Collections2 to filter the array.
But, I didn't found any example that is filtering Map object inside the array.
here is some of my example codes:
List<Map<String, Object>> baseList = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("ID", 1);
map1.put("Name", "aa");
baseList.add(map1);
Map<String, Object> map2 = new HashMap<>();
map2.put("ID", 2);
map2.put("Name", "bb");
baseList.add(map2);
Map<String, Object> map3 = new HashMap<>();
map3.put("ID", 3);
map3.put("Name", "cc");
baseList.add(map3);
List<Map<String, Object>> filteredList = new ArrayList<>();
filteredList = Collections2.filter() ???
I want to filter with a kind of ID >= 1 AND NAME<>"cc" Which will resulting Array containing Map object like this: [{ID=1,Name="aa"}, {ID=2,Name="bb"}]
Anyone can help?
I have no idea what do you need Guava for. I'd do that in the following way:
List<Map<String, Object>> filteredList = baseList.stream()
.filter(map -> map.entrySet().stream()
.anyMatch(e -> e.getKey().equals(1L) && e.getValue().equals("cc")))
.collect(Collectors.toList());
Do you use Java 8? You can do:
List<Map<String, Object>> filteredList = maps.stream()
.filter(map -> (Integer) map.get("ID") >= 1 && !"cc".equals(map.get("Name")))
.collect(Collectors.toList());
to have new list with filtered maps.
If you want collection view using Guava goodies (or no Java 8), you should use Collections2.filter:
Collection<Map<String, Object>> filteredList = Collections2.filter(
maps, new Predicate<Map<String, Object>>() {
#Override
public boolean apply(#Nullable Map<String, Object> map) {
return (Integer) map.get("ID") >= 1 && !"cc".equals(map.get("Name"));
}
});
there's no Lists.filter, see IdeaGraveyard for explanation, hence only Collection interface is provided.
Do you really need listĀ of maps instead of Map<Integer, String> (or maybe Map<Integer, YourDomainObject>)? Then you could do:
final Map<Integer, String> map = ImmutableMap.of(
1, "aa",
2, "bb",
3, "cc");
final Map<Integer, String> filteredMap = Maps.filterEntries(map,
e -> e.getKey() >= 1 && !"cc".equals(e.getValue()));

Group the map values to avoid duplicate [duplicate]

This question already has answers here:
Reverse HashMap keys and values in Java
(12 answers)
Closed 5 years ago.
I have a Map(map1) whose key is another map(map2) and value is string.
The value of map1 has several duplicate, so I must group them and set as key in another map3 whose value has to be map2.
eg:
map1 { [[a,b],xyz], [[c,d],wrt] , [[e,f],xyz] , [[r,m],xyz] }
output should be :
map3 { [ xyz,[ [a,b],[e,f],[r,m] ] ] , [ wrt,[ [c,d] ]
can i obtain like this ?
try MultiValueMap from (commons-collections) library
Map map1 = new HashMap<String[], String>();
map1.put(new String[]{"a", "b"}, "xyz");
map1.put(new String[]{"c", "d"}, "wrt");
map1.put(new String[]{"e", "f"}, "xyz");
map1.put(new String[]{"c", "d"}, "xyz");
MultiValueMap map2 = new MultiValueMap();
for(String[] key: map1.keySet()) {
String value = map1.get(key);
map2.add(value, key);
}
// now you got map2 as you want
NO, Though you can declare it but while using it, it may allow same keys(human readable). Because Map do not override Object's equals method your key comparison will be on JVM's object level (it may be different for objects with same values in them).
You can use the stream API to solve it:
Map<String, List<Map<String, String>>> map3 = map.entrySet().stream()
.collect(Collectors.groupingBy(Entry::getValue,
Collectors.mapping(Entry::getKey,
Collectors.toList())));
Explanation:
The entries will be grouped by its values (groupingBy(Entry::getValue).
But the values should not be a list of entries so the downstream is necessary. This is the list of keys of the original map.
It is not clear from your post what do you mean by map2, but let's assume you would like to use each Map.Entry<String, String> entry from map2 for each key for map1.
The following code is Java 7, it's a bit verbose. It could be done shorter with Java 8 streams, I guess.
public class MapReverser {
private Map<Map.Entry<String, String>, String> map1 = new HashMap<>();
private Map<String, String> map2 = new LinkedHashMap<>();
private void prepareMaps() {
map2.put("a", "b");
map2.put("c", "d");
map2.put("e", "f");
map2.put("r", "m");
String[] valueArray = { "xyz", "wrt", "xyz", "xyz" };
int i = 0;
for (Map.Entry<String, String> entry : map2.entrySet()) {
map1.put(entry, valueArray[i]);
i++;
}
}
public Map<String, List<Map.Entry<String, String>>> reverse() {
Map<String, List<Map.Entry<String, String>>> reversedMap = new HashMap<>();
for (Map.Entry<Map.Entry<String, String>, String> entry : map1.entrySet()) {
List<Map.Entry<String, String>> reversedMapValue = reversedMap.get(entry.getValue());
if (reversedMapValue == null) {
reversedMapValue = new ArrayList<>();
}
reversedMapValue.add(entry.getKey());
reversedMap.put(entry.getValue(), reversedMapValue);
}
return reversedMap;
}
private void printResult(Map<String, List<Map.Entry<String, String>>> reversedMap) {
for (Map.Entry<String, List<Map.Entry<String, String>>> entry : reversedMap.entrySet()) {
System.out.println("Key: \n" + entry.getKey());
System.out.println("Values:");
List<Map.Entry<String, String>> valuesList = entry.getValue();
for (Map.Entry<String, String> value : valuesList) {
System.out.println(value );
}
System.out.println("\n");
}
}
public static void main(String[] args) {
MapReverser mapReverser = new MapReverser();
mapReverser.prepareMaps();
Map<String, List<Map.Entry<String, String>>> reversedMap = mapReverser.reverse();
mapReverser.printResult(reversedMap);
}
}

How do I iterate over a Map<Integer, Set>?

So if i have the following:
TreeMap<Integer, TreeSet<String>> returnMap = new TreeMap<Integer, TreeSet<String>>();
How do I create an iterator for the treeset inside of the map?
I tried the following:
Iterator<String> mapItr = returnMap.values();
If you don't care about order...
final TreeMap<Integer, TreeSet<String>> returnMap = new TreeMap<Integer, TreeSet<String>>();
final List<String> strings = new ArrayList();
for (final TreeSet<String> treeSet : returnMap.values()) {
strings.addAll(treeSet);
}
final Iterator<String> mapItr = strings.iterator();
In Java 8, using streams, it could be:
final Iterator<String> mapItr = returnMap
.values()
.stream()
.flatMap(Set::stream)
.collect(Collectors.toList())
.iterator();
If you do care about order, you will need to go through your TreeSet instances in whatever order you want.

Rearrange a map in random fashion

I've a SortedMap<String, String> containing key:ID & value:Name.
Now I want to rearrange the elements of this Map in random fashion & store them in separate map.
// Get a random entry from the SortedMap.
String[] keyArr = student.keySet().toArray();
String key = keyArr[new Random().nextInt(keyArr.length)];
// Use a separate List<String> to store which key has been selected, so that they are not re-selected
But above method does not sound very efficient.
Please suggest.
Thank You
You would need to copy the entrySet into a List and shuffle it. This would give you the elements in a random order.
Now you can push those elements to a new LinkedHashMap - to preserve the random order. Something like the following:
final Map<String, Object> m = new TreeMap<>();
m.put("A", 1);
m.put("B", 1);
m.put("C", 1);
m.put("D", 1);
m.put("E", 1);
m.put("F", 1);
m.put("G", 1);
final List<Map.Entry<String, Object>> e = new ArrayList<>(m.entrySet());
Collections.shuffle(e);
final Map<String, Object> r = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : e) {
r.put(entry.getKey(), entry.getValue());
}
I'm not sure that I got your question, but you should be able to do something like
Map<String, String> result = new LinkedHashMap<>();
List keys = new ArrayList(map.keySet());
Collections.shuffle(keys);
for (Object o : keys) {
// Access keys/values in a random order
result.put(o, map.get(o));
}

Java: Sorting a Map, Map<Long,Map<String, Data>> based on the inner map

I am new to java and is still in the learning phase.
I have a structure
Map<Long, Map<String, Data>> mapData
Data has 2 fields time and distance
and the Map has a time which is a Long field and map with Identifier and Data
the structure looks like this
{190001919 = {[1= [data1], 2=[data2], 3=[data3]},
190001920={[1=[data4], 2=[data5], 3=[data6]},
1900019121= {[1=[data7], 2=[data8], 3=[data9]}}
and then convert it into a map - Map<String,List<Data>> mpData with
idenifier as key and values as the values where there the identifier was the same.
like
{1= [data1,data4,data7], 2= [data2,data5,data8],3= [data3,data6,data9]}
Could some one please help me?
Update:
With the below code, I get
{1= [data7,data7,data7], 2= [data8,data8,data8],3= [data9,data9,data9]}
instead of
{1= [data1,data4,data7], 2= [data2,data5,data8],3= [data3,data6,data9]}
Code:
public static Map<Long, Map<String, Data>> listData;
public static Map<String, List<Data>> mapData;
public convertMapData(Map<Long, Map<String, Data>> array) {
listData = new HashMap();
listData = array;
mapData = new HashMap<>();
Iterator it = listData.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Long, Map<String, Data>> pairs = (Map.Entry) it
.next();
Long keyValue = pairs.getKey();
Map inter = pairs.getValue();
Iterator it2 = inter.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry<String, Data> pairs_2 = (Map.Entry) it2
.next();
String identifierK = pairs_2.getKey();
Data resultV = pairs_2.getValue();
if (!(mapData.containsKey(identifierK))) {
mapData.put(identifierK, new ArrayList<Data>());
}
mapData.get(identifierK).add(resultV);
}
}
}
Define Map<String,List<Data>> listData = new HashMap<String, List<Data>>();
Iterate over mapData's values (seems you don't use the keys of that map).
For every value of mapData, which again is a map, iterate over the entrySet, which gives you key (a String, lets call it K) and value (a Data object, lets call it V) of every entry.
Check if your listData already has a key like K (using containsKey()) and if not, add one, using listData.put(K, new ArrayList<Data>())
add V to the list that's stored for the key: listData.get(K).add(V)
That's all. As Rohit Jain commented, you'll not need a list around the listData map.
Try this:
public Map<String, List<Data>> convert(Map<Long, Map<String, Data>> array) {
Map<String, List<Data>> result = new HashMap<String, List<Data>>();
for (Map<String, Data> inter : array.values()) {
for (Map.Entry<String, Data> entry : inter.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
if (!result.containsKey(k)) {
result.put(k, new ArrayList<Data>());
}
result.get(k).add(v);
}
}
return result;
}

Categories