Java 在ArrayList上排序,但ArrayList具有相同的值

Java 在ArrayList上排序,但ArrayList具有相同的值,java,file,sorting,arraylist,Java,File,Sorting,Arraylist,我的文本是 ---- SınİSırU, SınİUzO puanları azalan : ------ {deneme deneme deneme}{ANYA}{LTD. ŞTİ.}{1.75}{1.25}{14.00}{10.00}{1.75}{1.2}{false} ------ {deneme deneme deneme }{DZLİ}{ ŞTİ.}{1.43}{1.14}{11.43}{9.14}{1.43}{0.57}{false} ------ {deneme deneme de

我的文本是

---- SınİSırU, SınİUzO puanları azalan :
------ {deneme deneme deneme}{ANYA}{LTD. ŞTİ.}{1.75}{1.25}{14.00}{10.00}{1.75}{1.2}{false}
------ {deneme deneme deneme }{DZLİ}{ ŞTİ.}{1.43}{1.14}{11.43}{9.14}{1.43}{0.57}{false}
------ {deneme deneme deneme }{MEİN}{ ŞTİ.}{1.29}{1.10}{10.29}{8.76}{1.29}{0.55}{false}
------ {deneme deneme deneme }{HAY}{ ŞTİ.}{1.29}{1.10}{10.29}{8.76}{1.29}{0.55}{false}
------ {deneme deneme deneme }{AHAN}{ ŞTİ.}{1.29}{1.10}{10.29}{8.76}{1.29}{0.55}{false}
------ {deneme deneme deneme }{MSA}{ ŞTİ.}{1.29}{1.10}{10.29}{8.76}{1.29}{0.55}{false}
------ {deneme deneme deneme }{Ş}{ ŞTİ.}{1.29}{1.10}{10.29}{8.76}{1.29}{0.55}{false}
------ {deneme deneme deneme }{KO}{. ŞTİ.}{1.25}{1.08}{10.00}{8.67}{1.25}{0.54}{false}
科隆1:1.75,1.43,1.29

科隆2:1.25,1.14,1.10

然后,ı将它们从小到大排序

有替换号码ı不想替换号码。对于exemaple,如果我的包含文本包含3,3,3,3,2,2,2,1,1,1,1,1,1,1,1

ı只想要1 2 3

 public class PointStatue {


    private static List<Double> col1 = new ArrayList<Double>(); 
    private static List<Double> col2 = new ArrayList<Double>();
    private static List<Double> col3 = new ArrayList<Double>();
    private static  List<Double> col4 = new ArrayList<Double>();
    private static List<Double> col5 = new ArrayList<Double>();
    private static List<Double> col6 = new ArrayList<Double>();
    private static List<Double> list=new ArrayList<Double>();


    private static List<Double> numbers = new ArrayList<Double>();

    public static void main(String[] args) throws IOException {

        ArrayList<String> puan;
        puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
        System.out.format("%d kayıt okundu.%n", puan.size());

        for (int j = 0; j < puan.size(); j++) {

            String point = puan.get(j);

            String[] edit = point.split("[\\{\\}\\)]");

            for (String s : edit) {

                String a = s.replaceAll("[\\}\\-\\>\\>\\]\\[\\#\\*\\{]+", "");
                String b = a.replaceAll("[\\.]+", ",");
                Scanner scanner = new Scanner(b);

                while (scanner.hasNext()) {

                    if (scanner.hasNextDouble()) {
                        Double doubleValue = scanner.nextDouble();  
                        numbers.add(doubleValue);
                        // System.out.println(Arrays.deepToString(numbers.toArray()));

                    }// if scan end

                    else {

                        //if it is string comes value here
                        String stringValue = scanner.next();

                    }// if scan end

                }// while end

            }// for string s

        }// for j end


        int col=1;

        for(int i=0;i<numbers.size();i++)
        {

            Double rowValue = numbers.get(i);

            switch(col)
            {
            case 1:
                col1.add(rowValue);
                Collections.sort(col1);
                col++;
                break;
            case 2:
                col2.add(rowValue);
                Collections.sort(col2);
                col++;
                break;
            case 3:
                col3.add(rowValue);
                Collections.sort(col3);
                col++;
                break;
            case 4:
                col4.add(rowValue);
                Collections.sort(col4);
                col++;
                break;
            case 5:
                col5.add(rowValue);
                Collections.sort(col5);
                col++;
                break;
            case 6:
                col6.add(rowValue);
                Collections.sort(col6);
                col = 1;
                break;

            }//switch end

        }//for i end


        System.out.println("kolon1"+col1);
        System.out.println("kolon2"+col2);
        System.out.println("kolon3"+col3);
        System.out.println("kolon4"+col4);
        System.out.println("kolon5"+col5);
        System.out.println("kolon6"+col6);

    }// main end

    private static ArrayList<String> okuDiziyeKoy(String dosyaAdı) {

        ArrayList<String> dizi = new ArrayList<String>();
        try {
            FileInputStream fIS;
            fIS = new FileInputStream(dosyaAdı);
            Reader r = new InputStreamReader(fIS, "ISO-8859-9");
            BufferedReader bR = new BufferedReader(r);
            String satır;

            while ((satır = bR.readLine()) != null) {
                dizi.add(satır);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dizi;
    }// okuDiziyeKoyEnd

}// class end
公共类{
私有静态列表col1=newarraylist();
private static List col2=new ArrayList();
private static List col3=new ArrayList();
private static List col4=new ArrayList();
private static List col5=new ArrayList();
private static List col6=new ArrayList();
私有静态列表=新的ArrayList();
私有静态列表编号=new ArrayList();
公共静态void main(字符串[]args)引发IOException{
阿雷利斯特·普安;
puan=okuDiziyeKoy(“C:\\deneme\\HW.txt”);
System.out.format(“%d kayıt okundu.%n”,puan.size());
对于(int j=0;j\\\\]\[\\\\\\\\\*\{]+”,“”);
字符串b=a.replaceAll(“[\\.]+”,“,”);
扫描仪=新扫描仪(b);
while(scanner.hasNext()){
if(scanner.hasNextDouble()){
Double doubleValue=scanner.nextDouble();
数字。添加(双值);
//System.out.println(Arrays.deepToString(numbers.toArray());
}//如果扫描结束
否则{
//如果是字符串,则在此处输入值
String stringValue=scanner.next();
}//如果扫描结束
}//当结束时
}//对于字符串s
}//对于j端
int col=1;
对于(int i=0;i来看:

基于树映射的NavigableSet实现 使用其自然排序,或通过提供的比较器进行排序 根据使用的构造函数设置创建时间

这应该确保每个元素只有一个,并且列表根据需要进行排序。

查看:

基于树映射的NavigableSet实现 使用其自然排序,或通过提供的比较器进行排序 根据使用的构造函数设置创建时间

这应该确保每个元素只有一个,并且列表根据需要进行排序。

simple buddy:)

package com.blogspot.arashmd.examples;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类复制删除{
公共静态void main(字符串…arg){
双val[]={1,4.3,5,2,4,4,6.5,2,2,2,3.5,3.7,4,7,8,9};
对于(int i=0;isimple buddy:)

package com.blogspot.arashmd.examples;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类复制删除{
公共静态void main(字符串…arg){
双val[]={1,4.3,5,2,4,4,6.5,2,2,2,3.5,3.7,4,7,8,9};

对于(int i=0;i试试这个,它会在所有行中找到重复的值,每一行也适用于我:)

/*
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类复制删除{
公共静态void main(字符串…arg){
双val[]={1,4.3,5,2,4,4,6.5,2,2,2,3.5,3.7,4,7,8,9};

对于(int i=0;i试试这个,它会在所有行中找到重复的值,每一行也适用于我:)

/*
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类复制删除{
公共静态void main(字符串…arg){
双val[]={1,4.3,5,2,4,4,6.5,2,2,2,3.5,3.7,4,7,8,9};

对于(int i=0;你不能用集合代替列表吗?例如看HashSetı不想更改我的arraylist值.ı只想用块替换的数字你不能用集合代替列表吗?例如看HashSetı不想更改我的arraylist值.ı只想用块替换的数字,但ı不应该更改我的实际值,因为这段代码将e 1500双值后that@user2583040,它不会更改实际值-TreeSet同时排序和消除重复,这正是您所要求的。如果您希望在完成后将值返回到ArrayList中,只需调用
new ArrayList(treeSetInstance)
@user2583040:正如@David所建议的,您只需迭代当前列表并将值放入
TreeSet
。该集合将自动排序并删除数据中可能存在的任何重复项。@npinti您有关于它的示例吗?因为ı从未听过TreeSet:(@user2583040:您可以在这里检查:这是一个非常基本的示例,但它应该涵盖您需要的内容。您也可以提供一个比较器,以便选择对象的排序方式。我建议您检查Javadoc(在我的回答中链接)更多信息。但ı不应更改我的实际值,因为此代码在that@user2583040,它不会更改实际值-树集同时排序和消除重复,这正是您所要求的。如果您希望在完成后将值返回到ArrayList中,只需调用
new ArrayList即可(treeSetInstance);
@user2583040:正如@David所建议的,您所需要做的就是迭代当前列表并将值放入
TreeSet
中。该集合将自动排序并删除数据中可能存在的任何重复项。@npinti您对此有任何示例吗?因为ı从未听过TreeSet:(@user2583040:您可以在这里检查:这是一个非常基本的示例,但它应该涵盖您需要的内容。您也可以提供一个比较器,以便选择对象的排序方式。我建议您检查
package com.blogspot.arashmd.examples;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove {
public static void main(String...arg){ 
  double val[]={1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9};
  for(int i=0;i<val.length;i++){
    add(val[i]);
  }
  Collections.sort(list);
  System.out.println(list);
 }
static List<Double> list=new ArrayList<>();
public static void add(double d){
  if(!list.contains(d)){list.add(d);}
}
}
/*
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove {
public static void main(String...arg){ 
  double val[]={1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9};
  for(int i=0;i<val.length;i++){
    add(val[i]);
  }
  Collections.sort(list);
  System.out.println(list);
 }
static List<Double> list=new ArrayList<>();
public static void add(double d){
  if(!list.contains(d)){list.add(d);}
}
}
*/

    import java.io.IOException;
    import java.util.*;
    public class PointStatue {
    private static List<Double> col1 = new ArrayList<Double>(); 
    private static List<Double> col2 = new ArrayList<Double>();
    private static List<Double> col3 = new ArrayList<Double>();
    private static  List<Double> col4 = new ArrayList<Double>();
    private static List<Double> col5 = new ArrayList<Double>();
    private static List<Double> col6 = new ArrayList<Double>();
    private static List<Double> list=new ArrayList<Double>();


    private static List<Double> numbers = new ArrayList<Double>();

    public static void main(String[] args) throws IOException {

        ArrayList<String> puan;
        puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
        System.out.format("%d kayıt okundu.%n", puan.size());
        Double doubleValue;
        for (int j = 0; j < puan.size(); j++) {

            String point = puan.get(j);

            String[] edit = point.split("[\\{\\}\\)]");

            for (String s : edit) {

                String a = s.replaceAll("[\\}\\-\\>\\>\\]\\[\\#\\*\\{]+", "");
                String b = a.replaceAll("[\\.]+", ",");
                Scanner scanner = new Scanner(b);

                while (scanner.hasNext()) {

                    if (scanner.hasNextDouble()) {

                        doubleValue = scanner.nextDouble();
                        if(!numbers.contains(doubleValue)){
                        numbers.add(doubleValue);
                        }
                        // System.out.println(Arrays.deepToString(numbers.toArray()));

                    }// if scan end

                    else {

                        //if it is string comes value here
                        String stringValue = scanner.next();

                    }// if scan end

                }// while end

            }// for string s

        }// for j end


        int col=1;

        for(int i=0;i<numbers.size();i++)
        {

            Double rowValue = numbers.get(i);

            switch(col)
            {
            case 1:
                col1.add(rowValue);

                col++;
                break;
            case 2:
                col2.add(rowValue);

                col++;
                break;
            case 3:
                col3.add(rowValue);

                col++;
                break;
            case 4:
                col4.add(rowValue);

                col++;
                break;
            case 5:
                col5.add(rowValue);

                col++;
                break;
            case 6:
                col6.add(rowValue);

                col = 1;
                break;

            }//switch end

        }//for i end
        Collections.sort(col1);
        Collections.sort(col2);
        Collections.sort(col3);
        Collections.sort(col4);
        Collections.sort(col5);
        Collections.sort(col6);
        System.out.println("kolon1"+col1);
        System.out.println("kolon2"+col2);
        System.out.println("kolon3"+col3);
        System.out.println("kolon4"+col4);
        System.out.println("kolon5"+col5);
        System.out.println("kolon6"+col6);

    }// main end  
/*
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove {
public static void main(String...arg){ 
  double val[]={1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9};
  for(int i=0;i<val.length;i++){
    add(val[i]);
  }
  Collections.sort(list);
  System.out.println(list);
 }
static List<Double> list=new ArrayList<>();
public static void add(double d){
  if(!list.contains(d)){list.add(d);}
}
}
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PointStatue {
/*private static List<Double> col1 = new ArrayList<Double>(); 
private static List<Double> col2 = new ArrayList<Double>();
private static List<Double> col3 = new ArrayList<Double>();
private static List<Double> col4 = new ArrayList<Double>();
private static List<Double> col5 = new ArrayList<Double>();
private static List<Double> col6 = new ArrayList<Double>();
private static List<Double> col7 = new ArrayList<Double>();
private static List<Double> col8 = new ArrayList<Double>();*/
//private static List<Double> list=new ArrayList<Double>();
private static ArrayList<List<Double>> rows=new ArrayList<List<Double>>();

private static List<Double> numbers = new ArrayList<Double>();
private static void okuDiziyeKoy(String path,ArrayList<String> arr) throws Exception{
  BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  //skip the first line
  br.readLine();
  String g;
  while((g=br.readLine())!=null){
    arr.add(g);
  }
}
public static void main(String[] args) throws Exception {
    ArrayList<String> puan = new ArrayList<String>();
    okuDiziyeKoy("C:/deneme/HW.txt", puan);
    //   puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
    System.out.format("%d kayıt okundu.%n", puan.size());
    Double doubleValue;

    for (int j = 0; j < puan.size(); j++) {
      List<Double> listToAdd=new ArrayList<Double>();
        rows.add(j, listToAdd);
        String point = puan.get(j);
        //split data(line) by }{
        String[] edit = point.split("[}][{]");
        for(int l=3;l<edit.length-1;l++){
          //parsing String as double
          doubleValue=Double.parseDouble(edit[l]);
          //check if double value is not exist then add it to list
          if(!numbers.contains(doubleValue)){
          numbers.add(doubleValue);
          }
          if(!listToAdd.contains(doubleValue)){
            listToAdd.add(doubleValue);
          }
        }
       /* for (String s : edit) {

            String a = s.replaceAll("[\\}\\-\\>\\>\\]\\[\\#\\*\\{]+", "");
            String b = a.replaceAll("[\\.]+", ",");
            Scanner scanner = new Scanner(b);

            while (scanner.hasNext()) {

                if (scanner.hasNextDouble()) {

                    doubleValue = scanner.nextDouble();
                    if(!numbers.contains(doubleValue)){
                    numbers.add(doubleValue);
                    }
                    // System.out.println(Arrays.deepToString(numbers.toArray()));

                }// if scan end

                else {

                    //if it is string comes value here
                    String stringValue = scanner.next();

                }// if scan end

            }// while end

        }// for string s

    }// for j end*/

        }
    Collections.sort(numbers);
    System.out.println("Unique values(in all rows): ");
    for(Double d :numbers){
        System.out.print(d+" ");
      }
    System.out.println();
    /*int col=1;
    for(int i=0;i<numbers.size();i++)
    {

        Double rowValue = numbers.get(i);

        switch(col)
        {
        case 1:
            col1.add(rowValue);

            col++;
            break;
        case 2:
            col2.add(rowValue);

            col++;
            break;
        case 3:
            col3.add(rowValue);

            col++;
            break;
        case 4:
            col4.add(rowValue);

            col++;
            break;
        case 5:
            col5.add(rowValue);

            col++;
            break;
        case 6:
            col6.add(rowValue);

            col = 1;
            break;

        }//switch end

    }//for i end*/
   /* Collections.sort(col1);
    Collections.sort(col2);
    Collections.sort(col3);
    Collections.sort(col4);
    Collections.sort(col5);
    Collections.sort(col6);*/
    /*System.out.println("kolon1 "+col1);
    System.out.println("kolon2 "+col2);
    System.out.println("kolon3 "+col3);
    System.out.println("kolon4 "+col4);
    System.out.println("kolon5 "+col5);
    System.out.println("kolon6 "+col6);*/
    //sorting each row
    for(int i=0;i<rows.size();i++){
      Collections.sort(rows.get(i));
    }
    //showing each row(array)
    for(int i=0;i<rows.size();i++){
      System.out.println("kolon"+i+": "+rows.get(i));
    }

}// main end  
}
/*
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DupRemove {
public static void main(String...arg){ 
  double val[]={1,4.3,5,2,4,4,4,6.5,2,2,2,2,2,3.5,3.7,4,7,8,9};
  for(int i=0;i<val.length;i++){
    add(val[i]);
  }
  Collections.sort(list);
  System.out.println(list);
 }
static List<Double> list=new ArrayList<>();
public static void add(double d){
  if(!list.contains(d)){list.add(d);}
}
}
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PointStatue {
/*private static List<Double> col1 = new ArrayList<Double>(); 
private static List<Double> col2 = new ArrayList<Double>();
private static List<Double> col3 = new ArrayList<Double>();
private static List<Double> col4 = new ArrayList<Double>();
private static List<Double> col5 = new ArrayList<Double>();
private static List<Double> col6 = new ArrayList<Double>();
private static List<Double> col7 = new ArrayList<Double>();
private static List<Double> col8 = new ArrayList<Double>();*/
//private static List<Double> list=new ArrayList<Double>();
private static ArrayList<List<Double>> rows=new ArrayList<List<Double>>();

private static List<Double> numbers = new ArrayList<Double>();
private static void okuDiziyeKoy(String path,ArrayList<String> arr) throws Exception{
  BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
  //skip the first line
  br.readLine();
  String g;
  while((g=br.readLine())!=null){
    arr.add(g);
  }
}
public static void main(String[] args) throws Exception {
    ArrayList<String> puan = new ArrayList<String>();
    okuDiziyeKoy("C:/deneme/HW.txt", puan);
    //   puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
    System.out.format("%d kayıt okundu.%n", puan.size());
    Double doubleValue;
    for (int j = 0; j < 6; j++) {
      rows.add(new ArrayList<Double>());
    }
    for (int j = 0; j < puan.size(); j++) {
        String point = puan.get(j);
        //split data(line) by }{
        String[] edit = point.split("[}][{]");
        for(int l=3;l<edit.length-1;l++){
          //parsing String as double
          doubleValue=Double.parseDouble(edit[l]);
          //check if double value is not exist then add it to list
          if(!numbers.contains(doubleValue)){
          numbers.add(doubleValue);
          }
          if(!rows.get(l-3).contains(doubleValue)){
            rows.get(l-3).add(doubleValue);
          }
        }
       /* for (String s : edit) {

            String a = s.replaceAll("[\\}\\-\\>\\>\\]\\[\\#\\*\\{]+", "");
            String b = a.replaceAll("[\\.]+", ",");
            Scanner scanner = new Scanner(b);

            while (scanner.hasNext()) {

                if (scanner.hasNextDouble()) {

                    doubleValue = scanner.nextDouble();
                    if(!numbers.contains(doubleValue)){
                    numbers.add(doubleValue);
                    }
                    // System.out.println(Arrays.deepToString(numbers.toArray()));

                }// if scan end

                else {

                    //if it is string comes value here
                    String stringValue = scanner.next();

                }// if scan end

            }// while end

        }// for string s

    }// for j end*/

        }
    Collections.sort(numbers);
    System.out.println("Unique values(in all rows): ");
    for(Double d :numbers){
        System.out.print(d+" ");
      }
    System.out.println();
    /*int col=1;
    for(int i=0;i<numbers.size();i++)
    {

        Double rowValue = numbers.get(i);

        switch(col)
        {
        case 1:
            col1.add(rowValue);

            col++;
            break;
        case 2:
            col2.add(rowValue);

            col++;
            break;
        case 3:
            col3.add(rowValue);

            col++;
            break;
        case 4:
            col4.add(rowValue);

            col++;
            break;
        case 5:
            col5.add(rowValue);

            col++;
            break;
        case 6:
            col6.add(rowValue);

            col = 1;
            break;

        }//switch end

    }//for i end*/
   /* Collections.sort(col1);
    Collections.sort(col2);
    Collections.sort(col3);
    Collections.sort(col4);
    Collections.sort(col5);
    Collections.sort(col6);*/
    /*System.out.println("kolon1 "+col1);
    System.out.println("kolon2 "+col2);
    System.out.println("kolon3 "+col3);
    System.out.println("kolon4 "+col4);
    System.out.println("kolon5 "+col5);
    System.out.println("kolon6 "+col6);*/
    //sorting each row
    for(int i=0;i<rows.size();i++){
      Collections.sort(rows.get(i));
    }
    //showing each row(array)
    for(int i=0;i<rows.size();i++){
      System.out.println("kolon"+(i+1)+": "+rows.get(i));
    }

}// main end  
}