【去除集合中字符串的重复值-1】

package com.yjf.esupplier.common.test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author shusheng
 * @description 去除集合中字符串的重复值(字符串的内容相同)
 * @Email [email protected]
 * @date 2018/12/12 16:24
 */
public class ArrayListDemo {
    /**
     * ArrayList 去除集合中字符串的重复值(字符串的内容相同)
     * <p>
     * 分析:
     * A:创建集合对象
     * B:添加多个字符串元素(包含内容相同的)
     * C:创建新集合
     * D:遍历旧集合,获取得到每一个元素
     * E:拿这个元素到新集合去找,看有没有
     * 有:不搭理它
     * 没有:就添加到新集合
     * F:遍历新集合
     */
    public static void main(String[] args) {
        ArrayList array = new ArrayList();
        array.add("hello");
        array.add("world");
        array.add("world");
        array.add("world");
        array.add("java");
        array.add("java");
        array.add("hello");
        array.add("hello");
        array.add("software");

        ArrayList newArray = new ArrayList();

        Iterator it = array.iterator();
        while (it.hasNext()) {
            Object s = it.next();
            if (!newArray.contains(s)) {
                newArray.add(s);
            }
        }

        for (int x = 0; x < newArray.size(); x++) {
            System.out.println(newArray.get(x));
        }
    }

}

原文地址:https://www.cnblogs.com/zuixinxian/p/10340835.html

时间: 2024-08-27 14:44:17

【去除集合中字符串的重复值-1】的相关文章

ArrayList去除集合中字符串的重复值,只能在本集合内操作

/* * 需求:ArrayList去除集合中字符串的重复值,只能在本集合内操作 * * 分析: * 1.创建一个集合对象 * 2.添加多个字符串元素 * 3.用选择排序方法去比较 * A:如有相同的,则删除此元素 * B:没有,则保留 * 4.遍历输出 新集合 */ package com.ma.arraylist; import java.util.ArrayList; import java.util.Iterator; /** * ArrayList去除集合中字符串的重复值,只能在本集合内

【去除集合中字符串的重复值-2】

package com.yjf.esupplier.common.test; import java.util.ArrayList; import java.util.Iterator; /** * @author shusheng * @description 去除集合中字符串的重复值(字符串的内容相同) * @Email [email protected] * @date 2018/12/12 16:55 */ public class ArrayListDemo1 { public sta

【去除集合中字符串的重复值-3】

package com.yjf.esupplier.common.test; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; /** * @author shusheng * @description * @Email [email protected] * @date 2018/12/12 17:11 */ public class ArrayListDemo2 { public

算法--判断数组中是否有重复值

判断数组中是否有重复值 第14节 重复值判断练习题 请设计一个高效算法,判断数组中是否有重复值.必须保证额外空间复杂度为O(1). 给定一个int数组A及它的大小n,请返回它是否有重复值. 测试样例: [1,2,3,4,5,5,6],7 返回:true Java (javac 1.7) 代码自动补全 1 import java.util.*; 2 3 public class Checker { 4 public boolean checkDuplicate(int[] a, int n) {

js 判断数组中是否存在重复值

// 检测是否有重复值            function isRepeat(arr) { var isRepeat = false;                var hash = {}; for(var i in arr) { if (hash[arr[i]]) { isRepeat = arr[i];                        return isRepeat;                    } hash[arr[i]] = true;          

PHP判断数组中是否有重复值并找出重复值

可以用来测试需要唯一凭据号码的,是否有重复值,不过一般直接使用uuid了,简单粗暴就解决问题,这个就简单的测试生成的数据是否有重复值吧 <?php /* * @Author: wyy * @Date: 2019-01-09 13:34:16 * @Email: [email protected] * @Last Modified by: wyy * @Last Modified time: 2019-01-09 13:48:39 */ /** * 生成抽奖好 * @method build_ra

去除集合中重复字符串元素的案例

public class demo6 { public static void main(String[] args){ //创建集合对象 ArrayList list = new ArrayList(); //赋值 list.add("come"); list.add("baby"); list.add("baby"); list.add("hello"); list.add("come"); list.

Distinct去除集合中的重复项GetHashCode方法没有返回obj.GetHashCode()导致出错

这个自定义类必须继承IEqualityComparer接口,并且实现该接口方法public int GetHashCode(ActionInfo obj)必须返回 正确写法应该是下面这样子

SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用

进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: 1 select t.* from [PeopleCount] as t where t.procedureID='8334' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单纯的查询 GX 这一列用 distinct 是没有任何问题的 1 select distinct t.GX from [PeopleCount] as t where t.pr