Java Interview







 
1)how to remove duplicates from the list without using hashset
public class JavaStreamDistinctExamples
{
    public static void main(String[] args)
    {
        Collection<String> list = Arrays.asList("A", "B", "C", "D", "A", "B", "C");

// Get collection without duplicate i.e. distinct only
List<String> distinctElements = list.stream().distinct().collect(Collectors.toList());

//Let's verify distinct elements
System.out.println(distinctElements);

    }
output
[A,B,C,D]
2)how to remove duplicates from the userdefined object List without using hashset


import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class JavaStreamDistinctExamples
{
    public static void main(String[] args)
    {
        Person lokesh = new Person(1, "Lokesh", "Gupta");
        Person brian = new Person(2, "Brian", "Clooney");
        Person alex = new Person(3, "Alex", "Kolen");
        
      List<Person> list=new ArrayList<>();
        //Add some random persons
       // Collection<Person> list = Arrays.asList(lokesh,brian,alex,lokesh,brian,lokesh);
         
        list.add(lokesh);
      list.add(brian);
      list.add(alex);
      list.add(brian);
      list.add(lokesh);
      list.add(alex);
      list.add(alex);
     
      list.add(lokesh);list.add(lokesh);list.add(lokesh);
     
        // Get distinct only
        List<Person> distinctElements = list.stream().filter(distinctByKey(p -> p.getId())).collect(Collectors.toList());

        // Let's verify distinct elements
        System.out.println(distinctElements);
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
    {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}

class Person
{
    public Person(Integer id, String fname, String lname) {
        super();
        this.id = id;
        this.fname = fname;
        this.lname = lname;
    }

    private Integer id;
    private String fname;
    private String lname;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", fname=" + fname + ", lname=" + lname + "]";
    }
}
output
[Person [id=1, fname=Lokesh, lname=Gupta], Person [id=2, fname=Brian, lname=Clooney], Person [id=3, fname=Alex, lname=Kolen]]
================================================================

No comments:

Post a Comment