<Java基础语法>List中重复元素去重

直接上代码:

package com.itheima;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class forDemo {
	public static void main(String[] args) {
		
		List<String> list =  Arrays.asList("张三","李四","张三","王五","李四","赵六");
		System.out.println("removel_1.------------------------");
		List<String> removel_1 = removel_1(list);
		for (int i = 0; i < removel_1.size(); i++) {
			System.out.println(removel_1.get(i));
		}
		
		System.out.println("removel_2.------------------------");		
		List<String> removel_2 = removel_1(list);
		for (int i = 0; i < removel_2.size(); i++) {
			System.out.println(removel_2.get(i));
		}
		
		System.out.println("removel_3.------------------------");
		List<String> removel_3 = removel_1(list);
		for (int i = 0; i < removel_3.size(); i++) {
			System.out.println(removel_3.get(i));
		}
		
		System.out.println("removel_4.------------------------");
		List<String> removel_4 = removel_1(list);
		for (int i = 0; i < removel_4.size(); i++) {
			System.out.println(removel_4.get(i));
		}
		
		System.out.println("removel_5.------------------------");
		List<String> removel_5 = removel_1(list);
		for (int i = 0; i < removel_5.size(); i++) {
			System.out.println(removel_5.get(i));
		}
		
		System.out.println("removel_6.------------------------");
		List<String> removel_6 = removel_1(list);
		for (int i = 0; i < removel_6.size(); i++) {
			System.out.println(removel_6.get(i));
		}
		
		
	}
	
	/*
	 * 1.for 循环添加去重
	 */
	public static List<String> removel_1(List<String> list) {
		List<String> list2 = new ArrayList();
		for(String li :list) {
			if(!list2.contains(li)) {
				list2.add(li);
			}
		}
		return list2;
	}
	
	/*
	 * 2.for 双循环去重
	 */
	public static List<String> removel_2(List<String> list) {
		
		for (int i = 0; i < list.size() - 1; i++) {
			for (int j = list.size() - 1; j > i; j--) {
				if(list.get(j).equals(list.get(i))) {
					list.remove(j);
				}
			}
		}
		return list;
	}
	
	/*
	 * 3.for 循环重复坐标去重
	 */
	public static List<String> removel_3(List<String> list) {
		
		List<String> list2 = list;
		for(String li :list2) {
			if(list.indexOf(li) != list.lastIndexOf(li)) {
				list.remove(list.lastIndexOf(li));
			}
		}
		return list;
	}
	
	/*
	 * 4.Set 去重(HashSet:但是元素顺序可能无法保证)
	 */
	public static List<String> removel_4(List<String> list) {
		List<String> list2 = new ArrayList(new HashSet(list));
		return list2;
	}
	
	/*
	 * 5.Set 去重(LinkedHashSet:可保证元素顺序)
	 */
	public static List<String> removel_5(List<String> list) {
		List<String> list2 = new ArrayList(new LinkedHashSet(list));
		return list2;
	}
	
	/*
	 * 6.Stream流 去重(Jdk 1.8新特性)
	 */
	public static List<String> removel_6(List<String> list) {
		list = list.stream().distinct().collect(Collectors.toList());
		return list;
	}	
}

测试输出:

removel_1.------------------------
张三
李四
王五
赵六
removel_2.------------------------
张三
李四
王五
赵六
removel_3.------------------------
张三
李四
王五
赵六
removel_4.------------------------
张三
李四
王五
赵六
removel_5.------------------------
张三
李四
王五
赵六
removel_6.------------------------
张三
李四
王五
赵六

你可能感兴趣的:(java,ListMap,java,list,开发语言)