英文:
Removing duplicates while sorting numbers inside a String in java
问题
我需要对包含数字的字符串进行排序
例如:
输入:{"1", "12", "12", "2", "ABC", "a"}
预期输出:1 2 12 a ABC
我的输出:1 2 12 12 a ABC
我无法去除重复项。有人可以帮我吗?
以下是我用于排序的代码
public static String[] Sort(String[] list) {
Comparator
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Arrays.sort(list, comp);
return list;
}
提前感谢
英文:
I need to sort strings containing numbers
Eg :
input : {"1","12","12","2","ABC","a"}
Expected output: 1 2 12 a ABC
My output :1 2 12 12 a ABC
I am not able to remove duplicate. can anyone help me with this ?
Below is the code I used for sorting
public static String[] Sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Arrays.sort(list, comp);
return list;
}
Thanks in Advance
答案1
得分: 1
以下是翻译好的部分:
处理这个问题最简单的方法是使用 Streams。
这将使我们能够用一行代码解决这个问题。
String[] sorted = Arrays.stream(list).distinct().sorted((s1, s2) -> compare(s1, s2)).toArray(String[]::new);
编辑: 添加了使用给定比较器来完全支持数字字符串和其他字符串。
另外,比较器应在应用了 toLowerCase()
后再比较字符串。结果为:
catch (NumberFormatException e) {
return str1.toLowerCase().compareTo(str2.toLowerCase());
}
英文:
The easiest way of approaching this problem is using Streams.
This would let us tackle this problem using a single line of code.
String[] sorted = Arrays.stream(list).distinct().sorted((s1,s2)->compare(s1,s2)).toArray(String[]::new);
edit: added use of the given comparator for full support of both number strings and other strings.
Also, the comparator should compare the strings after toLowerCase()
have been applied. Resulting in
catch (NumberFormatException e) {
return str1.toLowerCase().compareTo(str2.toLowerCase());
}
答案2
得分: 0
以下是您要翻译的代码部分:
Set可以用来去除重复项。
public static Set<String> sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Set<String> set = new TreeSet<String>(comp);
set.addAll(Arrays.asList(list));
System.out.println(set);
return set;
}
英文:
Set can be used to remove dublicates
public static Set<String> sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Set<String> set = new TreeSet<String>(comp);
set.addAll(Arrays.asList(list));
System.out.println(set);
return set;
}
答案3
得分: 0
以下是翻译好的内容:
你希望进行排序并去除重复项。这意味着有两个步骤,对于比较器来说同时处理这两个步骤比较困难,你需要从数组中获取不重复的字符串,所以先排序然后获取唯一的字符串,有很多种方法,但我选择了一种易于理解的方法:
public static String[] Sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Arrays.sort(list, comp);
ArrayList<String> new_list = new ArrayList<String>();
if (list.length > 0) {
new_list.add(list[0]);
}
for (String s : list) {
// 与列表中最后添加的字符串进行比较,如果不相同则添加
if (!new_list.get(new_list.size() - 1).equals(s)) {
new_list.add(s);
}
}
return new_list.toArray(new String[new_list.size()]);
}
英文:
You want to sort as well remove duplicate. it means its two process that is difficult for comparator to do both, you need to get distinct string in array, so sort and then get only distinct, there are lot of ways, but i did which is easy to understand:
public static String[] Sort(String[] list) {
Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
try {
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
return Integer.compare(num1, num2);
}
catch (NumberFormatException e) {
return str1.compareTo(str2);
}
}
};
Arrays.sort(list, comp);
ArrayList<String> new_list = new ArrayList<String>;
if(list.length()>0){
new_list.add(list[0]);
}
for(String s : list){
//comparing with the last added string in list, if not same then add
if(!new_list.get(new_list.size()-1).equals(s)){
new_list.add(s);
}
}
return new_list.toArray(new String[new_list.size()]);
}
答案4
得分: 0
试一试这个。我添加了一个更复杂的相同元素数组来进行验证。
String[] vals = {"1","ABC","12","a", "1", "2", "12","2","ABC", "a", "ABC","a"};
Comparator<String> comp = ((a,b) -> {
if (a.matches("\\d+") && b.matches("\\d+")) {
return Integer.compare(Integer.valueOf(a), Integer.valueOf(b));
} else {
return a.toLowerCase().compareTo(b.toLowerCase());
}
});
String[] result = Arrays.stream(vals).distinct().sorted(comp).toArray(String[]::new);
System.out.println(Arrays.toString(result));
输出结果为
[1, 2, 12, a, ABC]
英文:
Try this. I included a more complicated array of the same elements to verify.
String[] vals = {"1","ABC","12","a", "1", "2", "12","2","ABC", "a", "ABC","a"};
Comparator<String> comp = ((a,b)-> {
if (a.matches("\\d+") && b.matches("\\d+")) {
return Integer.compare(Integer.valueOf(a), Integer.valueOf(b));
} else {
return a.toLowerCase().compareTo(b.toLowerCase());
}
});
String[] result = Arrays.stream(vals).distinct().sorted(comp).toArray(String[]::new);
System.out.println(Arrays.toString(result));
Prints
[1, 2, 12, a, ABC]
</details>
# 答案5
**得分**: 0
你已经提出了两个不同的问题!
为了回答第一个问题:
```java
Comparator<String> comparator = new Comparator<String>() {
public int compare(String str1, String str2) {
return isInteger(str1) && isInteger(str2)
? Integer.compare(num1, num2)
: isInteger(str1)
? -1
: isInteger(str2)
? 1
: str1.compareTo(str2);
}
};
private boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
}
catch (NumberFormatException e) {
return false;
}
}
英文:
You've asked 2 differents question !
To answear to the first:
Comparator<String> comparator = new Comparator<String>() {
public int compare(String str1, String str2) {
return isInteger(str1) && isInteger(str2)
? Integer.compare(num1, num2)
: isInteger(str1)
? -1
: isInteger(str2)
? 1
: str1.compareTo(str2);
}
};
private boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
}
catch (NumberFormatException e) {
return false;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论