Java - Replace single element with multiple elements from list - java

I want to replace single object from List<String> with multiple objects.
List<String> list = new ArrayList<>(Arrays.asList("ROLL_NO","FULL_NAME","PERCENTAGE", "ADDRESS"));
I want to replace "FULL_NAME" with "FIRST_NAME" AND "LAST_NAME". So my list elements will be like as
"ROLL_NO",
"FIRST_NAME",
"LAST_NAME",
"PERCENTAGE",
"ADDRESS"
Collections.replaceAll() method replaces with single object only.
Is there a better way with java8 stream?
Thanks in advance.

Remove the FULL_NAME element and replace it with the FIRST_NAME and LAST_NAME elements:
List<String> list = new ArrayList<>(Arrays.asList("ROLL_NO","FULL_NAME","PERCENTAGE", "ADDRESS"));
int index = list.indexOf("FULL_NAME");
list.remove(index); //remove "FULL_NAME"
list.add(index, "LAST_NAME");
list.add(index, "FIRST_NAME");
Note the order which I am adding them. First LAST_NAME and then FIRST_NAME. This is done as setting an element at in index shifts the current element at that index to the right of the list. By doing so the elements are in the correct order as you suggested in the question.

To solve your problem, you can use remove() and add(). But a better approach will be creating a class to hold all those attributes:
public class Person{
private String rollNo;
private String firstName;
private String lastName;
private double percentage;
private String double;
//your constructors, getters & setters
}
Then use an ArrayList to hold various Person object together as one collection:
ArrayList<Person> list = new ArrayList<>();
This way, whenever you need to add/remove a record as a whole, there is no need to add/remove all related attributes manually which is very prone to mistakes.

If you list is mutable, you can use a listIterator to accomplish the same thing.
import java.util.ArrayList;
import java.util.List;
class Scratch {
public static void main(String[] args) {
final var values = new ArrayList<>(List.of(args));
final var it = values.listIterator();
while (it.hasNext()) {
final var value = it.next();
if ("FULL_NAME".equals(value)) {
it.remove();
it.add("FIRST_NAME");
it.add("LAST_NAME");
}
}
}
}

Related

Remove a element from List of objects based on inner object list value in java

How can I remove the element from the list if some inner list attribute value fails to meet the condition.The trick here is that attribute is itself a list and comparison is based on some attribute of that inner list. Please refer the below sample and help out to fill the comment section in code:
Object :
Class product{
private String productId;
private String productName;
private List<Attribute> attributeList;
public static class Attribute{
private Long attributeId;
}
}
Driver class :
Class Driver{
List<product> productList = new ArrayList<product>();
/*
Remove the object from productList if attributeList doesn't contain attribute with attributeId = x;
*/
}
What you can do it to stream over original list, and leave only objects which satisfy the condition. It might look something like this:
List<Product> filtered = productList.stream()
.filter( p -> p.attributeList().stream().anyMatch( a -> a.attributeId.equals(x))
.collect(Collectors.toList())
in this live we are actually checking if nested list contains at least one object with attributeId = x
p.attributeList().stream().anyMatch( a -> a.attributeId.equals(x)
You can do a foreach loop and remove the unwanted elements. In "product" class you can insert a "FindInnerAtribute" function to search inside the Attributes list and return true if there is some.
List<product> productList = new ArrayList<product>();
for(product p : productList){
if ( p.FindInnerAttribute(x) ){
productList.remove(p);
}
}
How to remove from list

LinkedList object add() function not updating values - java

I am creating an inverted index dictionary, which takes a million or so tweets from a file, stores the words from these tweets as the Keys in the dictionary (HashMap) and a pointer to a postings list (LinkedList) which contains the document ID (tweet username, date etc.) as the Value of the key.
My function stores the words as the key for the HashMap with no problem and should store an object pointer to the postings list for each occurrence of the word as the value for the key. But for some reason when I try to update the list it doesn't work. Once the entire file has been read through, the HashMap contains the keys with null Objects as their values.
The code here:
String line = scanner.nextLine();
String[] lineArr = line.split(" ");
DocID id = new DocID(lineArr[0], lineArr[1],lineArr[2]);
for(int i=3; i<lineArr.length; i++){
ListPointer list = new ListPointer();
if(dict.containsKey(lineArr[i].toLowerCase())) list = dict.get(lineArr[i]);
list.postings.add(id);
dict.put(lineArr[i].toLowerCase(), list);
}
}
should store an Object with a list attribute as the value, effectively acting as a pointer to a list. If a similar key exists in the table, the value is obtained and the list attribute of that value should be updated and set again as the value for that key.
I know using a LinkedList as the value of the HashMap rather than using an object containing an inherent list would be better, but we were instructed that the postings list should be stored separately and shouldn't be an attribute of the dictionary class, and the dictionary should just contain a pointer to its relevant postings list.
So far these are the objects with their members:
public static HashMap<String, ListPointer> dict;
public static class DocID{
public String userID;
public String date;
public String time;
public DocID(String dte, String tme, String id){
this.userID = id;
this.date = dte;
this.time = tme;
}
}
public static class ListPointer{
public static LinkedList<DocID> postings;
public ListPointer(){
postings = new LinkedList<DocID>();
}
}
I could understand if it was an overwriting error, but no, the value of each key in the HashMap upon complete read through of the file is null and I have no idea why this could be?
Your postings member shouldn't be static. You have a single instance shared across all ListPointer instances, and you overwrite it with an empty LinkedList<DocID> each time the ListPointer constructor is invoked.
Change
public static LinkedList<DocID> postings;
to
public LinkedList<DocID> postings;
EDIT :
You have another problem in the retrieval from the Map :
Change
if(dict.containsKey(lineArr[i].toLowerCase())) list = dict.get(lineArr[i]);
to
if(dict.containsKey(lineArr[i].toLowerCase())) list = dict.get(lineArr[i].toLowerCase());
If you are passing a lower case String to containsKey, you must pass the same lower case String to get. Otherwise get will return null if the original key wasn't lower case.
I see two issues:
Issue 1.
public static class ListPointer{
public static LinkedList<DocID> postings;
...
The class ListPointer does not need to be static and the member "postings" does not to be static either.
Issue 2
if(dict.containsKey(lineArr[i].toLowerCase())) list = dict.get(lineArr[i]);
I think the main problem is in this line. you are trying to match everything in lower case, but when you get the key from dict you aren't using .toLowerCase()

Remove duplicates from a list in an Arraylist in java

I checked many examples but i could not applied for my variables.
I have a ArratyList Of lists of Strings.
ArrayList<List<String>> bulkUploadList = new ArrayList<List<String>>();
and it's look like this:
[id, title, tags, descriptions]
[4291483113.0000000000000, Camden, camdentown;london, NoValue]
[4292220054.0000000000000, IMG_2720, NoValue, NoValue]
[4292223824.0000000000000, IMG_2917, london;camdentown, NoValue]
[4292224728.0000000000000, IMG_2945, London;CamdenTown, NoValue]
I want to remove those rows which have the same titles and the same tags.
I do not know how work with HashSet since I have a ArrayList of List of Strings.
Not best solution, but you can start with this:
ArrayList<List<String>> bulkUploadList = new ArrayList<List<String>>();
ArrayList<List<String>> result = new ArrayList<List<String>>();
HashSet<String> hashSet = new HashSet<>();
for(List<String> item : bulkUploadList) {
String title = item.get(1);
String tags = item.get(2);
String uniqueString = (title + "#" + tags).trim().toUpperCase();
if(!hashSet.contains(uniqueString)) {
result.add(item);
hashSet.add(uniqueString);
} else {
System.out.println("Filtered element " + uniqueString);
}
}
As suggested in one of the comments, you should create a class for the data, make that class implement equals(), and then use HashSet to remove dups. Like this.
class Foo {
String id;
String title;
String tags;
String description;
public boolean equals(Foo this, Foo other) {
return this.id.equals(other.id)
&& this.title.equals(other.title)
&& etc.
}
then you can remove dups with
Set<Foo> set = new LinkedHashSet<Foo>(list);
as Sets do not allow duplication, and the equals() method is used to check.
You should use a linkedHashSet here because you want to preserve the order (according to a comment you made on elsewhere).
You should also implement a hashcode() method consistent with equals().

Java - How to use a for each loop to check the different occurrences of a value in a list of objects

Sorry if the title isn't clear, I wasn't sure how to word it. I have an arraylist of objects and within each of these objects I store an integer value referring to a category and one referring to an ID.
I want to find the number of unique combinations of category and IDs that there are.
So at the moment I have
for(Object object: listofObjects){
//For each unique type of object.getID
//For each unique type of object.getCategory
//Add 1 to counter
}
I can't figure out how to do this. Doing things like for(int cat: object.getCategory()) brings up an error.
I can add the values to a new list within the initial for each loop like so,
ArrayList<Integer> aList= new ArrayList<Integer>();
for (Object object : spriteExplore) {
aList.add(object.getCategory());
}
for (int cat : aList) {
testCounter++;
}
but this obviosuly does not take into account uniqueness and also makes it awkward for factoring in the other variable of ID.
I feel like there is probably some easier work around that I am missing. Any advice?
Thanks in advance.
So you list of UserDefine object in ArrayList and you want to find unique Object.Just create set from list.
For e.g Suppose you have
List<Customer> list=new ArrayList<Custeomer>();
list.add(new Customer("A",12));
list.add(new Customer("B",13));
list.add(new Customer("A",12));
now
create set From this list
Set<Customer> set = new HashSet<Customer>(list);
this will have unique Customer
IMP : dont forget to override equals and hashcode method for Customer
Your best approach would be storing the data correctly.
It's possible that you still need to store non-unique items, if that's so - continue using an ArrayList, but in addition, use the following:
Override the hashcode & equels function as shown in this link:
What issues should be considered when overriding equals and hashCode in Java?
Then, use a Set (HashSet would probably be enough for you) to store all your objects. This data structure will disregard elements which are not unique to elements already inside the set.
Then, all you need to do is query the size of the set, and that gives you the amount of unique elements in the list.
I don't know any library that does this automatically, but you can do it manually using sets. Sets will retain only unique object so if you try to add the same value twice it will only keep one reference.
Set<Integer> categories = new HashSet<Integer>();
Set<Integer> ids= new HashSet<Integer>();
for (Object object : listofObjects) {
categories.add(object.getCategory());
ids.add(object.getID());
}
Then you get the number of unique categories / ids by doing
categories.size()
ids.size()
And all your unique values are stored in the sets if you want to use them.
I would look into using a (Hash)Map<Integer, Integer>. Then just have 1 foreach loop, checking to see if the value of Map<object.getId(), object.getCategory()> is null by checking if map.get(object.getId()) is null - if it is, then this pair does not exist yet, so add this pair into the map by using map.put(object.getId(), object.getCategory()). If not, do nothing. Then at the end, to find the number of unique pairs you can just use map.size()
Hope this helps
Map<Integer,List<Integer>> uniqueCombinations = new HashMap<Integer,List<Integer>>();
for (Object object : listofObjects) {
if(uniqueCombinations.get(object.getCategoryId())==null) {
uniqueCombinations.put(object.getCategoryId(), new LinkedList<Integer>);
}
uniqueCombinations.get(object.getCategoryId()).add(object.getId());
}
return uniqueCombinations.size()
I believe you want unique combinations of both category and id, right?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SO {
class MyObject{
private int id;
private int category;
private String name;
private MyObject(int id, int category,String name) {
super();
this.id = id;
this.category = category;
this.name = name;
}
protected int getId() {
return id;
}
protected int getCategory() {
return category;
}
#Override
public String toString() {
return "MyObject [id=" + id + ", category=" + category + ", name=" + name + "]";
}
}
public static void main(String[] args) {
SO so = new SO();
List<Object> listofObjects = new ArrayList<Object>();
listofObjects.add(so.new MyObject(1,1,"One"));
listofObjects.add(so.new MyObject(1,1,"Two"));
listofObjects.add(so.new MyObject(1,2,"Three"));
Map<String,List<MyObject>> combinations = new HashMap<String,List<MyObject>>();
for(Object object: listofObjects ){
//For each unique type of object.getID
//For each unique type of object.getCategory
//Add 1 to counter
if (object instanceof MyObject){
MyObject obj = (MyObject)object;
String unique = obj.id+"-"+obj.category;
if (combinations.get(unique) == null){
combinations.put(unique, new ArrayList<MyObject>());
}
combinations.get(unique).add(obj);
}
}
System.out.println(combinations);
//counts
for(Entry<String,List<MyObject>> entry:combinations.entrySet()){
System.out.println(entry.getKey()+"="+entry.getValue().size());
}
}
}
Use the Hashmap to save occurence. Dont forget to implement hashcode und equals Methods. You can generate them if you work with Eclipse IDE.
public static void main(String[] args) {
List<MyObject> myObjects = Arrays.asList(new MyObject(1, 2), new MyObject(2, 3), new MyObject(3, 4), new MyObject(3, 4));
Map<MyObject, Integer> map = new HashMap<>();
for (MyObject myObject : myObjects) {
Integer counter = map.get(myObject);
if(counter == null){
counter = 1;
} else {
counter = counter + 1;
}
map.put(myObject, counter);
}
long uniqueness = 0;
for(Integer i : map.values()){
if(i == 1){
++uniqueness;
}
}
System.out.println(uniqueness);
}
The last part can be replaced by this one line expression if you are working with Java 8:
long uniqueness = map.values().stream().filter(i -> i == 1).count();

JPA: Store a list of integers in a single field

Is it possible to store a list of integers in a single field of the respective entity table with standard JPA 2?
#Entity
#Table(name="tbl_myentities")
public class MyEntity {
#ElementaryCollection
#Column(name="vals") // in table tbl_myentities
private List<Integer> vals;
It is not possible to store multiple values in a single field. Whats the reason behind storing them in a single field?
A way could be to use a field of type String and add all integers there in a comma separated list and join/explode in getters and setters:
private String vals;
public setVals(int vals[])
{
// this.vals = Iterate vals[] and create a comma separated string
}
public int[] getVals()
{
// vals.split(",") to get a list of Strings, then typecast/parse them to ints before returning
}
Using the #ElementCollection annotation and #CollectionTable to control the mappings requires a separate table to store the values in.
#ElementCollection
private Collection<Integer> integers;
Read more about element collections on on http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection
Similar question here Does JPA #ElementCollection annotation always produce an one-to-many relationship?
You can create a converter and use it with the annotation #Converter.
This converter must implement AttributeConverter which is a generic interface with two methods convertToDatabaseColumn and convertToEntityAttribute.
It is pretty easy to work with, you can check here: jpa independent custom type mapping / javax.persistence.x alternative to org.hibernate.annotations.Type and org.hibernate.annotations.TypeDef
You can store all the vals in a String field, separated with a comma, and change associated getter and setter like that :
public List<Integer> getVals() {
List<Integer> lstVals = new ArrayList<Integer>();
int val = 0;
for(String field : this.vals.split(",")) {
try {
val = Integer.parseInt(field);
}
// If the String contains other thing that digits and commas
catch (NumberFormatException e) {
}
lstVals.add(val);
}
return lstVals;
}
public void setVals(List<Integer> vals) {
String newVals = "";
for(int i : vals) {
newVals.concat(String.valueOf(i));
}
this.vals = newVals;
}
I don't think that's possible. Because you can not have a column in a database table that allows you to store list of integers.
What you can do is use a string type field instead of list of integers -
#Column(name="vals") // in table tbl_myentities
private String vals;
And do the conversion to string from list of integers and back manually before you save your entity and after you have read your entity.
Maybe a #Lob could suit you ? (despite what it imply)
#Lob
ArrayList<String> vals;
(note that your collection must be explicitly an ArrayList)

Categories

Resources