Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TreeSet到LinkedHashSet使用排序参数,如何编写流函数_Java - Fatal编程技术网

Java TreeSet到LinkedHashSet使用排序参数,如何编写流函数

Java TreeSet到LinkedHashSet使用排序参数,如何编写流函数,java,Java,我有一个非常简单的类,叫做customer: public class Customer implements Comparable<Customer>{ public String customerName; public Customer(String customerName){ this.customerName = customerName; } @Override public int compareTo(Cus

我有一个非常简单的类,叫做customer:

public class Customer implements Comparable<Customer>{
    public String customerName;

    public Customer(String customerName){
        this.customerName = customerName;
    }

    @Override
    public int compareTo(Customer o) {
        return customerName.compareTo(o.customerName);
    }
}
公共类客户实现可比较{
公共字符串客户名称;
公共客户(字符串客户名称){
this.customerName=客户名称;
}
@凌驾
公共内部比较(客户o){
返回customerName.compareTo(o.customerName);
}
}
在我的代码中的某个地方,我有一个这些客户的树集和一个包含所有客户名称(已排序)的字符串。我需要使用流将
TreeSet
转换为
LinkedHashSet

import java.util.LinkedHashSet;
import java.util.TreeSet;

public class TestClass {
    public static void main(String[] args) {

        // All customers as a TreeSet
        TreeSet<Customer> custTreeSet = new TreeSet<>();
        custTreeSet.add(new Customer("Mary"));
        custTreeSet.add(new Customer("John"));
        custTreeSet.add(new Customer("David"));
        custTreeSet.add(new Customer("Andrew"));
        custTreeSet.add(new Customer("Bill"));

        // All names of customers
        String allNames = "Bill/Andrew/David/John/Mary";

        // Need to get LinkedHashSet, where all customers appear as they are in "allNames" variable
        LinkedHashSet<Customer> custLinkedSet = sortCustomers(custTreeSet, allNames);

    }

    /*
    * How can I refactor this code using stream?
    * i.e. conversion from TreeSet => LinkedHashSet, accorging to names in string
    * */
    public static LinkedHashSet<Customer> sortCustomers(TreeSet<Customer> inputTreeSet, String custOrder){
        LinkedHashSet<Customer> result = new LinkedHashSet<>();

        String[] allnames = custOrder.split("/");
        for (int i = 0; i<allnames.length; i++) {
            String currentName = allnames[i];
            System.out.println(currentName);
            for (Customer cust : inputTreeSet){
                if (cust.customerName.equals(currentName)){
                    result.add(cust);
                    System.out.println("added");
                }
            }
        }
        return result;
    }
}
import java.util.LinkedHashSet;
导入java.util.TreeSet;
公共类TestClass{
公共静态void main(字符串[]args){
//将所有客户视为一个树集
TreeSet custreeset=新树集();
添加(新客户(“玛丽”);
添加(新客户(“约翰”);
添加(新客户(“大卫”);
添加(新客户(“Andrew”);
添加(新客户(“账单”);
//所有客户姓名
String allNames=“比尔/安德鲁/大卫/约翰/玛丽”;
//需要获取LinkedHashSet,其中所有客户都以“AllName”变量的形式出现
LinkedHashSet-custLinkedSet=sortCustomers(custTreeSet,AllName);
}
/*
*如何使用流重构此代码?
*即从TreeSet=>LinkedHashSet转换,根据字符串中的名称
* */
公共静态LinkedHashSet sortCustomers(树集inputTreeSet,字符串custOrder){
LinkedHashSet结果=新建LinkedHashSet();
字符串[]allnames=custOrder.split(“/”);

对于(int i=0;i为什么需要原始树集

LinkedHashSet<Customer> lhs = Arrays.stream( 
    custOrder.split("/") 
).map(Customer::new).collect( 
    Collectors.toCollection(
          LinkedHashSet::new
    )
);
LinkedHashSet lhs=Arrays.stream(
客户订单分割(“/”)
).map(客户::新建)。收集(
收藏(
LinkedHashSet::新建
)
);
如果必须使用原始树集,请将映射替换为从树集中获取相应客户的映射

LinkedHashSet<Customer> lhs = Arrays.stream( 
    custOrder.split("/") 
).map( string ->{
        return inputTreeSet.stream().filter( 
            customer -> string.equals( 
                customer.customerName() 
            ) 
        ).findFirst() 
    }
).filter( 
    Optional::isPresent 
).map( Optional::get ).collect(
    Collectors.toCollection(
          LinkedHashSet::new
    )
); 
LinkedHashSet lhs=Arrays.stream(
客户订单分割(“/”)
).map(字符串->{
返回InputRetreeSet.stream().filter(
客户->字符串。等于(
customer.customerName()
) 
).findFirst()
}
).过滤器(
可选::isPresent
).map(可选::get).collect(
收藏(
LinkedHashSet::新建
)
); 

这里有一种方法。它只需获取提供的名称列表,然后对该列表中的每个名称进行流式处理,筛选匹配项,然后使用找到的第一个名称

如果没有找到匹配项,将创建一个新客户并在流中传递

LinkedHashSet<Customer> custLinkedSet = sortCustomers(custTreeSet, allNames);

    public static Set<Customer> sortCustomers(Set<Customer> set, String nameList) {
       return Arrays.stream(nameList.split("/"))
            .map(name -> set.stream()
            .filter(cust -> cust.customerName.equals(name))
                // if customer not found, create a new one using
                // that customer's name
                .findFirst().orElse(new Customer(name)))
                .collect(Collectors.toCollection(LinkedHashSet::new));
}
sortMap
如下所示,数字表示顺序

Andrew=1
Bill=0
David=2
John=3
Mary=4
现在,使用该映射通过创建一个比较器来管理排序。排序不是根据名称排序,而是根据该名称的值进行排序

Comparator<Customer> comp = 
            Comparator.comparing(cust-> sortMap.get(cust.customerName));
印刷品

Bill
Andrew
David
John
Mary

您也可以为每个
Customer
对象分配一个
id
,并将其用于排序。

抱歉,这只是一个示例。Initial类非常大且复杂。@Gennadiy我已经更新了它,以便在“map”函数中包含更多内容。注意:如果需要使用尖括号,如
TreeSet
,则需要将它们放在backticks中,否则,它们将被解释为(无效)HTML。
TreeSet<Customer> custTreeSet = new TreeSet<>(comp);

custTreeSet.add(new Customer("Mary"));
custTreeSet.add(new Customer("John"));
custTreeSet.add(new Customer("David"));
custTreeSet.add(new Customer("Andrew"));
custTreeSet.add(new Customer("Bill"));

custTreeSet.forEach(System.out::println);
Bill
Andrew
David
John
Mary