-5

The core focus of this question is to manipulate a string with comma delimiters and duplicate entries in Java so that it returns a deduped, comma delimited string (all in one line and using native classes).

A.) Cast a comma delimited string as an Array,

B.) Dedupe that Array and then convert it back to a string

C.) Check against that newly deduped string to see how many unique entries there were, such that 1 commas means that there were 2 original unique entries.

D.) All of the above must be condensed into one line if possible. (If not, at least A and B is one line and C is its own line.

The following is a reference/picture of what is outlined above using JavaScript and jQuery. It is only anecdotal.

Consider in jQuery the following code to convert a string into an array and then dedupe said array by first converting it to a string and then cast it as a array again in order to leverage a powerful $.unique() jQuery function.

var daysProxyArray = "Monday,Monday,Friday,Friday".split(',');
daysProxyArray = $.unique(daysProxyArray.toString().split(','));
console.log(daysProxyArray.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Does anyone know how to do the equivalent in Java in one line where the end result is a string that I can count how many times a comma appears? Such that, if 2 commas appear, I will know that 3 unique days were in the original string.

Alexander Dixon
  • 837
  • 1
  • 9
  • 24
  • 3
    Java or Javascript? – R4ncid Apr 27 '22 at 16:38
  • @R4ncid in Java (native language for Talend program) I gave JavaScript example just to explain the way I know how to do it and for reference. Hoping someone with extension background in both languages find this question – Alexander Dixon Apr 27 '22 at 16:41

3 Answers3

2

If you want to count the amount of commas the following code should do the trick (in Java 8+):

String text = "Monday,Monday,Friday,Friday";
long numCommas = text.chars().filter(c -> c == ',').count();
Kenny
  • 530
  • 3
  • 12
2
Arrays.stream("Monday,Monday,Friday,Friday".split(","))
                .distinct()
                .collect(Collectors.joining(","));
Vinh Truong
  • 388
  • 1
  • 10
1

To achieve in Java what you're doing in JQuery:

  1. You first need to split the string and get an array of string with the split elements.

  2. Then, pass the returned array to a Set in order to keep only the unique elements. Since a Set does not directly accept an array you first need to use the asList static method of the Arrays class.

  3. Finally, either print the Set or get an array from the Set and print that.

//Splitting the string
String[] daysProxyArray = "Monday,Monday,Friday,Friday".split(",");

//Using a collection set to only keep the unique elements
Set<String> set = new HashSet(Arrays.asList(daysProxyArray));

//You could either print the set...
System.out.println(set);

//...Or returning the set's elements into an array and use that if you actually need an array
String[] res = new String[0];
res = set.toArray(res);
System.out.println(Arrays.toString(res));
Dan
  • 3,647
  • 5
  • 20
  • 26