I would like to know how to remove duplicate values in Integer Array.
I think you may face This question in all languages.
But in android I don't know how to achieve this.Can anyone please help me to fix this issue.
Thanks in advance...
Be sure your array is Integer type not int
Integer[] array; // Your integer array...
Set<Integer> set = new HashSet<Integer>();
Collections.addAll(set, array);
val finalArrayList: ArrayList<String>
val arrayList = arrayListOf<String>()
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
finalArrayList = arrayList.toSet().toList() as ArrayList<String>
Answer
arrayList: [ABC, XYZ, ABZ, ABC, XYZ, ABZ]
finalArrayList: [ABC, XYZ, ABZ]
Try this Code
public static void removeDuplicateWithOrder(ArrayList arlList)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
arlList.clear();
arlList.addAll(newList);
}
Related
i have String Array like this:
String[] q1={"AAA-BBB","AAA-CCC","AAA-DDD"}
and i want result like this
temp={"BBB","CCC","DDD"}
i tried below code but the result is wrong
for(int i=0;i<q1.length;i++){
ArrayList<String> temp=new ArrayList<>(Arrays.asList(q1[i].split("AAA-")));
}
Try like this:
ArrayList<String> temp=new ArrayList<>();
for(int i=0;i<q1.length;i++){
String[] array = q1[i].split("-");
temp.add(array[1]);
}
You could use substring:
ArrayList<String> temp = new ArrayList<>();
for(int i=0; i<q1.length; i++){
temp.add(q[i].substring(q[i].indexOf('-') + 1, q[i].length()))
}
you find error Because you use split
Splits this string around matches of the given regular expression.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
q1[i].split("AAA-")
in this line you got 2 result splited 0 = "" AND 1 = "BBB"
so you need to pick the sec result
you have multi Solution
like https://stackoverflow.com/a/50234408/6998825 said
String[] array = q1[i].split("-");
temp.add(array[1]);
//change this q1[i].split("AAA-") to
q1[0].substring(4)
if your AAA- is not going to change
Have you tried creating the ArrayList outside of the loop? As previously you were creating a new ArrayList for every element in your string array
ArrayList<String> temp = new ArrayList<>();
for(int i=0;i<q1.length;i++){
temp.add(q1[i].substring(4);
}
Assuming that "AAA-" is not going to change.
Hey stackoverflow community,
i want to send an Arraylist of Textviews through shared Preferences to another Activity, so i tried to convert my list into a HashSet.
This is not accepted:
public List<TextView> FavDishes = new ArrayList<TextView>();
.
.
.
FavDishes = new ArrayList<>();
FavDishes.add(eingabe);
**Set<String> taskSet = new HashSet<String>(FavDishes);**
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putStringSet("Data", taskSet)
.apply();
It tells me: "cannot resolve constructor HashSet on android widget Textview"
How would you solve this Problem?
Thanks for your time.
Iterate over the FavDishes array to get the TextView values as string and store them to an array, then pass that array to your taskset instead of FavDishes.
Code will be something like:
List<TextView> FavDishes = new ArrayList<TextView>();
FavDishes.add(fav1TV);
FavDishes.add(fav2TV);
FavDishes.add(fav3TV);
ArrayList<String> FavDishesString = new ArrayList<String>();
for (TextView FavDish : FavDishes ){
FavDishesString.add(FavDish.getText().toString());
}
Set<String> taskSet = new HashSet<String>(FavDishesString);
Helpful link to ways to iterate arrayList:
https://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
Please any one can help how to remove particular key from hashmap and then rearrange the keys in hashmap accordingly.
Below is my code.
Set<Integer> integerSet = hashMap.keySet();
int removekey = pos;
ArrayList<Integer> integers = new ArrayList<>();
for (Integer integer : integerSet) {
if (integer > removekey) {
integers.add(integer);
}
}
for (Integer integer : integers) {
if (hashMap.containsKey(integer)) {
AddCardPojo pojo = hashMap.get(integer);
pojo.setImagCard(cardImage[integer - 1]);
hashMap.remove(integer);
hashMap.put(integer - 1, pojo);
}
}[![enter image description here][1]][1]
I have attached screenshot of error
You can directly remove a key value pair,you can directly do
hashMap.remove(removeKey);
as for 're arranging keys in hashmap',
it is a data structure which makes no guarantees of order of data.
Check this answer for more
If you need a particular order as per integer, you could use arraylist
Finally it could be done.
Below is my answer.
hashMap.remove(key);
List<AddCardPojo> hashMapsList=new ArrayList<>();
Iterator it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
hashMapsList.add((AddCardPojo) pair.getValue());
}
hashMap = new HashMap<>();
for(int i=0; i<hashMapsList.size();i++){
hashMap.put(i,hashMapsList.get(i));
}
public static void printList(ArrayList<Col> list, String place)
{
ArrayList<Col> list2 = list;
int count=0;
float r,g,b;
while(!list2.isEmpty())
{
r = list2.get(count).getR();
g = list2.get(count).getG();
b = list2.get(count).getB();
list2.remove(0);
Log.i(place+": "+count + "", "R: "+r+" G: "+g+" B: "+b);
}
}
this method is removing all the items from my original list for some reason..
i'm thinking maybe the way i duplicate the list is wrong, but i couldnt find the right way.
Since ArrayList is an object:
ArrayList<Col> list2 = list;
is a soft copy. It only copies a reference to the same ArrayList object.
In Java you can clone an object like so:
ArrayList<Col> list2 = (ArrayList<Col>)list.clone();
USE
ArrayList list2 = new ArrayList(list);
INSTEAD OF
ArrayList list2 = list;
Use the following code before assigning the value:
ArrayList<Col> list2 = new ArrayList<Col> ();
Suppose I have LinkedhashMap<String, List> myLHM = new LinkedHashMap<>()
Values of myLHM :
<Roy,[1,2,3]>
<Roy,[14,15,16]>
<Neha,[1,5,6]>
<Neha,[11,12,13]>
<Jane,[11,8,9]>
In above eg., Roy and Neha is repetitive/duplicate.
Is it possible to hold duplicate keys in myLHM ? Because I'm not able to store duplicate keys
No? Then what is the alternative to LinkedHashMap to hold duplicate keys?
TIA!
Edit: Those two Roy and Neha are each the same person
I don't know of any map in the standard java library that can hold duplicate keys, but Guava is an excellent extension to the normal java Collections (and more) done by Google. There you have Multimap which can hold several values for the same key (I guess this is what you want in the end). The main benefit I find in using such a library/implementation is that it will take care of everything for you associated with the storage of the values and you don't need to bother about implementing it yourself.
PS: Guava is not just about Collections, which I think it's another Pro why you should check it out.
Add a List of items by key. Whenever you insert, if key was found, just add to the collection. When not found, add a new colelction with current item.
I solved this by myself.
What I did is used :
LinkedHashMap<String, ArrayList<ArrayList>> myLHM = new LinkedHashMap<>() ;
Inserted into it as:
ArrayList<ArrayList> multiDimArray = new ArrayList<>();
ArrayList list = new ArrayList();
list.add("abc");//some string 1
list.add("lmn");//some string 2
list.add("xyz");//some string 3
multiDimArray.add(list);
myLHM.put("abc"/*use your variable OR pass value like myList.get(position)*/,multiDimArray);
Fetched / Retrieved values as:
List tempNames=new ArrayList();
Iterator myVeryOwnIterator = myLHM.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
tempNames.add(myVeryOwnIterator.next());
}
ArrayList<ArrayList> tempArrayList = new ArrayList();
for (int i=0;i<tempNames.size();i++){
tempArrayList.addAll(myLHM.get(tempNames.get(i)));
}
String item = null; int year = 0; int amount = 0;
for (int i=0;i<tempArrayList.size();i++) {
for (int j=0;j<tempArrayList.get(i).size();j++){
if(j==0){
item = (String) tempArrayList.get(i).get(j);
item = item.replaceAll("\\s", "_");
} else if (j==1){
year = Integer.valueOf(tempArrayList.get(i).get(j).toString());
} else if (j==2){
amount = Integer.valueOf(tempArrayList.get(i).get(j).toString());
} else {
Log.e("Problem","for loop error");
}
}
db.execSQL("INSERT INTO ch // my insert query...
VALUES(" +"'" + item +"'," + year +"," + amount +"," + id +");");
}