I have customized string array
for an example:
String[] myArr = {"Zen", "Maruthi","Swift", "Audi"};
I have a tool which will generate array like
String[] output = getCars();//returns {"Swift", "Audi", "Zen", "Maruthi"}
// it should return same as myArr...means {"Zen", "Maruthi","Swift", "Audi"}
I want to sort output arrays to myArr; i am sorting using for loop with comparison of myArr;
So is there any other method to sort customised array?plz help me
Edit:
No need to sort here. Just add similar string into the list as per element(s) sequence in myArr array.
List<String> items=new ArrayList<String>();
for(String item:myArr)
{
for(String outputItem:output)
{
if(item.equals(outputItem))
items.add(outputItem);
}
}
for(String s:items)
System.out.println(s);
Related
I have multiple numbers[] arrays in the below request
I want to merge them into one map. If a key exists in multiple lists, in that case, I should merge arrays.
For example, the Map should be like:
{60075=[100,200,500,600], 60076=[700,600]}
Can someone help me with a way to complete my requirement?
Request:
"details": [
{
"productSku": "60075",
"numbers": [
"100",
"200"
]
},
{
"productSku": "60075",
"numbers": [
"500",
"600"
]
},
{
"productSku": "60076",
"numbers": [
"700",
"600"
]
}
]
I tried the below code but it's not working:
Map<String, List<String>> map1 = new HashMap<String, List<String>>();
for (Details details: detailsList) {
List<String> numbers = (map1.get(details.getProductSku()));
map1.put(details.getProductSku(), (numbers== null) ? numbers
: numbers.addAll(diohShippingDetails.getSerialNumbers()));
}
Your conditional (numbers== null) ? numbers: numbers.addAll(diohShippingDetails.getSerialNumbers()) has two problems.
When numbers is null, which is the initial state as the map is empty, it uses number as result, which has been proven to be null, so the result would always be null.
addAll does not return a list at all, but a boolean. You have to separate the expression to produce the map’s value from the addAll operation.
You can use something like
List<String> numbers = map1.get(details.getProductSku());
if(numbers == null) {
numbers = new ArrayList<>();
map1.put(details.getProductSku(), numbers);
}
numbers.addAll(diohShippingDetails.getSerialNumbers());
When map1.get(…) returned a non-null List, we don’t need to put it again, but can add new elements to it. Only when numbers is null, we need to create a new list and put it into the map.
Alternatively, you can use
Map<String, List<String>> map1 = new HashMap<>();
for(Details details: detailsList) {
map1.computeIfAbsent(details.getProductSku(), key -> new ArrayList<>())
.addAll(diohShippingDetails.getSerialNumbers());
}
computeIfAbsent will use the specified function (key -> new ArrayList<>()) to create and store a new value if none exists. It will return an already existing list or the list just created and stored. Therefore, you can unconditionally add the elements to the returned list.
Try this:
Map<Integer, List<Integer>> map =
details.stream()
.collect(
toMap(Details::productSku,
d -> d.getNumbers() == null ? Collections.emptyList() : d.getNumbers(),
(a, b) -> Stream.concat(a.stream(), b.stream()).collect(toList())
)
);
trying to check "x" is present or not in {"a","b","c","d"}
String a="x";
String[] ids = {"a","b","c","d"};
Using beanshell
String a="x";
String[] ids = {"a","b","c","d"};
if(Arrays.asList(ids).contains(a)!= true)
{
log.info("Value is New");
}
I have the below class:
class A{
String property1;
String property2;
Double property3;
Double property4;
}
So the property1 and property2 is the key.
class Key{
String property1;
String property2;
}
I already have a list of A like below:
List<A> list=new ArrayList<>();
I want to group by using the key and add to another list of A in order to avoid having multiple items with same key in the list:
Function<A, Key> keyFunction= r-> Key.valueOf(r.getProperty1(), r.getProperty2());
But then while doing group by I have to take a sum of property3 and average of property4.
I need an efficient way to do it.
Note: I have skipped the methods of the given classes.
Collecting to a Map is unavoidable since you want to group things. A brute-force way to do that would be :
yourListOfA
.stream()
.collect(Collectors.groupingBy(
x -> new Key(x.getProperty1(), x.getProperty2()),
Collectors.collectingAndThen(Collectors.toList(),
list -> {
double first = list.stream().mapToDouble(A::getProperty3).sum();
// or any other default
double second = list.stream().mapToDouble(A::getProperty4).average().orElse(0D);
A a = list.get(0);
return new A(a.getProperty1(), a.getProperty2(), first, second);
})))
.values();
This could be slightly improved for example in the Collectors.collectingAndThen to only iterate the List once, for that a custom collector would be required. Not that complicated to write one...
Try like this:
Map<A,List<A>> map = aList
.stream()
.collect(Collectors
.groupingBy(item->new A(item.property1,item.property2)));
List<A> result= map.entrySet().stream()
.map(list->new A(list.getValue().get(0).property1,list.getValue().get(0).property1)
.avgProperty4(list.getValue())
.sumProperty3(list.getValue()))
.collect(Collectors.toList());
and create avgProperty4 and sumProperty3 methods like to this
public A sumProperty3(List<A> a){
this.property3 = a.stream().mapToDouble(A::getProperty3).sum();
return this;
}
public A avgProperty4(List<A> a){
this.property4 = a.stream().mapToDouble(A::getProperty4).average().getAsDouble();
return this;
}
result = aList.stream().collect(Collectors
.groupingBy(item -> new A(item.property1, item.property2),
Collectors.collectingAndThen(Collectors.toList(), list ->
new A(list.get(0).property1, list.get(0).property1)
.avgProperty4(list).sumProperty3(list))
)
);
I have trained TokenNameFinder of OpenNLP which outputs .bin file. Now I need to list features with their importance.
I read code of TokenNameFinder and NameFinderME but could not find a way to print features. Is their any way to list all features of model along with their importance ?
Finally I figured out a way to list features. Function getDataStructures() of AbstractModel class returns a array of Object instances. Second element of this array is a Map<String, Integer> whose keys are combination of features and their values. Following is the code snippet for accessing features and their values:
AbstractModel maxModel = model.getArtifact("nameFinder.model");
Object[] obj = maxModel.getDataStructures();
if(obj!=null) {
Map<String, Integer> pmap = (HashMap<String, Integer>) obj[1];
Set<String> keySet = pmap.keySet();
for(String key: keySet) {
System.out.println(key +" **** "+ pmap.get(key));
}
} else {
System.out.println("obj is null." );
}
This should be simple but I could not wrap my head around it.. Here is how I am doing it now but it seems so wasteful.
There is a
List<string> committees
and
List<string> P.committees
I just want to see if one list has any strings that are contained in the other.
List<Person> listFilteredCommitteesPerson = new List<Person>();
foreach (Person p in listFilteredPerson)
{
foreach (string strCommittee in p.Committees)
{
if (committees.Contains(strCommittee))
{
listFilteredCommitteesPerson.Add(p);
}
}
}
listFilteredPerson = listFilteredCommitteesPerson;
For a boolean value:
var match =
committees.Intersect(listFilteredPerson.SelectMany(p => p.Committees)).Any();
If you want a collection of Person that have a match you can use:
var peopleThatMatch =
listFilteredPerson.Where(p => committees.Intersect(p.Committees).Any());
or:
var peopleThatMatch =
listFilteredPerson.Where(p => p.Committees.Any(s => committees.Contains(s)));
You might want to consider another collection type (e.g. HashSet<T>) for performance reasons if you have large collections.