How can I concatenate string elements from list in java8 based on their amount? - java-8

I have the following structure:
class MyClass {
String name;
String descr;
public String getName() {
return name;
}
}
Now I have a List of those objects and I want to print the name from the object above if the list contains any of those elements.
This is my code so far:
List<MyClass> list = getList();
if (list != null && list.size() > 0) {
System.out.println(list.get(0).getName());
} else {
System.out.println("list is empty");
}
This will work when the list contains only one element. Now I need to improve it and consider an example when there is more than one element - in that case I need to print all names, comma separated.
For example the output should be:
When there are 3 elements:
name1,name2,name3
when there's one element:
name1
and when there's none:
list is empty
what's the most efficient way of implementing it?

You could use Collectors.joining combined with Collectors.collectingAndThen:
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.joining;
...
String res =
list.stream()
.map(c -> c.name)
.collect(collectingAndThen(joining(","), s -> s.isEmpty() ? "list is empty" : s));
If you want to take in account the option when the list is null, you could do:
String res =
Optional.ofNullable(list).map(l -> l.stream()...).orElse("list is empty");
but to be honest I would use an if statement beforehand:
if(list == null || list.isEmpty()) {
return "list is empty";
} else {
return list.stream().map(c -> c.name).collect(joining(","));
}

You also can use the built-in class StringJoiner to simplify your code as following code:
StringJoiner joiner = new StringJoiner(",");
joiner.setEmptyValue("list is empty");
list.forEach(it -> joiner.add(it.getName()));
System.out.println(joiner);

Related

Sorting a list of pojos in Java

I would like to sort a list of Pojos based on the field (sortField) and order (sortOrder) parameters passed in. I have this working (for 6 fields) -
switch (sortField) {
case "field1" :
if (sortOrder == "asc") {
list.sort((lItem1, lItem2) -> lItem1.getField1().compareTo(lItem2.getField1()));
} else {
list.sort((lItem1, lItem2) -> lItem2.getField1().compareTo(lItem1.getField1()));
}
break;
case "field2" :
if (sortOrder == "asc") {
list.sort((lItem1, lItem2) -> lItem1.getField2().compareTo(lItem2.getField2()));
} else {
list.sort((lItem1, lItem2) -> lItem2.getField2().compareTo(lItem1.getField2()));
}
break;
//...
}
This works perfectly but feels a little bit clunky and I just wondered if anyone could point me to a (relatively simple!) tidier & more graceful way to do it ?
You can create a Map<String, Comparator<MyPojo>> and use it like:
Map<String, Comparator<MyPojo>> comparators = new HashMap<>();
comparators.put("field1", Comparator.comparing(MyPojo::getField1));
comparators.put("field2", Comparator.comparing(MyPojo::getField2));
//...
Comparator<MyPojo> comp = comparators.get(sortField);
list.sort("asc".equals(sortOrder) ? comp : comp.reversed());
I think, there are few things you can consider like reusability and duplicate codes.
Duplicate codes:
if (sortOrder == "asc")
{
list.sort((lItem1, lItem2) -> lItem1.getField2().compareTo(lItem2.getField2()));
}
else
{
list.sort((lItem1, lItem2) -> lItem2.getField2().compareTo(lItem1.getField2()));
}
To solve this, I would write either two separate methods to do sorting in asc or desc separately or write a single method to do both with parameter to define sorting order.
Solutions
Example1:
public List sortedList(List items, boolean isOrderAscending) { //do stuffs with items based on isOrderAscending and return sorted items}
Example2:
public List sortedListInAscendingOrder(List items) { //return ascending ordered list}
public List sortedListInDescendingOrder(List items) { //return descending ordered list}
I would even write a method to get sorting order from sortOrder.
public boolean isInAscending(String sortOrder) { //return true or false based on sortOrder}
Compiling whole:
List sortedItems = sortedList(items, isOrderAscending(sortedOrder));
Maybe after giving enough thoughts, whole field part can be altered in a more elegant way.

Conditional method call in the map method java 8

Persons = personDao.getFileInformation(filePath)
.skip(1)
.map(this::getPerson)
.filter(person -> person != null)
.collect(Collectors.toList());
getFileInformation(filePath) returns Stream<String>
after reading lines in a file.
I would like to replace the getPerson method with a getMale or getFemale method based on the value of an enum
public enum gender {
male,female
}
How can this be achieved used lambda expressions?
If you just want to filter by gender (assuming there is an accessor like Person.getGender), then you only need to add a filter:
List<Person> malePeople;
malePeople = personDao.getFileInformation(filePath)
.skip(1)
.map(this::getPerson)
.filter(Objects::nonNull)
.filter(p -> p.getGender() == gender.male) // or gender.female
.collect(Collectors.toList());
If you rather want to group your results, the following will help you:
Map<gender, List<Person>> peopleByGender;
peopleByGender = personDao.getFileInformation(filePath)
.skip(1)
.map(this::getPerson)
.filter(Objects::nonNull)
.collect(Collectors.groupingBy(Person::getGender));
now access all your female people with:
List<Person> femalePeople = peopleByGender.get(gender.female);
and the male ones with:
List<Person> malePeople = peopleByGender.get(gender.male);
If you just wanted to use a method to simplify the filter-predicate (p -> p.getGender() == gender.male), then you could use one of the following:
.filter(this::getMale) // or: YourClass::getMale for a static method
where this::getMale refers to the following method:
boolean getMale(Person p) {
return p.getGender() == gender.male;
}
or
.filter(getMale())
where getMale() refers to the following method:
Predicate<Person> getMale() {
return p -> p.getGender() == gender.male;
}

How can I write the following method in Java 8 streams?

How can I write the following method in Java 8 streams? I couldn't find a way to do it. This is my code:
public static List<ObjectB> getFilteredList(List<ObjectA> list, LocalTime startTime, LocalTime endTime, int quantity) {
List<ObjectA> objectAList = new LinkedList<>();
List<ObjectB> objectBList = new LinkedList<>();
for (ObjectA object : list) {
if (object.getDateTime().toLocalTime().isAfter(startTime) && object.getDateTime().toLocalTime().isBefore(endTime)) {
objectAList.add(object);
}
}
for (ObjectA objectA : objectAList) {
int total = 0;
for (ObjectA object : list) {
if (object.getDateTime().toLocalDate().equals(objectA.getDateTime().toLocalDate())) {
total += object.getQuantity();
}
}
if (total > quantity) {
objectBList.add(new ObjectB(objectA.getDateTime(), objectA.getDescription(), objectA.getQuantity(), true));
} else {
objectBList.add(new ObjectB(objectA.getDateTime(), objectA.getDescription(), objectA.getQuantity(), false));
}
}
return objectBList;}
I have a list of objects with two fields: date and quantity. I need to return a list with one object for each date, but with one more feild - boolean, which should be true if the total sum of all quantites per day is more than 16, and false if it's not.
Let's do this step-by-step.
for (ObjectA object : list) {: a for loop is usually replaced with stream(), so start with list.stream().
if (...) {: condition is usually replaced with filter(), so continue with .filter(object -> object.getDateTime()...)
objectAList.add(object);: adding the results to the container is usually replaced with collect(). You are using LinkedList(), but any other List would be fine here, so we will simply use collect(Collectors.toList()).
So here's first loop:
List<ObjectA> objectAList = list.stream()
.filter(object -> object.getDateTime().toLocalTime().isAfter(startTime) &&
object.getDateTime().toLocalTime().isBefore(endTime))
.collect(Collectors.toList());
Now let's look into the inner loop which calculates the total:
int total = 0;
for (ObjectA object : list) {
if (object.getDateTime().toLocalDate().equals(objectA.getDateTime().toLocalDate())) {
total += object.getQuantity();
}
}
It's also stream-filter-collect sequence, but here you want to collect the sum. So you may use IntStream here which already has the sum() method:
int total = list.stream()
.filter(object -> object.getDateTime().toLocalDate().equals(
objectA.getDateTime().toLocalDate())
.mapToInt(ObjectA::getQuantity).sum();
To make your code less crowded I would extract this to the separate method:
private static int getQuantityByDate(List<ObjectA> list, LocalDate date) {
return list.stream().filter(object -> object.getDateTime().toLocalDate().equals(date))
.mapToInt(ObjectA::getQuantity).sum();
}
Now the next if statement. It just changes the last boolean argument, so I would rewrite it (even without Stream API):
objectBList.add(new ObjectB(objectA.getDateTime(), objectA.getDescription(),
objectA.getQuantity(), total > quantity));
So now we see that the outer loop becomes stream-map-collect chain and could be rewritten this way:
List<ObjectB> objectBList = objectAList.stream()
.map(objectA ->
new ObjectB(objectA.getDateTime(), objectA.getDescription(), objectA.getQuantity(),
getQuantityByDate(list, objectA.getDateTime().toLocalDate()) > quantity))
.collect(Collectors.toList());
Now you can notice that collecting into objectAList is unnecessary as we just use it to create another stream. So we can merge both loops into single pipeline, resulting in the following final code:
private static int getQuantityByDate(List<ObjectA> list, LocalDate date) {
return list.stream().filter(object -> object.getDateTime().toLocalDate().equals(date))
.mapToInt(ObjectA::getQuantity).sum();
}
public static List<ObjectB> getFilteredList(
List<ObjectA> list, LocalTime startTime, LocalTime endTime, int quantity) {
return list.stream()
.filter(object -> object.getDateTime().toLocalTime().isAfter(startTime) &&
object.getDateTime().toLocalTime().isBefore(endTime))
.map(objectA -> new ObjectB(
objectA.getDateTime(), objectA.getDescription(), objectA.getQuantity(),
getQuantityByDate(list, objectA.getDateTime().toLocalDate()) > quantity))
.collect(Collectors.toList());
}

How to search on multiple strings entered in single text box in mvc3

i have a single textbox named Keywords.
User can enter multiple strings for search.
How this is possible in mvc3?
I am using nhibernate as ORM.
Can i create criteria for this?
Edited Scenario
I have partial view to search job based on following values:
Keywords(multiple strings), Industry(cascading dropdown with functional area )//working well ,FunctionalArea//working well
Loaction(multiple locations), Experience//working well
In Controller i am retrieving these values from form collection.
What datatype should i use for keywords and location (string or string[] )?
public ActionResult SearchResult(FormCollection formCollection)
{
IList<Jobs> JobsSearchResultList = new List<Jobs>();
//string[] keywords = null;
string location = null;
int? industry = 0;
int? functionaArea = 0;
int? experience = 0;
string keywords = null;
if (formCollection["txtKeyword"] != "")
{
keywords = formCollection["txtKeyword"];
}
//if (formCollection["txtKeyword"] != "")
//{
// keywordAry = formCollection["txtKeyword"].Split(' ');
// foreach (string keyword in keywordAry)
// {
// string value = keyword;
// }
//}
......retrieving other values from formcollection
....
//Now passing these values to Service method where i have criteria for job search
JobsSearchResultList = oEasyJobsService.GetJobsOnSearchExists(keywords,industry,functionaArea,location,experience);
return View(JobsSearchResultList);
}
In Services i have done like:
public IList<EASYJobs> GetJobsOnSearchExists(string keywords, int? industryId, int? functionalAreaId, string location, int? experience)
{
IList<JobLocation> locationlist = new List<JobLocation>();
IList<Jobs> JobsList = null;
var disjunction = Expression.Disjunction();
ICriteria query = session.CreateCriteria(typeof(Jobs), "EJobs");
if (keywords != null)
{
foreach (string keyword in keywords)
{
string pattern = String.Format("%{0}%", keyword);
disjunction
.Add(Restrictions.InsensitiveLike("Jobs.keywords", pattern,MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike("YJobs.PostTitle",pattern,MatchMode.Anywhere));
}
query.Add(disjunction)
.Add(Expression.Eq("EASYJobs.Industry.IndustryId", industryId))
.Add(Expression.Eq("Jobs.FunctionalArea.FunctionalAreaId", functionalAreaId))
.Add(Expression.Eq("Jobs.RequiredExperience", experience)));
}
else
{..
}
JobsList = criteria.List<Jobs>();
}
Problems i am facing are:
In controller if i use string[],then Split(',') does not split the string with specified separator.It passes string as it is to Service.
2.In services i am trying to replace string with %{0}% ,strings with spaces are replaced/concat() here with given delimeter.
But the problem here is It always return the whole job list means not giving the required output.
Pleas help ...
As long as you have a delimiter you can break the input into pieces on you should be able to create an or expression with the parts. You can use a disjunction to combine an arbitrary number of criteria using OR's.
var criteria = session.CreateCriteria<TestObject>();
Junction disjunction = Restrictions.Disjunction();
var input = "key words";
foreach (var keyword in input.Split(" "))
{
ICriterion criterion = Restrictions.Eq("PropertyName", keyword);
disjunction.Add(criterion);
}
criteria.Add(disjunction);
Multiple keywords with special characters or extra spaces are replaced with single space with Regex expressions.
And then keywords are separated with Split("").
Its working as required....
if (!string.IsNullOrEmpty(keywords))
{
keywords = keywords.Trim();
keywords = System.Text.RegularExpressions.Regex.Replace(keywords, #"[^0-9a-zA-Z\._\s]", " ");
keywords = System.Text.RegularExpressions.Regex.Replace(keywords, #"[\s]+", " ");
if (keywords.IndexOf(" ") > 0)
{
string[] arr = keywords.Split(" ".ToCharArray());
for (int i = 0; i < arr.Length; i++)
{
if (!string.IsNullOrEmpty(arr[i]))
{
criteria.Add(Restrictions.Disjunction()
.Add(Expression.Like("EASYJobs.keywords", arr[i], MatchMode.Anywhere)));
}
}
}
else
{
criteria.Add(Restrictions.Disjunction()
.Add(Expression.Like("EASYJobs.keywords", keywords, MatchMode.Anywhere)));
}
}

How to get out of repetitive if statements?

While looking though some code of the project I'm working on, I've come across a pretty hefty method which does
the following:
public string DataField(int id, string fieldName)
{
var data = _dataRepository.Find(id);
if (data != null)
{
if (data.A == null)
{
data.A = fieldName;
_dataRepository.InsertOrUpdate(data);
return "A";
}
if (data.B == null)
{
data.B = fieldName;
_dataRepository.InsertOrUpdate(data);
return "B";
}
// keep going data.C through data.Z doing the exact same code
}
}
Obviously having 26 if statements just to determine if a property is null and then to update that property and do a database call is
probably very naive in implementation. What would be a better way of doing this unit of work?
Thankfully C# is able to inspect and assign class members dynamically, so one option would be to create a map list and iterate over that.
public string DataField(int id, string fieldName)
{
var data = _dataRepository.Find(id);
List<string> props = new List<string>();
props.Add("A");
props.Add("B");
props.Add("C");
if (data != null)
{
Type t = typeof(data).GetType();
foreach (String entry in props) {
PropertyInfo pi = t.GetProperty(entry);
if (pi.GetValue(data) == null) {
pi.SetValue(data, fieldName);
_dataRepository.InsertOrUpdate(data);
return entry;
}
}
}
}
You could just loop through all the character from 'A' to 'Z'. It gets difficult because you want to access an attribute of your 'data' object with the corresponding name, but that should (as far as I know) be possible through the C# reflection functionality.
While you get rid of the consecutive if-statements this still won't make your code nice :P
there is a fancy linq solution for your problem using reflection:
but as it was said before: your datastructure is not very well thought through
public String DataField(int id, string fieldName)
{
var data = new { Z = "test", B="asd"};
Type p = data.GetType();
var value = (from System.Reflection.PropertyInfo fi
in p.GetProperties().OrderBy((fi) => fi.Name)
where fi.Name.Length == 1 && fi.GetValue(data, null) != null
select fi.Name).FirstOrDefault();
return value;
}
ta taaaaaaaaa
like that you get the property but the update is not yet done.
var data = _dataRepository.Find(id);
If possible, you should use another DataType without those 26 properties. That new DataType should have 1 property and the Find method should return an instance of that new DataType; then, you could get rid of the 26 if in a more natural way.
To return "A", "B" ... "Z", you could use this:
return (char)65; //In this example this si an "A"
And work with some transformation from data.Value to a number between 65 and 90 (A to Z).
Since you always set the lowest alphabet field first and return, you can use an additional field in your class that tracks the first available field. For example, this can be an integer lowest_alphabet_unset and you'd update it whenever you set data.{X}:
Init:
lowest_alphabet_unset = 0;
In DataField:
lowest_alphabet_unset ++;
switch (lowest_alphabet_unset) {
case 1:
/* A is free */
/* do something */
return 'A';
[...]
case 7:
/* A through F taken */
data.G = fieldName;
_dataRepository.InsertOrUpdate(data);
return 'G';
[...]
}
N.B. -- do not use, if data is object rather that structure.
what comes to my mind is that, if A-Z are all same type, then you could theoretically access memory directly to check for non null values.
start = &data;
for (i = 0; i < 26; i++){
if ((typeof_elem) *(start + sizeof(elem)*i) != null){
*(start + sizeof(elem)*i) = fieldName;
return (char) (65 + i);
}
}
not tested but to give an idea ;)

Resources