Create a Java for loop to concatenate 2 sets of strings - java

I have
Map<String, String> prefixes
and
List<String> annotationProperties
And I am trying to get the string output of
prefix: annotationProperty
for each entry (they were entered in order).
Is there a for loop I could use to concatenate these? I need to return the entries as a List<String> to use in an XML output.
Thank you!

I assume annotationProperties is a key for prefix map. If that is the case then in Java 8 you can do this:
List<String> output = annotationProperties.stream()
.map(prop -> String.format("%s: %s", prefix.get(prop), prop))
.collect(Collectors.toList());
stream is called so you can use stream functions such as map and collect
map is called to transform the strings in annotationProperties into your desired output
and collect is called to convert the stream back into a list
If you want to use a for loop then you could also do it like this:
// The end size is known, so initialize the capacity.
List<String> output = new ArrayList<>(annotationProperties.size());
for (String prop : annotationProperties){
output.add(String.format("%s: %s", prefix.get(prop), prop));
}
Can a set a variable equal to .collect(Collectors.toList()); ?
We already have one! In both cases we made a variable output which is a list of Strings formatted as prefix: property. If you want to use this list then you can loop over it like this:
for (String mystring : output) {
// do xml creation with mystring
}
or like this:
for (int i = 0; i < output.size(); i++){
String mystring = output.get(i);
// do xml creation with mystring
}
or like this:
output.stream().forEach(mystring -> {
// do xml creation with mystring
});

Related

How to collect data from a stream in different lists based on a condition?

I have a stream of data as shown below and I wish to collect the data based on a condition.
Stream of data:
452857;0;L100;csO;20220411;20220411;EUR;000101435;+; ;F;1;EUR;000100000;+;
452857;0;L120;csO;20220411;20220411;EUR;000101435;+; ;F;1;EUR;000100000;+;
452857;0;L121;csO;20220411;20220411;EUR;000101435;+; ;F;1;EUR;000100000;+;
452857;0;L126;csO;20220411;20220411;EUR;000101435;+; ;F;1;EUR;000100000;+;
452857;0;L100;csO;20220411;20220411;EUR;000101435;+; ;F;1;EUR;000100000;+;
452857;0;L122;csO;20220411;20220411;EUR;000101435;+; ;F;1;EUR;000100000;+;
I wish to collect the data based on the index = 2 (L100,L121 ...) and store it in different lists of L120,L121,L122 etc using Java 8 streams. Any suggestions?
Note: splittedLine array below is my stream of data.
For instance: I have tried the following but I think there's a shorter way:
List<String> L100_ENTITY_NAMES = Arrays.asList("L100", "L120", "L121", "L122", "L126");
List<List<String>> list= L100_ENTITY_NAMES.stream()
.map(entity -> Arrays.stream(splittedLine)
.filter(line -> {
String[] values = line.split(String.valueOf(DELIMITER));
if(values.length > 0){
return entity.equals(values[2]);
}
else{
return false;
}
}).collect(Collectors.toList())).collect(Collectors.toList());
I'd rather change the order and also collect the data into a Map<String, List<String>> where the key would be the entity name.
Assuming splittedLine is the array of lines, I'd probably do something like this:
Set<String> L100_ENTITY_NAMES = Set.of("L100", ...);
String delimiter = String.valueOf(DELIMITER);
Map<String, List<String>> result =
Arrays.stream(splittedLine)
.map(line -> {
String[] values = line.split(delimiter );
if( values.length < 3) {
return null;
}
return new AbstractMap.SimpleEntry<>(values[2], line);
})
.filter(Objects::nonNull)
.filter(tempLine -> L100_ENTITY_NAMES.contains(tempLine.getEntityName()))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList());
Note that this isn't necessarily shorter but has a couple of other advantages:
It's not O(n*m) but rather O(n * log(m)), so it should be faster for non-trivial stream sizes
You get an entity name for each list rather than having to rely on the indices in both lists
It's easier to understand because you use distinct steps:
split and map the line
filter null values, i.e. lines that aren't valid in the first place
filter lines that don't have any of the L100 entity names
collect the filtered lines by entity name so you can easily access the sub lists
I would convert the semicolon-delimited lines to objects as soon as possible, instead of keeping them around as a serialized bunch of data.
First, I would create a model modelling our data:
public record LBasedEntity(long id, int zero, String lcode, …) { }
Then, create a method to parse the line. This can be as well an external parsing library, for this looks like CSV with semicolon as delimiter.
private static LBasedEntity parse(String line) {
String[] parts = line.split(";");
if (parts.length < 3) {
return null;
}
long id = Long.parseLong(parts[0]);
int zero = Integer.parseInt(parts[1]);
String lcode = parts[2];
…
return new LBasedEntity(id, zero, lcode, …);
}
Then the mapping is trivial:
Map<String, List<LBasedEntity>> result = Arrays.stream(lines)
.map(line -> parse(line))
.filter(Objects::nonNull)
.filter(lBasedEntity -> L100_ENTITY_NAMES.contains(lBasedEntity.lcode()))
.collect(Collectors.groupingBy(LBasedEntity::lcode));
map(line -> parse(line)) parses the line into an LBasedEntity object (or whatever you call it);
filter(Objects::nonNull) filters out all null values produced by the parse method;
The next filter selects all entities of which the lcode property is contained in the L100_ENTITY_NAMES list (I would turn this into a Set, to speed things up);
Then a Map is with key-value pairs of L100_ENTITY_NAME → List<LBasedEntity>.
You're effectively asking for what languages like Scala provide on collections: groupBy. In Scala you could write:
splitLines.groupBy(_(2)) // Map[String, List[String]]
Of course, you want this in Java, and in my opinion, not using streams here makes sense due to Java's lack of a fold or groupBy function.
HashMap<String, ArrayList<String>> map = new HashMap<>();
for (String[] line : splitLines) {
if (line.length < 2) continue;
ArrayList<String> xs = map.getOrDefault(line[2], new ArrayList<>());
xs.addAll(Arrays.asList(line));
map.put(line[2], xs);
}
As you can see, it's very easy to understand, and actually shorter than the stream based solution.
I'm leveraging two key methods on a HashMap.
The first is getOrDefault; basically if the value associate with our key doesn't exist, we can provide a default. In our case, an empty ArrayList.
The second is put, which actually acts like a putOrReplace because it lets us override the previous value associated with the key.
I hope that was helpful. :)
you're asking for a shorter way to achieve the same, actually your code is good. I guess the only part that makes it look lengthy is the if/else check in the stream.
if (values.length > 0) {
return entity.equals(values[2]);
} else {
return false;
}
I would suggest introduce two tiny private methods to improve the readability, like this:
List<List<String>> list = L100_ENTITY_NAMES.stream()
.map(entity -> getLinesByEntity(splittedLine, entity)).collect(Collectors.toList());
private List<String> getLinesByEntity(String[] splittedLine, String entity) {
return Arrays.stream(splittedLine).filter(line -> isLineMatched(entity, line)).collect(Collectors.toList());
}
private boolean isLineMatched(String entity, String line) {
String[] values = line.split(DELIMITER);
return values.length > 0 && entity.equals(values[2]);
}

Java Stream group by one attribute and collect max element by another attribute

I am wondering if there is already an implemented feature in streams (or Collectors) which first groups a stream by an attribute and then returns the first element in the list sorted by another attribute. E.g. the following code tries to group a stream of objects using the first attribute and then wants to collect that object which has the highest value of the second attribute.
class MyClass{
String att1;
String att2;
}
Now, I want to achieve something like this with Stream myClassStream -
Map<String,MyClass> myMap = myClassStream().collect(Collectors.groupingBy(MyClass::getAtt1)); //Now I want to do Sorting after grouping to collect only the element which has the highest value of attr2.
My code using simple for loop is:
Map<String, MyClass> postAnalyticsMap = new HashMap<>();
for (MyClass post : myClassList) {
if (post.get(post.getAtt1()) == null) {
post.put(post.getAtt1(), post);
} else {
MyClass existingClass = postAnalyticsMap.get(post.getAtt1());
if (existingPostAnalytics.getAtt2() < post.getAtt2()) {
postAnalyticsMap.put(post.getAtt1(), post);
}
}
}
Any help will be much appreciated.
Use the toMap with merge function to find the max element on second attribute
Map<String, MyClass> map = myClassList.stream()
.collect(Collectors.toMap(MyClass::getAtt1, Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(MyClass::getAtt2))));
You can do something like this:
myClassStream()
.collect(Collectors.groupingBy(MyClass::getAtt1,
Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(MyClass::getAtt2)),
myClass -> myClass.map(MyClass::getAtt2).orElse(""))));

How to remove \n in every Arraylist item

I extract some Data into a Arraylist. And in every Item there are several line breaks (\n) that I want to get rid of afterwards.
I´ve tried to do this:
public List<String> MemberIDList() {
// Getting the ArrayList
idList = listProjection.getIDListOfMembers();
for (int i = 0; idList.size() > i; i++) {
String item = idList.get(i);
item.replaceAll("\n", "");
}
return idList;
}
If I print that out on the console, it still contains all the line breaks:
The ID ...... (not null)
145
145
Thanks
EDIT: I also want to filter unnecessary whitespace. Is ther a better option than running the answers down below twice. There are spaces --------------------- this huge.
idList.replaceAll(item -> item.replaceAll("\\s{2,}", "").trim());
I got it :D
Change
item.replaceAll("\n", "");
to
idList.set(i,item.replaceAll("\n", ""));
item.replaceAll doesn't modify the state of the String referenced by item (which is impossible, since String is immutable). It returns a new String instead.
You can replace all your method to be like so :
public List<String> memberIDList() {
idList.replaceAll(item -> item.replaceAll("\n", ""));
return idList;
}
You can stream and map them:
idList.stream()
.map(str -> str.replaceAll("\n", ""))
.collect(Collectors.toList());
This will apply function str -> str.replaceAll("\n", "") to every element and collect them back to list. You can use this instead of your MemberIDList method.
P.S.: Method names are starting with lowercase in java.

Parsing String by pattern of substrings

I need to parse a formula and get all the variables that were used. The list of variables is available. For example, the formula looks like this:
String f = "(Min(trees, round(Apples1+Pears1,1)==1&&universe==big)*number";
I know that possible variables are:
String[] vars = {"trees","rivers","Apples1","Pears1","Apricots2","universe","galaxy","big","number"};
I need to get the following array:
String[] varsInF = {"trees", "Apples1","Pears1", "universe", "big","number"};
I believe that split method is good here but can’t figure the regexp required for this.
No need for any regex pattern - just check which item of the supported vars is contained in the given string:
List<String> varsInf = new ArrayList<>();
for(String var : vars)
if(f.contains(var))
varsInf.add(var);
Using Stream<> you can:
String[] varsInf = Arrays.stream(vars).filter(f::contains).toArray(String[]::new);
Assuming "variable" is represented by one alphanumeric character or sequential sequence of multiple such characters, you should split by not-alphanumeric characters, i. e. [^\w]+, then collect result by iteration or filter:
Set<String> varSet = new HashSet<>(Arrays.asList(vars));
List<String> result = new ArrayList<>();
for (String s : f.split("[^\\w]+")) {
if (varSet.contains(s)) {
result.add(s);
}
}

Replace strings populated in an ArrayList<String> with other values

I am currently working on a project where I need to check an arraylist for a certain string and if that condition is met, replace it with the new string.
I will only show the relevant code but basically what happened before is a long string is read in, split into groups of three, then those strings populate an array. I need to find and replace those values in the array, and then print them out. Here is the method that populates the arraylist:
private static ArrayList<String> splitText(String text)
{
ArrayList<String> DNAsplit = new ArrayList<String>();
for (int i = 0; i < text.length(); i += 3)
{
DNAsplit.add(text.substring(i, Math.min(i + 3, text.length())));
}
return DNAsplit;
}
How would I search this arraylist for multiple strings (Here's an example aminoAcids = aminoAcids.replaceAll ("TAT", "Y");) and then print the new values out.
Any help is greatly appreciated.
In Java 8
list.replaceAll(s-> s.replace("TAT", "Y"));
There is no such "replace all" method on a list. You need to apply the replacement element-wise; the only difference vs doing this on a single string is that you need to get the value out of the list, and set the new value back into the list:
ListIterator<String> it = DNAsplit.listIterator();
while (it.hasNext()) {
// Get from the list.
String current = it.next();
// Apply the transformation.
String newValue = current.replace("TAT", "Y");
// Set back into the list.
it.set(newValue);
}
And if you want to print the new values out:
System.out.println(DNAsplit);
Why dont you create a hashmap that has a key-value and use it during the load time to populate this list instead of revising it later ?
Map<String,String> dnaMap = new HashMap<String,String>() ;
dnaMap.push("X","XXX");
.
.
.
dnaMap.push("Z","ZZZ");
And use it like below :
//Use the hash map to lookup the temp key
temp= text.substring(i, Math.min(i + 3, text.length()));
DNAsplit.add(dnaMap.get(temp));

Categories

Resources