JAVA notes List

Keywords: Java

List in Java

Basic concepts

List: inherits the collection. The list elements are ordered. Because the list has an index, the elements can be repeated. The commonly used implementation classes are ArrayList (data structure uses array, query speed is fast, adding and deleting is slow, thread is not synchronous), LinkedList (data structure uses linked list, query is slow, adding and deleting is fast), Vector (data structure uses array, query and adding and deleting are slow, and the base is replaced by ArrayList).

List < Object > de duplication

  • List < string > de duplication

Use Set to de duplicate

Set<Integer> set = new HashSet<>(list);

Second, use JDK1.8 Stream to remove List, first get the Stream. of this list, then call distinct() method, java8 provides the way of flow to deal with the data, very quickly, the bottom layer uses forkJoin frame, and provides parallel processing, so that many processors process the data in the flow at the same time, so it takes a very short time.

List<String> unique = list.stream().distinct().collect(Collectors.toList());
  • List < user > de duplication
    private Integer id;
    private String userName;
    private Integer age;
    private String address;

Override equals() and hashcode()

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (address == null) {
            if (other.address != null)
                return false;
        } else if (!address.equals(other.address))
            return false;
        if (age == null) {
            if (other.age != null)
                return false;
        } else if (!age.equals(other.age))
            return false;
        if (userName == null) {
            if (other.userName != null)
                return false;
        } else if (!userName.equals(other.userName))
            return false;
        return true;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((address == null) ? 0 : address.hashCode());
        result = prime * result + ((age == null) ? 0 : age.hashCode());
        result = prime * result + ((userName == null) ? 0 : userName.hashCode());
        return result;
    }
public class Test {
    public static void main(String[] args) {
        User u1=new User(1,"aaa",1,"shanghai");
        User u2=new User(2,"bbb",2,"beijing");
        User u3=new User(1,"aaa",1,"shanghai");
        List<User> list=new ArrayList<User>();
        List<User> personList =new ArrayList<User>();
        list.add(u1);
        list.add(u2);
        list.add(u3);
         // Duplicate removal
        list.stream().forEach(
                u -> {
                    if (!personList.contains(u)) {
                        personList.add(u);
                    }
                }
        );
        System.out.println(personList);         
    }

There is also a cool way to write.

package test;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.collectingAndThen;


public class Test {
    public static void main(String[] args) {
        User u1=new User(1,"aaa",1,"shanghai");
        User u2=new User(2,"bbb",2,"beijing");
        User u3=new User(1,"aaa",1,"shanghai");
        List<User> list=new ArrayList<User>();
        list.add(u1);
        list.add(u2);
        list.add(u3);
         // Duplicate removal
         List<User> unique = list.stream().collect(
                    collectingAndThen(
                            toCollection(() -> new TreeSet<>(comparingInt(User::getId))), ArrayList::new)
            );
        System.out.println(unique);     
    }
}

List < Object > sort

public class User implements Comparable<User>{
    ...
    @Override
    public int compareTo(User o) {
        return this.id.compareTo(o.getId());
    }
}
    list.sort(Comparator.naturalOrder());
    Collections.sort(list);

List use

  • In a circle of 14 people, count 1, 2 and 3 from the first person. What's the last number of people left after counting to three?
public class Test {
    public static void main(String[] args) {
        List<Integer> list=new ArrayList<Integer>();
        for(int i=0;i<14;i++) {
            list.add(i+1);            
        }
         int m = 0;  
            while(list.size() > 1) {  
                for (int j=0; j<list.size(); j++) {  
                    m++;  
                    if (m%3 == 0) {  
                        System.out.println(list.remove(j)+"No. leave");  
                        j--;  
                        m = 0;    
                    }     
                }     
            }  
            System.out.println(list);     
    }
}
  • There are 50 balls in a box according to the number of 1. Take two balls randomly from the box each time and divide them into a group to find the member number of each group?
public class Test {
    public static void main(String[] args) {
        List<String> list=getList();
        int length = 0,index,index2;
        Random random=new Random();
        for(int i=0;i<25;i++) {        
            while(true) {                
                index=random.nextInt(50-length);            
                index2=random.nextInt(50-length);
                if(index!=index2) {
                    break;
                }
            }        
            System.out.println("The first"+i+"Group:"+list.get(index)+","+list.get(index2));    
            list.remove(index);
            if(index>index2){
                list.remove(index2);
            }else {
                list.remove(index2-1);
            }
            length=length+2;                
        }        
    }
    
    public static List<String> getList() {
        List<String> list=new ArrayList<String>();
        for(int i=0;i<50;i++) {
            list.add(i+1+"");
        }
        return list;
    }
}

Posted by ericdorman on Sun, 08 Dec 2019 04:54:38 -0800