Converting delimited string/text into a map object - java

I have been trying to figure out a solution for a while now, but I can't seem to get any suitable answer after thinking through/surfing the net for a solution. Hope the community can help me out!
I have some string and wishes to convert them into a nested map object, example below.
Fruits.Apple.Red
Fruits.Apple.Green
Fruits.Orange.Yellow
Fruits.Watermelon.Yellow
Fruits.Watermelon.Red
I would like to convert the above example into something like this.
{ Fruits:{
Apple:{
Red: null,
Green: null
},
Orange:{
Yellow: null
},
Watermelon:{enter code here
Yellow: null,
Red: null
}
}
}
Pardon me if you find this example to be a bad one. There is a reason why the value for the last child is null, I am trying to reproduce the problem I am facing.

A recursive function addToMap may be implemented to find the key before the dot . as a delimiter, creating a map if necessary using Map::computeIfAbsent, or adding the key with null value to the top-level map:
#SuppressWarnings({"rawtypes", "unchecked"})
static void addToMap(String s, Map<String, Map> upperLevel) {
int dotPos = s.indexOf(".");
if (dotPos < 0) {
upperLevel.put(s, null);
} else {
String key = s.substring(0, dotPos);
addToMap(s.substring(dotPos + 1), upperLevel.computeIfAbsent(key, k -> new HashMap<>()));
}
}
Then the test may look as follows:
String[] arr = {
"Fruits.Cherry",
"Fruits.Apple.Red",
"Fruits.Apple.Green",
"Fruits.Orange.Yellow",
"Fruits.Watermelon.Yellow",
"Fruits.Watermelon.Red",
};
Map<String, Map> result = new HashMap<>();
for (String t : arr) {
addToMap(t, result);
}
System.out.println(result);
Output:
{Fruits={Apple={Red=null, Green=null}, Cherry=null, Watermelon={Red=null, Yellow=null}, Orange={Yellow=null}}}
If an insertion order is important, LinkedHashMap should be created instead of HashMap or an overridden version with Supplier<Map> may be used:
Supplier<Map> mapSupplier = LinkedHashMap::new;
Map<String, Map> result = mapSupplier.get();
for (String t : arr) {
addToMap(t, result, mapSupplier);
}
System.out.println(result);
#SuppressWarnings({"rawtypes", "unchecked"})
static void addToMap(String s, Map<String, Map> upperLevel, Supplier<Map> mapSupplier) {
int dotPos = s.indexOf(".");
if (dotPos < 0) {
upperLevel.put(s, null);
} else {
String key = s.substring(0, dotPos);
addToMap(s.substring(dotPos + 1), upperLevel.computeIfAbsent(key, k -> mapSupplier.get()), mapSupplier);
}
}
Output (order changed):
{Fruits={Cherry=null, Apple={Red=null, Green=null}, Orange={Yellow=null}, Watermelon={Yellow=null, Red=null}}}

Related

Java stream groupingby nested string

I have a list of string, and I want to be able to group them hierarchically.
Example of the list:
var list = new String[]{"caso.id",
"caso.unidadeDoCaso.id",
"caso.etiqueta",
"caso.sigiloso",
"caso.idPecaSegredoJustica",
"caso.numeroAno",
"caso.numero",
"caso.competencia.id",
"caso.competencia.ativo",
"caso.competencia.nome",
"caso.responsavel.id",
"caso.responsavel.dadosPessoais.nome",
"caso.escrivao.id",
"caso.escrivao.dadosPessoais.nome"};
I want to group them in Maps.
Like:
caso->
id
sigiloso,
...
unidadeDoCaso->
id
competencia->
id
ativo
...
responsavel->
id
dadosPessoais->
nome
...
...
...
I was able to group just one level. I was wondering if there's a way to do it recursively.
In spite of my suggestion I decided to provide this. There are two recursive routines.
one to fill the map.
the other to print it.
String[] array = {
"caso.id","caso.unidadeDoCaso.id","caso.etiqueta",
"caso.sigiloso","caso.idPecaSegredoJustica","caso.numeroAno",
"caso.numero","caso.competencia.id","caso.competencia.ativo",
"caso.competencia.nome","caso.responsavel.id",
"caso.responsavel.dadosPessoais.nome","caso.escrivao.id",
"caso.escrivao.dadosPessoais.nome"
};
Create the map
Then iterated across the data, splitting on the dot.
then call fill with the map, just split nodes, and the starting node index.
Map<String, Object> map = new HashMap<>();
for (String s : array) {
String[] nodes = s.split("\\.");
fill(map, nodes, 0);
}
print(map, "");
prints
caso
unidadeDoCaso
id
etiqueta
idPecaSegredoJustica
escrivao
id
dadosPessoais
nome
sigiloso
numero
id
numeroAno
responsavel
id
dadosPessoais
nome
competencia
ativo
nome
id
The fill method continues until the supplied nodes are all processed.
first the map is checked to see if the node exists or not(equal to null)
if not present, a new map is constructed and added to the supplied map. Then the method is called to process the next node.
otherwise, the method is called to add the current node to the map after the one that exists and continue processing the nodes.
public static void fill(Map<String, Object> map, String[] nodes, int i) {
if (i >= nodes.length) {
return;
}
String node = nodes[i];
#SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>)(node);
if (obj == null) {
Map<String, Object> m = new HashMap<>();
map.put(node, m);
fill(m, nodes, i + 1);
} else {
fill( obj, nodes, i + 1);
}
}
This prints the map elements and indents each subsequent nested map level on a separate line.
#SuppressWarnings("unchecked")
public static void print(Map<String, Object> map, String indent) {
for (String key : map.keySet()) {
if (key != null) {
System.out.println(indent + key);
print((Map<String, Object>) map.get(key), indent + " ");
}
}
}
Here is how you could do this using a Map<String, Map> and mutable reduction using the collect method that takes a supplier, accumulator, and combiner. The API is not the most pleasant to use, as WJS pointed out.
It requires unchecked casts because you can't represent these recursive structures of unknown depth using generics.
class Scratch {
public static void main(String[] args) {
var list = new String[]{"caso.id",
"caso.unidadeDoCaso.id",
"caso.etiqueta",
"caso.sigiloso",
"caso.idPecaSegredoJustica",
"caso.numeroAno",
"caso.numero",
"caso.competencia.id",
"caso.competencia.ativo",
"caso.competencia.nome",
"caso.responsavel.id",
"caso.responsavel.dadosPessoais.nome",
"caso.escrivao.id",
"caso.escrivao.dadosPessoais.nome"};
Map<String, Map> result = Arrays.stream(list).collect(HashMap::new, Scratch::mapRecursively, HashMap::putAll);
System.out.println(result);
// {caso={unidadeDoCaso={id=null}, etiqueta=null, idPecaSegredoJustica=null, escrivao={id=null, dadosPessoais={nome=null}}, sigiloso=null, numero=null, id=null, numeroAno=null, responsavel={id=null, dadosPessoais={nome=null}}, competencia={ativo=null, nome=null, id=null}}}
System.out.println(result.get("caso").keySet());
// [unidadeDoCaso, etiqueta, idPecaSegredoJustica, escrivao, sigiloso, numero, id, numeroAno, responsavel, competencia]
}
#SuppressWarnings("unchecked")
private static void mapRecursively(HashMap<String, Map> map, String s) {
// first recursion: s = caso.competencia.id
// second recursion: s = id
int dot = s.indexOf('.');
// Base case 1
if (dot == -1) {
map.put(s, null);
return;
}
String key = s.substring(0, dot); // caso
String value = s.substring(dot + 1); // competencia.id
boolean isFirstTimeToComeAcrossWord = !map.containsKey(key);
if (isFirstTimeToComeAcrossWord) {
map.put(key, new HashMap<>());
}
// Base case 2
int dot2 = value.indexOf('.');
if (dot2 == -1) {
map.get(key).put(value, null);
return;
}
String newKey = value.substring(0, dot2); // competencia
String leftover = value.substring(dot2 + 1); // id
boolean isFirstTimeWeComeAcrossNestedWord = !map.get(key).containsKey(newKey);
// Recursive cases
if (isFirstTimeWeComeAcrossNestedWord) {
var newMap = new HashMap<String, Map>();
map.get(key).put(newKey, newMap);
mapRecursively(newMap, leftover);
} else {
mapRecursively((HashMap<String, Map>) map.get(key).get(newKey), leftover);
}
}
}

Java Map toString() where Map Value is Interface

I have many types of maps in my method. I've tried to make a mapToString() method to handle them, but some of my maps have interfaces as their key value.
public <T> String interfaceToString(Class<T> clazz, Object instance) {
log.info(clazz + "");
StringBuilder toString = new StringBuilder("[ " + clazz.getSimpleName() + " - ");
List<Method> methods = Arrays.asList(clazz.getDeclaredMethods());
methods.forEach(method -> {
try {
toString.append(method.getName() + ": " + method.invoke(instance) + ", ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
toString.replace(toString.length() - 2, toString.length() - 1, "");
toString.append("]");
return toString.toString();
}
public <T> String mapToString(Map<T, ?> map) {
StringBuilder toString = new StringBuilder("[ ");
for (T key : map.keySet()) {
Object value = map.get(key);
if (value.getClass().isInterface()) {
toString.append(interfaceToString(value.getClass(), value));
} else {
toString.append("[" + key + ", " + value + "] ");
}
}
toString.append(" ]");
return toString.toString();
}
It works for regular maps, but it just prints the object representation for map's whose key values are interfaces. I'm working with many interfaces, because I am using Spring JPA Projections to pull back partial parts of my database tables.
When I was debugging, when I logged value.getClass() in the mapToString, it's class was a Proxy. Which is why the isInterface() check failed when appending to the StringBuilder.
Is there a better way to do this?
EDIT
Here is my interface. It's a Spring JPA repository projection.
public interface OrderGetOpenReceievablesAndHistoryView {
Integer getNo();
String getBillchCode();
String getBilltlCode();
}
Here is my map:
Map<Integer, OrderGetOpenReceievablesAndHistoryView> carrierOrders = new HashMap<>();
I am assigning the map's key as an Integer and it's value as OrderGetOpenReceievablesAndHistoryView.
carrierOrders = orderRepository.findOpenRecievablesHistoryViewByNoIn(carrierOrderNos).stream().collect(Collectors.toMap(OrderGetOpenReceievablesAndHistoryView::getNo, Function.identity()));
I want to print the OrderGetOpenReceievablesAndHistoryView values as a normal String from the interfaceToString() method.
EDIT2
So, what I really want is a method to print out a map whose values are an interface. If I do this, it prints it out fine:
for (Integer key : carrierOrders.keySet()) {
OrderGetOpenReceievablesAndHistoryView value = carrierOrders.get(key);
log.info("{} : {}, {}, {}", key, value.getBillchCode(), value.getBilltlCode(), value.getNo());
}
Since I'm working with potentially hundreds of maps whose values could be anything, I don't want to have to write this loop everytime I want to print them.
Here is an output from the regular for each loop:
104432581 : TAXZ443237, HJMU499371, 104432581
Here is an output from the mapToString method():
[104406075, org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap#750bef00]
Every object is an instance of a concrete class, so value.getClass().isInterface() will never evaluate to true. When you want a special treatment for an interface type known at compile time, i.e. the map’s value type as declared in the generic type, you have to pass it as an argument to the method.
E.g.
public static <V> String mapToString(Map<?,V> map, Class<? super V> valueType) {
StringBuilder toString = new StringBuilder("[ ");
for(Map.Entry<?,V> entry: map.entrySet()) {
toString.append(entry.getKey()).append(": ");
if(!valueType.isInterface()) toString.append(entry.getValue());
else appendInterface(toString, valueType, entry.getValue());
toString.append(", ");
}
if(toString.length() > 2) toString.setLength(toString.length() - 2);
return toString.append(" ]").toString();
}
private static <V> void appendInterface(
StringBuilder toString, Class<V> valueType, V value) {
toString.append("[ ").append(valueType.getSimpleName()).append(" - ");
for(Method m: valueType.getDeclaredMethods()) {
if(!Modifier.isStatic(m.getModifiers()) && m.getParameterCount() == 0) {
Object o;
try {
o = m.invoke(value);
} catch(ReflectiveOperationException e) {
e.printStackTrace();
continue;
}
toString.append(m.getName()).append(": ").append(o).append(", ");
}
}
toString.replace(toString.length() - 2, toString.length(), "]");
}
Which you can invoke like
Map<Integer, OrderGetOpenReceievablesAndHistoryView> carrierOrders = new HashMap<>();
String s = mapToString(carrierOrders, OrderGetOpenReceievablesAndHistoryView.class);

Returning HashMap values and keys through an array method

I'm trying to return keys and values through an array method.
I have done this:
public ArrayList<String> translationList() {
for (String key : translations.keySet()) {
System.out.println(key + " = ");
}
return new ArrayList<String>(this.translations.values());
}
And in my Main.java
ArrayList<String> translations = dictionary.translationList();
for (String translation : translations) {
System.out.println(translation);
}
It returns
apina =
cembalo =
banaani =
monkey
harpsichord
banana
I'm not sure how to get them to print on the same line after the translation of the word. I know it's printing the for loop before returning the array but that is where my problem is and not sure how to solve it.
Since you are getting only values back there is no way to get key based on value. So hence not possible.
What you can do is return keys from method and iterate that in your method.
or simply change your method to
public ArrayList<String> translationList() {
List<String> returnList = new ArrayList<>(String);
for (Entry<Integer, String> entry : testMap.entrySet()) {
returnList.add(entry.getKey()+"="+entry.getValue());
}
return returnList;
}
and in your main method
ArrayList<String> translations = dictionary.translationList();
for (String translation : translations) {
System.out.println(translation);
}

List of string with occurrences count and sort

I'm developing a Java Application that reads a lot of strings data likes this:
1 cat (first read)
2 dog
3 fish
4 dog
5 fish
6 dog
7 dog
8 cat
9 horse
...(last read)
I need a way to keep all couple [string, occurrences] in order from last read to first read.
string occurrences
horse 1 (first print)
cat 2
dog 4
fish 2 (last print)
Actually i use two list:
1) List<string> input; where i add all data
In my example:
input.add("cat");
input.add("dog");
input.add("fish");
...
2)List<string> possibilities; where I insert the strings once in this way:
if(possibilities.contains("cat")){
possibilities.remove("cat");
}
possibilities.add("cat");
In this way I've got a sorted list where all possibilities.
I use it like that:
int occurrence;
for(String possible:possibilities){
occurrence = Collections.frequency(input, possible);
System.out.println(possible + " " + occurrence);
}
That trick works good but it's too slow(i've got millions of input)... any help?
(English isn’t my first language, so please excuse any mistakes.)
Use a Map<String, Integer>, as #radoslaw pointed, to keep the insertion sorting use LinkedHashMap and not a TreeMap as described here:
LinkedHashMap keeps the keys in the order they were inserted, while a TreeMap is kept sorted via a Comparator or the natural Comparable ordering of the elements.
Imagine you have all the strings in some array, call it listOfAllStrings, iterate over this array and use the string as key in your map, if it does not exists, put in the map, if it exists, sum 1 to actual result...
Map<String, Integer> results = new LinkedHashMap<String, Integer>();
for (String s : listOfAllStrings) {
if (results.get(s) != null) {
results.put(s, results.get(s) + 1);
} else {
results.put(s, 1);
}
}
Make use of a TreeMap, which will keep ordering on the keys as specified by the compare of your MyStringComparator class handling MyString class which wraps String adding insertion indexes, like this:
// this better be immutable
class MyString {
private MyString() {}
public static MyString valueOf(String s, Long l) { ... }
private String string;
private Long index;
public hashcode(){ return string.hashcode(); }
public boolean equals() { // return rely on string.equals() }
}
class MyStringComparator implements Comparator<MyString> {
public int compare(MyString s1, MyString s2) {
return -s1.getIndex().compareTo(s2.gtIndex());
}
}
Pass the comparator while constructing the map:
Map<MyString,Integer> map = new TreeMap<>(new MyStringComparator());
Then, while parsing your input, do
Long counter = 0;
while (...) {
MyString item = MyString.valueOf(readString, counter++);
if (map.contains(item)) {
map.put(map.get(item)+1);
} else {
map.put(item,1);
}
}
There will be a lot of instantiation because of the immutable class, and the comparator will not be consistent with equals, but it should work.
Disclaimer: this is untested code just to show what I'd do, I'll come back and recheck it when I get my hands on a compiler.
Here is the complete solution for your problem,
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataDto implements Comparable<DataDto>{
public int count = 0;
public String string;
public long lastSeenTime;
public DataDto(String string) {
this.string = string;
this.lastSeenTime = System.currentTimeMillis();
}
public boolean equals(Object object) {
if(object != null && object instanceof DataDto) {
DataDto temp = (DataDto) object;
if(temp.string != null && temp.string.equals(this.string)) {
return true;
}
}
return false;
}
public int hashcode() {
return string.hashCode();
}
public int compareTo(DataDto o) {
if(o != null) {
return o.lastSeenTime < this.lastSeenTime ? -1 : 1;
}
return 0;
}
public String toString() {
return this.string + " : " + this.count;
}
public static final void main(String[] args) {
String[] listOfAllStrings = {"horse", "cat", "dog", "fish", "cat", "fish", "dog", "cat", "horse", "fish"};
Map<String, DataDto> results = new HashMap<String, DataDto>();
for (String s : listOfAllStrings) {
DataDto dataDto = results.get(s);
if(dataDto != null) {
dataDto.count = dataDto.count + 1;
dataDto.lastSeenTime = System.nanoTime();
} else {
dataDto = new DataDto(s);
results.put(s, dataDto);
}
}
List<DataDto> finalResults = new ArrayList<DataDto>(results.values());
System.out.println(finalResults);
Collections.sort(finalResults);
System.out.println(finalResults);
}
}
Ans
[horse : 1, cat : 2, fish : 2, dog : 1]
[fish : 2, horse : 1, cat : 2, dog : 1]
I think this solution will be suitable for your requirement.
If you know that your data is not going to exceed your memory capacity when you read it all into memory, then the solution is simple - using a LinkedList or a and a LinkedHashMap.
For example, if you use a Linked list:
LinkedList<String> input = new LinkedList();
You then proceed to use input.add() as you did originally. But when the input list is full, you basically use Jordi Castilla's solution - but put the entries in the linked list in reverse order. To do that, you do:
Iterator<String> iter = list.descendingIterator();
LinkedHashMap<String,Integer> map = new LinkedHashMap<>();
while (iter.hasNext()) {
String s = iter.next();
if ( map.containsKey(s)) {
map.put( s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
Now, the only real difference between his solution and mine is that I'm using list.descendingIterator() which is a method in LinkedList that gives you the entries in backwards order, from "horse" to "cat".
The LinkedHashMap will keep the proper order - whatever was entered first will be printed first, and because we entered things in reverse order, then whatever was read last will be printed first. So if you print your map the result will be:
{horse=1, cat=2, dog=4, fish=2}
If you have a very long file, and you can't load the entire list of strings into memory, you had better keep just the map of frequencies. In this case, in order to keep the order of entry, we'll use an object such as this:
private static class Entry implements Comparable<Entry> {
private static long nextOrder = Long.MIN_VALUE;
private String str;
private int frequency = 1;
private long order = nextOrder++;
public Entry(String str) {
this.str = str;
}
public String getString() {
return str;
}
public int getFrequency() {
return frequency;
}
public void updateEntry() {
frequency++;
order = nextOrder++;
}
#Override
public int compareTo(Entry e) {
if ( order > e.order )
return -1;
if ( order < e.order )
return 1;
return 0;
}
#Override
public String toString() {
return String.format( "%s: %d", str, frequency );
}
}
The trick here is that every time you update the entry (add one to the frequency), it also updates the order. But the compareTo() method orders Entry objects from high order (updated/inserted later) to low order (updated/inserted earlier).
Now you can use a simple HashMap<String,Entry> to store the information as you read it (I'm assuming you are reading from some sort of scanner):
Map<String,Entry> m = new HashMap<>();
while ( scanner.hasNextLine() ) {
String str = scanner.nextLine();
Entry entry = m.get(str);
if ( entry == null ) {
entry = new Entry(str);
m.put(str, entry);
} else {
entry.updateEntry();
}
}
Scanner.close();
Now you can sort the values of the entries:
List<Entry> orderedList = new ArrayList<Entry>(m.values());
m = null;
Collections.sort(orderedList);
Running System.out.println(orderedList) will give you:
[horse: 1, cat: 2, dog: 4, fish: 2]
In principle, you could use a TreeMap whose keys contained the "order" stuff, rather than a plain HashMap like this followed by sorting, but I prefer not having either mutable keys in a map, nor changing the keys constantly. Here we are only changing the values as we fill the map, and each key is inserted into the map only once.
What you could do:
Reverse the order of the list using
Collections.reverse(input). This runs in linear time - O(n);
Create a Set from the input list. A Set garantees uniqueness.
To preserve insertion order, you'll need a LinkedHashSet;
Iterate over this set, just as you did above.
Code:
/* I don't know what logic you use to create the input list,
* so I'm using your input example. */
List<String> input = Arrays.asList("cat", "dog", "fish", "dog",
"fish", "dog", "dog", "cat", "horse");
/* by the way, this changes the input list!
* Copy it in case you need to preserve the original input. */
Collections.reverse(input);
Set<String> possibilities = new LinkedHashSet<String>(strings);
for (String s : possibilities) {
System.out.println(s + " " + Collections.frequency(strings, s));
}
Output:
horse 1
cat 2
dog 4
fish 2

Having a hashmap add 1 for each instance of a string in an array.

My hashmap class is as follows:
public HashMap<String, Integer> getWordCounts() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
String[] quoteOne = getWordArray();
for (String stuff : quoteOne) {
map.put(stuff, +1);
}
return map;
}
As it goes through quoteOne I want it to put each word from the array into the hashmap but for duplicates add 1 to the integer. e.g. "If you see this you are cool" would be put into the hashmap as
if 1
you 2
see 1
this 1
are 1
cool 1
But my code is outting it into the hashmap with you 1. What is wrong?
In your code, for every word you see, you put +1 (the int value of positive 1).
You need to update the value, not override it.
for (String stuff : quoteOne) {
Integer oldVal = map.get(stuff);
if (oldVal == null) {
oldVal = 0;
}
map.put(stuff, oldVal+1);
}
Your for loop will be
for (String stuff : quoteOne) {
if(map.get(stuff) != null){
int i = map.get(stuff);
map.put(stuff,i+1)
}else{
map.put(stuff, 1);
}
}
HashMap replaces values if same key is provided.
From Java doc of HashMap#put
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
Try something like this
for(String w : words) {
Integer i = wordCounts.get(w);
if(i == null) wordCounts.put(w, 1);
else wordCounts.put(w, i + 1);
}
for(String i: quoteOne)
map.put(i, (map.get(i)!=null)? map.get(i)+1:1);

Categories

Resources