I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Although converting the ArrayList to a HashSet effectively removes duplicates, if you need to preserve insertion order, I'd rather suggest you to use this variant
// list is some List of Strings
Set<String> s = new LinkedHashSet<>(list);
Then, if you need to get back a List reference, you can use again the conversion constructor.
In Java 8:
List<String> deduped = list.stream().distinct().collect(Collectors.toList());
Please note that the hashCode-equals contract for list members should be respected for the filtering to work properly.
Suppose we have a list of String like:
List<String> strList = new ArrayList<>(5);
// insert up to five items to list.
Then we can remove duplicate elements in multiple ways.
Prior to Java 8
List<String> deDupStringList = new ArrayList<>(new HashSet<>(strList));
Note: If we want to maintain the insertion order then we need to use LinkedHashSet in place of HashSet
Using Guava
List<String> deDupStringList2 = Lists.newArrayList(Sets.newHashSet(strList));
Using Java 8
List<String> deDupStringList3 = strList.stream().distinct().collect(Collectors.toList());
Note: In case we want to collect the result in a specific list implementation e.g. LinkedList then we can modify the above example as:
List<String> deDupStringList3 = strList.stream().distinct()
.collect(Collectors.toCollection(LinkedList::new));
We can use parallelStream also in the above code but it may not give expected performace benefits. Check this question for more.
If you don't want duplicates, use a Set instead of a List. To convert a List to a Set you can use the following code:
// list is some List of Strings
Set<String> s = new HashSet<String>(list);
If really necessary you can use the same construction to convert a Set back into a List.
Java 8 streams provide a very simple way to remove duplicate elements from a list. Using the distinct method.
If we have a list of cities and we want to remove duplicates from that list it can be done in a single line -
List<String> cityList = new ArrayList<>();
cityList.add("Delhi");
cityList.add("Mumbai");
cityList.add("Bangalore");
cityList.add("Chennai");
cityList.add("Kolkata");
cityList.add("Mumbai");
cityList = cityList.stream().distinct().collect(Collectors.toList());
How to remove duplicate elements from an arraylist
You can also do it this way, and preserve order:
// delete duplicates (if any) from 'myArrayList'
myArrayList = new ArrayList<String>(new LinkedHashSet<String>(myArrayList));
Here's a way that doesn't affect your list ordering:
ArrayList l1 = new ArrayList();
ArrayList l2 = new ArrayList();
Iterator iterator = l1.iterator();
while (iterator.hasNext()) {
YourClass o = (YourClass) iterator.next();
if(!l2.contains(o)) l2.add(o);
}
l1 is the original list, and l2 is the list without repeated items
(Make sure YourClass has the equals method according to what you want to stand for equality)
this can solve the problem:
private List<SomeClass> clearListFromDuplicateFirstName(List<SomeClass> list1) {
Map<String, SomeClass> cleanMap = new LinkedHashMap<String, SomeClass>();
for (int i = 0; i < list1.size(); i++) {
cleanMap.put(list1.get(i).getFirstName(), list1.get(i));
}
List<SomeClass> list = new ArrayList<SomeClass>(cleanMap.values());
return list;
}
It is possible to remove duplicates from arraylist without using HashSet or one more arraylist.
Try this code..
ArrayList<String> lst = new ArrayList<String>();
lst.add("ABC");
lst.add("ABC");
lst.add("ABCD");
lst.add("ABCD");
lst.add("ABCE");
System.out.println("Duplicates List "+lst);
Object[] st = lst.toArray();
for (Object s : st) {
if (lst.indexOf(s) != lst.lastIndexOf(s)) {
lst.remove(lst.lastIndexOf(s));
}
}
System.out.println("Distinct List "+lst);
Output is
Duplicates List [ABC, ABC, ABCD, ABCD, ABCE]
Distinct List [ABC, ABCD, ABCE]
There is also ImmutableSet from Guava as an option (here is the documentation):
ImmutableSet.copyOf(list);
Probably a bit overkill, but I enjoy this kind of isolated problem. :)
This code uses a temporary Set (for the uniqueness check) but removes elements directly inside the original list. Since element removal inside an ArrayList can induce a huge amount of array copying, the remove(int)-method is avoided.
public static <T> void removeDuplicates(ArrayList<T> list) {
int size = list.size();
int out = 0;
{
final Set<T> encountered = new HashSet<T>();
for (int in = 0; in < size; in++) {
final T t = list.get(in);
final boolean first = encountered.add(t);
if (first) {
list.set(out++, t);
}
}
}
while (out < size) {
list.remove(--size);
}
}
While we're at it, here's a version for LinkedList (a lot nicer!):
public static <T> void removeDuplicates(LinkedList<T> list) {
final Set<T> encountered = new HashSet<T>();
for (Iterator<T> iter = list.iterator(); iter.hasNext(); ) {
final T t = iter.next();
final boolean first = encountered.add(t);
if (!first) {
iter.remove();
}
}
}
Use the marker interface to present a unified solution for List:
public static <T> void removeDuplicates(List<T> list) {
if (list instanceof RandomAccess) {
// use first version here
} else {
// use other version here
}
}
EDIT: I guess the generics-stuff doesn't really add any value here.. Oh well. :)
public static void main(String[] args){
ArrayList<Object> al = new ArrayList<Object>();
al.add("abc");
al.add('a');
al.add('b');
al.add('a');
al.add("abc");
al.add(10.3);
al.add('c');
al.add(10);
al.add("abc");
al.add(10);
System.out.println("Before Duplicate Remove:"+al);
for(int i=0;i<al.size();i++){
for(int j=i+1;j<al.size();j++){
if(al.get(i).equals(al.get(j))){
al.remove(j);
j--;
}
}
}
System.out.println("After Removing duplicate:"+al);
}
If you're willing to use a third-party library, you can use the method distinct() in Eclipse Collections (formerly GS Collections).
ListIterable<Integer> integers = FastList.newListWith(1, 3, 1, 2, 2, 1);
Assert.assertEquals(
FastList.newListWith(1, 3, 2),
integers.distinct());
The advantage of using distinct() instead of converting to a Set and then back to a List is that distinct() preserves the order of the original List, retaining the first occurrence of each element. It's implemented by using both a Set and a List.
MutableSet<T> seenSoFar = UnifiedSet.newSet();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (seenSoFar.add(item))
{
targetCollection.add(item);
}
}
return targetCollection;
If you cannot convert your original List into an Eclipse Collections type, you can use ListAdapter to get the same API.
MutableList<Integer> distinct = ListAdapter.adapt(integers).distinct();
Note: I am a committer for Eclipse Collections.
If you are using model type List< T>/ArrayList< T> . Hope,it's help you.
Here is my code without using any other data structure like set or hashmap
for (int i = 0; i < Models.size(); i++){
for (int j = i + 1; j < Models.size(); j++) {
if (Models.get(i).getName().equals(Models.get(j).getName())) {
Models.remove(j);
j--;
}
}
}
If you want to preserve your Order then it is best to use LinkedHashSet.
Because if you want to pass this List to an Insert Query by Iterating it, the order would be preserved.
Try this
LinkedHashSet link=new LinkedHashSet();
List listOfValues=new ArrayList();
listOfValues.add(link);
This conversion will be very helpful when you want to return a List but not a Set.
This three lines of code can remove the duplicated element from ArrayList or any collection.
List<Entity> entities = repository.findByUserId(userId);
Set<Entity> s = new LinkedHashSet<Entity>(entities);
entities.clear();
entities.addAll(s);
for(int a=0;a<myArray.size();a++){
for(int b=a+1;b<myArray.size();b++){
if(myArray.get(a).equalsIgnoreCase(myArray.get(b))){
myArray.remove(b);
dups++;
b--;
}
}
}
When you are filling the ArrayList, use a condition for each element. For example:
ArrayList< Integer > al = new ArrayList< Integer >();
// fill 1
for ( int i = 0; i <= 5; i++ )
if ( !al.contains( i ) )
al.add( i );
// fill 2
for (int i = 0; i <= 10; i++ )
if ( !al.contains( i ) )
al.add( i );
for( Integer i: al )
{
System.out.print( i + " ");
}
We will get an array {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Code:
List<String> duplicatList = new ArrayList<String>();
duplicatList = Arrays.asList("AA","BB","CC","DD","DD","EE","AA","FF");
//above AA and DD are duplicate
Set<String> uniqueList = new HashSet<String>(duplicatList);
duplicatList = new ArrayList<String>(uniqueList); //let GC will doing free memory
System.out.println("Removed Duplicate : "+duplicatList);
Note: Definitely, there will be memory overhead.
ArrayList<String> city=new ArrayList<String>();
city.add("rajkot");
city.add("gondal");
city.add("rajkot");
city.add("gova");
city.add("baroda");
city.add("morbi");
city.add("gova");
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(city);
city.clear();
city.addAll(hashSet);
Toast.makeText(getActivity(),"" + city.toString(),Toast.LENGTH_SHORT).show();
you can use nested loop in follow :
ArrayList<Class1> l1 = new ArrayList<Class1>();
ArrayList<Class1> l2 = new ArrayList<Class1>();
Iterator iterator1 = l1.iterator();
boolean repeated = false;
while (iterator1.hasNext())
{
Class1 c1 = (Class1) iterator1.next();
for (Class1 _c: l2) {
if(_c.getId() == c1.getId())
repeated = true;
}
if(!repeated)
l2.add(c1);
}
LinkedHashSet will do the trick.
String[] arr2 = {"5","1","2","3","3","4","1","2"};
Set<String> set = new LinkedHashSet<String>(Arrays.asList(arr2));
for(String s1 : set)
System.out.println(s1);
System.out.println( "------------------------" );
String[] arr3 = set.toArray(new String[0]);
for(int i = 0; i < arr3.length; i++)
System.out.println(arr3[i].toString());
//output: 5,1,2,3,4
List<String> result = new ArrayList<String>();
Set<String> set = new LinkedHashSet<String>();
String s = "ravi is a good!boy. But ravi is very nasty fellow.";
StringTokenizer st = new StringTokenizer(s, " ,. ,!");
while (st.hasMoreTokens()) {
result.add(st.nextToken());
}
System.out.println(result);
set.addAll(result);
result.clear();
result.addAll(set);
System.out.println(result);
output:
[ravi, is, a, good, boy, But, ravi, is, very, nasty, fellow]
[ravi, is, a, good, boy, But, very, nasty, fellow]
This is used for your Custom Objects list
public List<Contact> removeDuplicates(List<Contact> list) {
// Set set1 = new LinkedHashSet(list);
Set set = new TreeSet(new Comparator() {
#Override
public int compare(Object o1, Object o2) {
if (((Contact) o1).getId().equalsIgnoreCase(((Contact) o2).getId()) /*&&
((Contact)o1).getName().equalsIgnoreCase(((Contact)o2).getName())*/) {
return 0;
}
return 1;
}
});
set.addAll(list);
final List newList = new ArrayList(set);
return newList;
}
As said before, you should use a class implementing the Set interface instead of List to be sure of the unicity of elements. If you have to keep the order of elements, the SortedSet interface can then be used; the TreeSet class implements that interface.
import java.util.*;
class RemoveDupFrmString
{
public static void main(String[] args)
{
String s="appsc";
Set<Character> unique = new LinkedHashSet<Character> ();
for(char c : s.toCharArray()) {
System.out.println(unique.add(c));
}
for(char dis:unique){
System.out.println(dis);
}
}
}
public Set<Object> findDuplicates(List<Object> list) {
Set<Object> items = new HashSet<Object>();
Set<Object> duplicates = new HashSet<Object>();
for (Object item : list) {
if (items.contains(item)) {
duplicates.add(item);
} else {
items.add(item);
}
}
return duplicates;
}
ArrayList<String> list = new ArrayList<String>();
HashSet<String> unique = new LinkedHashSet<String>();
HashSet<String> dup = new LinkedHashSet<String>();
boolean b = false;
list.add("Hello");
list.add("Hello");
list.add("how");
list.add("are");
list.add("u");
list.add("u");
for(Iterator iterator= list.iterator();iterator.hasNext();)
{
String value = (String)iterator.next();
System.out.println(value);
if(b==unique.add(value))
dup.add(value);
else
unique.add(value);
}
System.out.println(unique);
System.out.println(dup);
If you want to remove duplicates from ArrayList means find the below logic,
public static Object[] removeDuplicate(Object[] inputArray)
{
long startTime = System.nanoTime();
int totalSize = inputArray.length;
Object[] resultArray = new Object[totalSize];
int newSize = 0;
for(int i=0; i<totalSize; i++)
{
Object value = inputArray[i];
if(value == null)
{
continue;
}
for(int j=i+1; j<totalSize; j++)
{
if(value.equals(inputArray[j]))
{
inputArray[j] = null;
}
}
resultArray[newSize++] = value;
}
long endTime = System.nanoTime()-startTime;
System.out.println("Total Time-B:"+endTime);
return resultArray;
}
I was asked to write a small program to remove the duplicates from a list and make a new list without the duplicates. We had to do this using Generics in Java. This is what I have so far:
All help is greatly appreciated!!!
import java.util.ArrayList;
public class Assignment13 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("one");
list.add("two");
list.add("three");
list.add("three");
System.out.println("Prior to removal: " + list);
System.out.println("after: " + list2);
}
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> list2 = new ArrayList<E>();
for (int i = 1; i < list2.size(); i++) {
String a1 = list2.get(i);
String a2 = list2.get(i-1);
if (a1.equals(a2)) {
list2.remove(a1);
}
}
return list2;
}
}
This is the algorithm:
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> list2 = new ArrayList<E>();
for (E elem : list)
if (!list2.contains(elem))
list2.add(elem);
return list2;
}
You can achieve this with a loop and a condition:
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> list2 = new ArrayList<E>();
for(E item : list) {
if(!list2.contains(item)) {
list2.add(item);
}
}
return list2;
}
Using a Set (if allowed in your assignment) is more efficient than checking list.contains on each item. Ex:
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
Set<E> uniques = new HashSet<E>();
uniques.addAll(list);
return new ArrayList<E>(uniques);
}
The general strategy here is that you want to maintain a context as you traverse the list, and at each step, you use that piece of context to answer the question of whether the current item should be kept or thrown out. In pseudo-code:
public static <A> List<A> removeDuplicates(List<? extends A> original) {
List<A> result = new ArrayList<A>();
/* initialize context */
for (A item : original) {
if ( /* context says item is not a duplicate */ ) {
result.add(item);
}
/* update context to incorporate the current `item` */
}
return result;
}
Some people have brought up the question of whether you mean consecutive duplicates or non-consecutive ones. In reality, the difference in the solutions is small:
For consecutive duplicates the context is the most recently seen item.
For non-consecutive it's the Set<A> of all items seen up to that point.
I'll let you fill in the pattern for those cases.
From your question it seems that there is no guarantee that duplicated elements in the original list must appear in sequence. This means that checking if two adjacent elements are equal is not sufficient to remove all duplicates.
Checking adjacent elements would work if the list was sorted. But since you need to use generics, I suppose you are not allowed to make assumptions about the nature of the elements. This means you cannot sort the list, because you would need to know that the elements were Comparable.
So you can do it like this:
1. Create a new empty list
2. For each element in the original list
2.1 If the element is not in the new list, add it
2.2 Else, ignore it
If you are allowed to use Java 8, this is much easier:
static <E> List<E> removeDuplicates(List<E> list) {
return list.stream().distinct().collect(Collectors.toList());
}
Updated for your question
ArrayList<E> list2 = new ArrayList<E>();
for (int i = 1; i < list.size(); i++) {
String a1 = list2.get(i);
if (!list2.contains(a1)) {
list2.add(a1);
}
}
How to convert a nested list into a single list in java?
List list = new ArrayList();
List newList = new ArrayList();
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(4);
list.add(5);
list.addAll(list1);
list.add(6);
How can i add the elements of list to newList so that when i print newList
it prints
[5,2,4,6]
Unless there is something you're not telling us, it is simply a repeat of what you have already done:
newList.addAll(list);
As has been said in the comments though, you really should be using full generic typing for your list variables:
List<Integer> list = new ArrayList<>();
List<Integer> newList = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
You shouldn't be using raw types, and you can call newList.addAll(list); -
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(); // <-- diamond operator, Java 7 and up.
List<Integer> newList = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
list1.add(2);
list1.add(4);
list.add(5);
list.addAll(list1);
list.add(6);
newList.addAll(list); // <-- here
System.out.println(newList);
}
Output is the requested
[5, 2, 4, 6]
You already used a possible way with
list.addAll(list1)
Another way would be walking through the list resp. using it's iterator
to fetch and put all items into the newList.
Have a look at the API for more detailed info: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)
for(int i=0;i<list.size();i++){
if(list.get(i) instanceof Integer){
newlist.add(list.get(i));
}else{
newlist.addAll((ArrayList)list.get(i));
}
}
The list that has to hold another list should be defined properly, rest working will be same. Refer below:
import java.util.List;
import java.util.ArrayList;
public class demo2 {
public static void main(String[] args) {
List<List<Integer>> nestedList = new ArrayList<>();
List<Integer> list = new ArrayList<>();
list.add(1);
nestedList.add(list);
System.out.println(nestedList);
}
}
I am doing homework. I would like to build a base case for a recursion where ordering given numbers (list2) in ascending order. Purpose of writing this codes is that when all numbers are in ascending order then should stop calling a method called ascending(list2, list1); and all values in list2 should be shipped to list1. For instance, list2 = 6,5,4,3,2,1 then list2 becomes empty and list1 should be 1,2,3,4,5,6. I am trying to compare result with previous one and if matches then stop. But I can't find the base case to stop it. In addition, Both ascending() and fixedPoint() are void method. Anybody has idea? lol Took me 3 days...
When I run my code then
6,5,4,3,2,1
5,6,4,3,2,1
4,5,6,3,2,1
3,4,5,6,2,1
2,3,4,5,6,1
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
infinite.............
public class Flipper
{
public static void main(String[] args)
{
Flipper aFlipper = new Flipper();
List<Integer> content = Arrays.asList(6,5,4,3,2,1);
ArrayList<Integer> l1 = new ArrayList<Integer>(content);
ArrayList<Integer> l2 = new ArrayList<Integer>(); // empty list
aFlipper.fixedPoint(l2,l1);
System.out.println("fix l1 is "+l1);
System.out.println("fix l2 is "+l2);
}
public void fixedPoint(ArrayList<Integer> list1, ArrayList<Integer> list2)
{
// data is in list2
ArrayList<Integer> temp1 = new ArrayList<Integer>(); // empty list
if (temp1.equals(list2))
{
System.out.println("found!!!");
}
else
{
ascending(list2, list1); // data, null
temp1 = list1; // store processed value
System.out.println("st list1 is "+list1);
System.out.println("st list2 is "+list2);
}
fixedPoint(list2, list1); // null, processed data
}
Second try after receiving advice.
else {
temp1 = list2;
System.out.println("temp1: "+temp1);
// temp1 printed out the value assigned
// store only previous value
ascending(list2, list1); // data, null
temp2 = list1;
// store previous value
System.out.println("temp1: "+temp1);
// after invoking ascending() temp1
becomes empty lol So not able to compare in if statement....
Can anybody correct it?
System.out.println("temp2: "+temp2);
}
fixedPoint(list2, list1); // previous, proceeded data
After brain storming with dasblinkenlight, Julien S, Nikolas, ZouZou and vels4j a solution found. I appreciate your contribution of thought! :-)
public void fixedPoint(ArrayList<Integer> list1,
ArrayList<Integer> list2)
{
List<Integer> content = Arrays.asList(1);
ArrayList<Integer> temp1 = new ArrayList<Integer>(content);
fixedPoint(list2, list1, temp1);
}
// Since it is recursive method I needed to create another parameter
// to store temporary values.
public void fixedPoint(ArrayList<Integer> list1,
ArrayList<Integer> list2,
ArrayList<Integer> temp)
{
ArrayList<Integer> temp1 = new ArrayList<Integer>();
temp1 = temp;
if (temp1.equals(list2))
{
return;
}
else
{
temp1.clear();
for(int i = 0; i < list2.size(); i++)
// To store temp value of list2,
// I used add method. Because ArrayList is an object type so if I assign
// list2 to temp1 then it will assign memory address rather
// than values. Thus I will lose the values after invoking ascending() as
// all elements of list2 will shipped to list1. So List2 becomes empty.
{
temp1.add(list2.get(i));
}
ascending(list2, list1);
fixedPoint(list2, list1, temp1);
}
}
Purpose of writing this codes is that when all numbers are in ascending order then should stop calling a method called ascending(list2, list1)
Then you should add a loop that checks for the elements of list1 to be in ascending order, like this:
public void fixedPoint(ArrayList<Integer> list1, ArrayList<Integer> list2)
{
boolean isAscending = true;
for (int i = 1 ; (isAscending) && (i < list2.size()) ; i++) {
isAscending = list2.get(i-1) < list2.get(i);
}
if (isAscending) {
... // Insert code to copy the data from list2 to list1.
... // Note that a simple assignment is not going to work here!
System.out.println("found!!!");
return;
}
// It's not in ascending order - continue recursing down.
ascending(list2, list1);
ArrayList<Integer> temp1 = new ArrayList<Integer>(list1); // store processed value
fixedPoint(list2, list1);
// temp1 makes the old value of list1 available for comparison
System.out.println("st list1 is "+list1);
System.out.println("st list1 was "+temp1);
System.out.println("st list2 is "+list2);
}
There is no return in fixedPoint when the case is found. Therefore the line fixedPoint(list2, list1);
will be processed regardless of the fixed point.
I am not able to test since ascending method is not provided, however I think that
if (CollectionUtils.isEqualsCollection(list1,list2)
{
System.out.println("found!!!");
return;
}
would do the job.
You will require Apache-commons Collections to perform the equality on lists with isEqualsCollection.
Your problem probably arises from the usage of temp1.equals(list2). What you want to use is Arrays.equals(temp1, list2).
An explanation for this is given here by Peter Lawrey.
Edit
Yeah, I should probably read better.
I just checked and it appears ArrayList inherits .equals() from List which is defined differently than array.equals() and "should" work.