合并 java 中唯一列表中的多个字符串列表 8

Merge multiple list of string in unique list in java 8

编辑 2: : 我有这样的主要数据(列表或数组,没关系):

{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}

我想:

1-将所有包含3的值替换为"julie"

2-所有 val % 15 == 0 的值都应替换为 "jack"

3-也替换所有 val % 5 == 0 应该替换为“john”的值,

注意: 没有 If Else 只是有 Java 8.

最后我应该有这个数据:

("1","2","julie","4","john","6","7","8","9","john","11","12","julie","14","jack","16","17","18","19","john")

对于这个问题,我使用流并将这些值替换为相关字符串,并为每个字符串创建了 3 个相关列表:

1-need1 的结果(将所有包含 3 的值替换为 "julie"): ("1","2","julie","4","5","6","7","8","9","10","11","12","julie","14","15","16","17","18","19","20")

2-need2 的结果(所有 val % 15 == 0 的值都应替换为 "jack"):("1","2","3","4","5","6","7","8","9","10","11","12","13","14","jack","16","17","18","19","20")

3-需要 3 的结果(替换所有 val % 5 == 0 的值应替换为 "john"):("1","2","3","4","john","6","7","8","9","john","11","12","13","14","john","16","17","18","19","john")

现在我想要一个最终结果,如下所示(使用 mereg 这些列表或任何其他方法 不使用 If&Else 仅使用 java8)::

("1","2","julie","4","john","6","7","8","9","john","11","12","julie","14","jack","16","17","18","19","john")

谢谢!

编辑 1:我不确定你的意思:

merge all of them into each other and duplicate values should not be deleted to have below list

我假设你的意思是 return 一个列表,其值存在于所有三个列表中(所有列表共享的公共元素),其中还包括各自列表中的重复值。

也许你可以找到 3 个列表之间的交集(找到它们之间的共同元素),然后从每个列表中追加重复值 - 但是,可以想到这样一种情况所有列表都包含相同的重复值,例如 list1 = [1 1]list2 = [1 1]list3 = [1 1]。您希望最终结果有多少 1s?)

    // merge lists together - only common elements
    List<String> intersect = list1
        .stream()
        .filter(list2::contains)
        .filter(list3::contains)
        .collect(Collectors.toList());
    
    // iterate through your lists
    List<List<String>> listAll = Arrays.asList(list1, list2, list3);
    for (List<String> list: listAll) {
        // find duplicates inside each the list
        Set<String> items = new HashSet<>();
        List<String> duplicateValue = list
           .stream()
           .filter(n -> !items.add(n))
           .collect(Collectors.toList());
        // add to the duplicate to the final result
        intersect.addAll(duplicateValue);
        intersect.addAll(duplicateValue);
    }
    

输出: [1, 4, 7, 8, 11, julie, julie, john, john, jack, jack] 其中 1, 4, 7、8、11在所有列表中都很常见,而名称在各自的单个列表中是重复的


但是,如果您只是想将列表的所有值合并在一起,可以通过以下几种方法来实现:

使用 Stream (https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html) 合并列表。创建一个包含列表的流,然后将其展平以检索所有元素

List<String> uniqueList = Stream.of(list1, list2, list3)
    .flatMap(Collection::stream).collect(Collectors.toList());

List<String> uniqueList = new ArrayList<>();
Stream.of(list1, list2, list3).forEach(joinedList::addAll);

有关详细信息,请参阅此相关问题:

I want to merge all of them into each other and duplicate values should not be deleted

如果你想保留只有重复项并且每个元素应该在结果列表中只出现一次,你可以创建一个中间映射Map<String, Boolean>,这会将每个唯一字符串与一个布尔值相关联,表示它是否重复。

然后在一组映射条目上创建一个流,过滤掉重复项并将剩余的键收集到一个列表中。

List<String> result = Stream.of(collect1, collect2, collect3)
    .flatMap(List::stream)
    .collect(Collectors.toMap(
        Function.identity(),
        str -> false,         // first occurence - key isn't proved to be a duplicate
        (left, right) -> true // is a duplicate
    ))
    .entrySet().stream()
    .filter(Map.Entry::getValue)
    .map(Map.Entry::getKey)
    .collect(Collectors.toList());

这个怎么样:

public void solution() {
        Integer[] integers = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        List<String> numbers = Stream.of(integers).map(this::yourMappingFunction).collect(Collectors.toList());
        LOG.info("numbers: {}", numbers);
        // prints [1, 2, julie, 4, john, 6, 7, 8, 9, john, 11, 12, julie, 14, john, 16, 17, 18, 19, john]
    }

    // in the same class
    public String yourMappingFunction(Integer integer) {
        // write needed if conditions here and return the needed string for each
        // condition
        if (integer.toString().contains("3")) {
            return "julie";
        } else if (integer % 5 == 0) {
            return "john";
        } else if (integer % 15 == 0) {
            return "jack";
        } else {
            return integer.toString();
        }
    }

好的,根据您的说明,我可以像这样创建最终的字符串。

以下是如何根据您的标准合并三个列表。

String[] arr1 = { "1", "2", "julie", "4", "5", "6", "7", "8",
        "9", "10", "11", "12", "julie", "14", "15", "16",
        "17", "18", "19", "20" };
String[] arr2 = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10", "11", "12", "13", "14", "jack", "16", "17",
        "18", "19", "20" };
String[] arr3 = { "1", "2", "3", "4", "john", "6", "7", "8",
        "9", "john", "11", "12", "13", "14", "john", "16",
        "17", "18", "19", "john" };
  • IntStream 从 0 到数组大小
  • 检查每个数组元素是否等于 juliejack 并使用匹配
  • 否则,它必须是 john 或未填充的 space,因此请使用它。
String[] merged = IntStream.range(0, arr1.length)
        .mapToObj(i->
                arr1[i].equals("julie") ? "julie" :
                arr2[i].equals("jack") ? "jack" : arr3[i])
        .toArray(String[]::new);

System.out.println(Arrays.toString(merged));

打印

[1, 2, julie, 4, john, 6, 7, 8, 9, john, 11, 12, julie, 14, jack, 16, 17, 18, 19
, john]

我想你可以像这样直接从数据中做到这一点。

  • 创建一个值列表并随机排列它们。它们也可以是随机的。
  • 流式传输它们并应用您的标准和 return 数组。
List<Integer> list = new ArrayList<>(IntStream.range(0, 21).boxed().toList());
Collections.shuffle(list);
System.out.println(list);

String[] result = list.stream()
        .map(i -> Integer.toString(i).contains("3") ?
                "julie" : i % 15 == 0 ? "jack" :
                i % 5 == 0 ? "john" : i + "")
        .toArray(String[]::new);
            
System.out.println(Arrays.toString(result));

打印源数据和结果

[17, 9, 13, 8, 16, 19, 0, 6, 3, 1, 7, 2, 11, 4, 15, 20, 12, 18, 14, 5, 10]

[17, 9, julie, 8, 16, 19, jack, 6, julie, 1, 7, 2, 11, 4, jack, john, 12, 18, 14
, john, john]

注意:根据您更新的答案,我按照 1、2 和 3 中提供的顺序进行了测试。任何被 15 整除的东西都将被 5 整除。 30 也是如此,它包含“3”并且可以被 515 整除。所以检查的顺序很重要。

您可以使用此方法获取结果

Stream<Integer> list = Stream.of(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
    String result = list
            .map(String::valueOf)
            .map(s -> s.equals("3") ? "julie" : s)
            .map(s -> ( StringUtils.isNumeric(s) ? ((Integer.parseInt(s)%15)==0 ? "jack":s):s))
            .map(s -> ( StringUtils.isNumeric(s) ? ((Integer.parseInt(s)%5)==0 ? "jack":s):s))
            .collect(Collectors.joining(","));
    System.out.print(result);

控制台输出

1,2,julie,4,jack,6,7,8,9,jack,11,12,13,14,jack,16,17,18,19,jack