0% found this document useful (0 votes)
2 views

Java programs ARRAYLIST

The document contains Java code for two tasks: filtering duplicate elements from an array and sorting a list of strings using Java Collections. The first part defines a class that creates a list of numbers, adds duplicates, and processes the list to filter out duplicates using a HashSet. The second part is a prompt for writing code to sort a list of strings, but the implementation is not provided.

Uploaded by

ishapotphode06
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java programs ARRAYLIST

The document contains Java code for two tasks: filtering duplicate elements from an array and sorting a list of strings using Java Collections. The first part defines a class that creates a list of numbers, adds duplicates, and processes the list to filter out duplicates using a HashSet. The second part is a prompt for writing code to sort a list of strings, but the implementation is not provided.

Uploaded by

ishapotphode06
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java programs

Question-1: Write code to filter duplicate elements from an array and print as a list?
package simple.test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class findDuplicates
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
// Form a list of numbers from 0-9
.for (int i = 0; i < 10; i++) {
list.add(String.valueOf(i));
}
// Insert a new set of numbers from 0-5.
for (int i = 0; i < 5; i++)
{
list.add(String.valueOf(i));
}
System.out.println("Input list : " + list);
System.out.println("\nFiltered duplicates : " + processList(list));
}
public static Set<String> processList(List<String> listContainingDuplicates)
{
final Set<String> resultSet = new HashSet<String>();
final Set<String> tempSet = new HashSet<String>();
for (String yourInt : listContainingDuplicates)
{if (!tempSet.add(yourInt))
{
resultSet.add(yourInt);
}
}
return resultSet;
}
}
Question-2: Write code to sort the list of strings using Javacollection?

You might also like