如何避免在排序的字符串数组中出现重复值?

huangapple 未分类评论45阅读模式
标题翻译

How to avoid duplicate values in a sorted string array?

问题

我发现了这段代码本来是用来避免重复的数字值所以我将它改成了考虑字符串元素它在打印时成功避免了重复的名称但它并没有按字母顺序对名称进行排序

我还希望它不要打印出特定的元素"vacant"因为它将在稍后通过输入更新为一个名称

**这是基于预订系统的用于按升序排序名称因此空元素被称为"vacant"**<br>
请有人能帮帮我

String[] a = {"Anne", "Anne", "Afrid", "vacant", "vacant", "Sammy", "Dora", "vacant"};
HashSet<String> nameSet = new HashSet<>();
for (int i = 0; i < a.length; i++) {
    nameSet.add(a[i]);
}
for (String name : nameSet) {
    System.out.println(name + " ");
}
英文翻译

I found this code which was meant to avoid duplicate values for numbers so I changed it to consider String elements.It successfully avoids duplicate names when printing, but it does not sort the names in alphabetic order.

I also hope that it does not print a specific element "vacant", because it will be later updated with a name through an input.

This is based on a booking system to sort names in ascending order,so empty elements are called "vacant".<br>
Please can someone help me.

String[] a ={&quot;Anne&quot;,&quot;Anne&quot;,&quot;Afrid&quot;,&quot;vacant&quot;,&quot;vacant&quot;,&quot;Sammy&quot;,&quot;Dora&quot;,&quot;vacant&quot;};
HashSet&lt;String&gt; nameSet = new HashSet&lt;&gt;();
for (int i = 0; i &lt; a.length;i++){
	nameSet.add(a[i]);
}
for (String name: nameSet) {
	System.out.println(name+&quot; &quot;);
}

答案1

得分: 0

这里有一个简单的代码来执行以下操作,

  1. 去除重复项
  2. 去除空项
  3. 去除任何空值
  4. 对列表进行忽略大小写排序
  5. 打印它们
	public static void main(String argv[]) {
		//输入
		String[] a = { "Anne", "Anne", "Afrid", "vacant", "vacant", "Sammy", "Dora", "vacant" };
		List<String> list = Stream.of(a) //转换为流
				.filter(Objects::nonNull) //去除空值
				.sorted(new IgnoreCaseStringSorter()) //忽略大小写排序
				.distinct() //去除重复项
				.filter(name -> !name.equalsIgnoreCase("vacant")) //去除 "vacant"
				.collect(Collectors.toList()); //转换为列表
		System.out.println("Names: ");
		list.stream().forEach(System.out::println);//打印列表

	}
	static class IgnoreCaseStringSorter implements Comparator<String> {
		// 用于按升序排序
		public int compare(String a, String b) {
			return a.compareToIgnoreCase(b);
		}
	}

输出:

Names: 
Afrid
Anne
Dora
Sammy

英文翻译

Here is a simple code to do the following,

  1. Removed duplicates
  2. Removed Vacant
  3. Removes anything that has null
  4. Sorts the list ignore case
  5. Prints them
	public static void main(String argv[]) {
		//Input
		String[] a = { &quot;Anne&quot;, &quot;Anne&quot;, &quot;Afrid&quot;, &quot;vacant&quot;, &quot;vacant&quot;, &quot;Sammy&quot;, &quot;Dora&quot;, &quot;vacant&quot; };
		List&lt;String&gt; list = Stream.of(a) //Convert to Stream
				.filter(Objects::nonNull) //Remove Null values
				.sorted(new IgnoreCaseStringSorter()) //Sort ignore case
				.distinct() //Remove duplicates
				.filter(name -&gt; !name.equalsIgnoreCase(&quot;vacant&quot;)) //Remove &quot;vacant&quot;
				.collect(Collectors.toList()); //Convert to list
		System.out.println(&quot;Names: &quot;);
		list.stream().forEach(System.out::println);//Print the list

	}
	static class IgnoreCaseStringSorter implements Comparator&lt;String&gt; {
		// Used for sorting in ascending order
		public int compare(String a, String b) {
			return a.compareToIgnoreCase(b);
		}
	}

Output:

Names: 
Afrid
Anne
Dora
Sammy

huangapple
  • 本文由 发表于 2020年3月16日 21:42:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/60707113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定