একটি তালিকা থেকে সদৃশ অপসারণ করতে, কোডটি নিম্নরূপ -
উদাহরণ
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.Set; public class Demo { public static void main(String[] args){ ArrayList<String> list = new ArrayList<String>(); list.add("Jacob"); list.add("Gary"); list.add("Gary"); list.add("Harry"); list.add("Harry"); list.add("Kevin"); System.out.println("List = "+list); Set<String> set = new LinkedHashSet<String>(list); System.out.println("List after removing duplicate elements = "+set); } }
আউটপুট
List = [Jacob, Gary, Gary, Harry, Harry, Kevin] List after removing duplicate elements = [Jacob, Gary, Harry, Kevin]
উদাহরণ
এখন আরেকটি উদাহরণ দেখা যাক -
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Demo { public static void main(String[] args){ List<String> list = new ArrayList<String> (Arrays.asList("Ryan","Kevin","Ryan","Harry","Mark","Mark")); System.out.println("ArrayList = "+list); List<String> list2 = list.stream() .distinct() .collect(Collectors.toList()); System.out.println("Updated ArrayList without duplicates = "+list2); } }
আউটপুট
ArrayList = [Ryan, Kevin, Ryan, Harry, Mark, Mark] Updated ArrayList without duplicates = [Ryan, Kevin, Harry, Mark]