Java 如何删除列表中类似的命名字符串?
给定字符串列表/数组:Java 如何删除列表中类似的命名字符串?,java,arrays,list,Java,Arrays,List,给定字符串列表/数组: document document (1) document (2) document (3) mypdf (1) mypdf myspreadsheet (1) myspreadsheet myspreadsheet (2) 如何删除所有副本,但只保留最高的副本号 最终结果为: document (3) mypdf (1) myspreadsheet (2) Set mySet=newhashset(Arrays.asList(您的)); 我从stackoverf
document
document (1)
document (2)
document (3)
mypdf (1)
mypdf
myspreadsheet (1)
myspreadsheet
myspreadsheet (2)
如何删除所有副本,但只保留最高的副本号
最终结果为:
document (3)
mypdf (1)
myspreadsheet (2)
Set mySet=newhashset(Arrays.asList(您的));
我从stackoverflow的另一个用户那里发现,如果它有效,请尝试。祝你好运:)你提出了一个宽泛的问题,所以这里有一个不具体的(尽管如此)“完整”的答案:
X (1)
X (2)
X (3)
治疗比治疗容易
X (1)
X (3)
X (2)
在您的案例中,似乎可以假设最后一个X(n)包含最大的n。这使得使用HashMap(如cainiaofei所建议的)成为一个不错的解决方案。是一个替代解决方案
使用
HashMap
键是名称(例如文档名称(1)
文档(2)文档(3)都是文档)这可以通过以下代码实现
str.substring(0,str.indexOf('(')).trim()
值是键出现的次数,最后遍历地图得到对应值最大的键,结果是
键(value-1)
我建议您使用词汇:
Map<String, Integer> dict = new HashMap<>();
for (String s : listOfInput){
String name = s.split(" ")[0];
String version = s.split(" ")[1].charAt(1);
if(dict.get(name)!=null){
if (Integer.parseInt(version) < dict.get(name)){
continue;
}
}
dict.put(name, version);
}
Map dict=newhashmap();
for(字符串s:listOfInput){
字符串名称=s.split(“”[0];
字符串版本=s.split(“”[1]。字符(1);
if(dict.get(name)!=null){
if(Integer.parseInt(版本)
数据将在字典的末尾:
键值
文件| 3
mypdf | 1
myspreadsheet | 2这里有一种可能的方法,但只有在版本号不超过9(*)的情况下才有效: 1) 按相反顺序对列表排序,以便最新版本首先显示 (*)排序基于字母顺序,除非您的版本号超过一位,否则您应该很好。。例如,10以字母顺序出现在9之前 您的列表将变成:
myspreadsheet (2)
myspreadsheet (1)
myspreadsheet
mypdf (1)
mypdf
document (3)
document (2)
document (1)
document
2) 在列表上迭代,只保留给定文档的第一次出现(即,由于反向排序,最近出现的文档)
3) 如果需要,可以将剩余列表按更自然的顺序排序
List<String> documents = new ArrayList<String>();
documents.add("document");
documents.add("document (1)");
documents.add("document (2)");
documents.add("document (3)");
documents.add("mypdf (1)");
documents.add("mypdf");
documents.add("myspreadsheet (1)");
documents.add("myspreadsheet");
documents.add("myspreadsheet (2)");
// 1) Sort in reverse order, so that the most recent document version appears first
Collections.sort(documents, Collections.reverseOrder());
String lastDocumentName = "";
ListIterator<String> iter = documents.listIterator();
// 2)
while (iter.hasNext()) {
String document = iter.next();
// Store the first part of the String , i.e the document name (without version)
String firstPart = document.split("\\s+")[0];
// Check if this document is a version of the last checked document
// If it is the case, this version is anterior, remove it from the list
if (lastDocumentName.equals(firstPart)) {
iter.remove();
}
// Store this document's name as the last one checked
lastDocumentName = firstPart;
}
// 3) Sort back to natural order
Collections.sort(documents);
for (String doc : documents) {
System.out.println(doc);
}
List documents=new ArrayList();
文件。添加(“文件”);
文件。添加(“文件(1)”;
文件。添加(“文件(2)”;
文件。添加(“文件(3)”;
文件.增补(“mypdf(1)”;
文件。添加(“mypdf”);
文件。添加(“我的前页(1)”;
文件。添加(“myspreadsheet”);
文件。添加(“myspreadsheet(2)”;
//1)按相反顺序排序,以便首先显示最新的文档版本
Collections.sort(文档,Collections.reverseOrder());
字符串lastDocumentName=“”;
ListIterator iter=documents.ListIterator();
// 2)
while(iter.hasNext()){
String document=iter.next();
//存储字符串的第一部分,即文档名(无版本)
字符串第一部分=document.split(\\s+)[0];
//检查此文档是否为上次检查文档的版本
//如果是这种情况,则此版本为前版本,请将其从列表中删除
if(lastDocumentName.equals(第一部分)){
iter.remove();
}
//将此文档的名称存储为上次选中的名称
lastDocumentName=第一部分;
}
//3)按自然顺序排序
收集.分类(文件);
用于(字符串文档:文档){
系统输出打印项次(doc);
}
这是一个利用映射的简单解决方案。
首先在列表中循环,拆分字符串,然后将其添加到映射中,名称作为键,而参数中的内容作为值。对于每个条目,检查键是否已经存在。如果键存在,则比较值并添加下一个e如果值大于已存储的值,则返回地图。最后,循环浏览地图并获取列表
这可能适用于任何类型的输入。我认为
当然,这可以做得更好。如果有人有建议,请随时分享
public static void main(String[] args) {
List<String> list = Arrays.asList("document", "document (1)", "document (2)", "document (3)", "mypdf (1)", "mypdf", "myspreadsheet (1)",
"myspreadsheet", "myspreadsheet (2)");
Map<String, Integer> counterMap = new HashMap<>();
List<String> newList = new ArrayList<>();
for (String item : list) {
if (item.indexOf(')') != -1) {
String namePart = item.substring(0, item.indexOf('(')).trim();
Integer numberPart = Integer.parseInt(item.substring(item.indexOf('(') + 1, item.indexOf(')')));
Integer existingValue = counterMap.get(namePart);
if (existingValue != null) {
if (numberPart > existingValue) {
counterMap.put(namePart, numberPart);
}
} else {
counterMap.put(namePart, numberPart);
}
} else {
newList.add(item);
}
}
Iterator<Entry<String, Integer>> iterator = counterMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Integer> next = iterator.next();
String key = next.getKey();
Integer value = next.getValue();
if (newList.contains(key)) {
newList.remove(key);
}
newList.add(key + " (" + value + ")");
}
System.out.println(newList);
}
publicstaticvoidmain(字符串[]args){
列表列表=Arrays.asList(“文档”、“文档(1)”、“文档(2)”、“文档(3)”、“mypdf(1)”、“mypdf”、“MyPreadSheet(1)”,
"myspreadsheet","myspreadsheet(2)";;
Map counterMap=newhashmap();
List newList=newarraylist();
用于(字符串项:列表){
if(item.indexOf('))!=-1){
字符串namePart=item.substring(0,item.indexOf(“(”).trim();
Integer numberPart=Integer.parseInt(item.substring(item.indexOf('(')+1,item.indexOf(')));
整数existingValue=counterMap.get(namePart);
if(existingValue!=null){
如果(numberPart>existingValue){
计数器映射。放置(名称部分、数字部分);
}
}否则{
计数器映射。放置(名称部分、数字部分);
}
}否则{
public static void main(String[] args) {
List<String> list = Arrays.asList("document", "document (1)", "document (2)", "document (3)", "mypdf (1)", "mypdf", "myspreadsheet (1)",
"myspreadsheet", "myspreadsheet (2)");
Map<String, Integer> counterMap = new HashMap<>();
List<String> newList = new ArrayList<>();
for (String item : list) {
if (item.indexOf(')') != -1) {
String namePart = item.substring(0, item.indexOf('(')).trim();
Integer numberPart = Integer.parseInt(item.substring(item.indexOf('(') + 1, item.indexOf(')')));
Integer existingValue = counterMap.get(namePart);
if (existingValue != null) {
if (numberPart > existingValue) {
counterMap.put(namePart, numberPart);
}
} else {
counterMap.put(namePart, numberPart);
}
} else {
newList.add(item);
}
}
Iterator<Entry<String, Integer>> iterator = counterMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Integer> next = iterator.next();
String key = next.getKey();
Integer value = next.getValue();
if (newList.contains(key)) {
newList.remove(key);
}
newList.add(key + " (" + value + ")");
}
System.out.println(newList);
}
Map<String, List<String>> grouped = input.stream()
.collect(Collectors.groupingBy(preprocessedString(), Collectors.toList()));
List<String> finalResult = grouped.entrySet().stream()
.map(e -> e.getValue().stream()
.max(Comparator.comparing(revisionNumber())).get()) //at this point we have at least one element
.collect(Collectors.toList());
}
private static Function<String, Integer> revisionNumber() {
return s -> s.contains("(") ? Integer.valueOf(s.substring(s.indexOf('(') + 1, s.indexOf(')'))) : 0;
}
private static Function<String, String> preprocessedString() {
return s -> s.contains("(") ? s.substring(0, s.lastIndexOf("(")).trim() : s.trim();
}
List<String> input = Arrays.asList(
"document",
"document (1)",
"document (2)",
"document (3)",
"mypdf (1)",
"mypdf",
"myspreadsheet (12)",
"myspreadsheet",
"myspreadsheet (2)",
"single");
public List<String> removeDuplicates(List<String> inputArray) {
Map<String, Integer> map = new HashMap<String, Integer>();
List<String> result = new ArrayList<String>();
int numberOfOcurences = 0;
for (int i = 0; i < inputArray.size(); i++) {
String element = inputArray.get(i);
if (element.charAt(element.length() - 1) == ')') {
numberOfOcurences = Character.getNumericValue(element.charAt(element.length() - 2));
element = element.substring(0, element.length() - 4);
} else {
numberOfOcurences = 0;
}
if (map.isEmpty()) {
map.put(element, numberOfOcurences);
} else {
if (null != map.get(element) && map.get(element) < numberOfOcurences) {
map.put(element, numberOfOcurences);
} else if (null == map.get(element)) {
map.put(element, numberOfOcurences);
}
}
}
for (String a : map.keySet()) {
result.add(a + " (" + map.get(a)+ ")");
}
return result;
}